6.3. For...Next Repetition Statement

Section 6.2 presented the essentials of counter-controlled repetition with the While statement. Visual Basic also provides the For...Next repetition statement, which specifies the counter-controlled repetition details in a single line of code. To illustrate the power of For...Next, we rewrite the program of Fig. 6.1 using the For...Next statement in Fig. 6.2.

Figure 6.2. Counter-controlled repetition with the For...Next statement.
					1
					' Fig. 6.2: ForCounter.vb
					2
					' Using the For...Next statement for counter-controlled repetition.
					3
					Module ForCounter
 4
					Sub Main()
 5
					' initialization, repetition condition and             
					6
					' incrementing are all included in For...Next statement
					7
					For counter As Integer = 2
						To
						10
						Step
						2                

Get Visual Basic 2005 for Programmers: Deitel Developer Series, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.