The For - Next Loop
The For - Next loop is a good choice for loops where a known number of passes must be made through the loop. For example, if a loop is supposed to total the monthly sales for the year, chances are pretty good that we shouldn't make 13 passes through the loop. If you're doing something with days of the week, it seems reasonable that there will be seven passes through the loop. If you have a good idea of how many passes must be made through the loop, chances are good that the For - Next loop will do the job.
The For - Next loop has the following basic syntax format:
For LoopCounter = StartValue To EndValue [ Step BumpIt ] ' For statement block Next [ LoopCounter ]
Let's use a For - Next loop in place of our earlier Goto loop. The new code is shown in Listing 12.2.
Listing 12.2 The Simple Loop Program Modified to Use a For - Next Loop Structure
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles btnStart.Click Dim i As Integer, Sum As Integer, Terminate As Integer Dim NewLine As String txtResult.Text = "" NewLine = Chr(13) & Chr(10) Terminate = CInt(txtPasses.Text) ' Target iterations Sum = 0 ' Clear out the running total For i = 1 To Terminate Sum += i txtResult.Text += CStr(Sum) & " (i = " & CStr(i) & ")" & NewLine Next i End Sub
Actually, there aren't too many differences between the two listings. First, we did away with the LoopStart label. We don't need it anymore. Second, because i is initialized at the start of the For statement, we dropped the statement that initialized i equal to 0.
The For statement starts by initializing i to equal its starting value. In our program, we're setting i to equal 1, but it could be any value you want, including negative values. This initialization step is done only once, when we first enter the For loop.
The program then inspects the value for Terminate . In our program, this is the value that's entered by the user . If Terminate is greater than the value of i , the For statement block code is executed. The For statement block is the block of statements after the For statement, but before the Next keyword. It's customary to indent the For statement block lines one tab space, as shown in the listing.
Note that if the value of Terminate is less than the initial value of i , the For statement block isn't executed. If the value of Terminate equals the initial value of i , the block statements are executed only once.
Type in the program changes shown in Listing 12.2 and compile the program. Set a breakpoint on the For statement and run the program. Now single-step the program to see how the code is executed. You'll notice that the For statement is never executed a second time. How does the loop know when to stop? That's the purpose of the Next statement.
The Next Statement
When program execution reaches the Next statement, the loop counter variable ( i in our example) is incremented and then compared to the number of passes that are to be made through the loop ( Terminate in our program). As long as the loop counter is less than or equal to the number of passes to be made through the loop, control is sent back to the first statement following the For statement. Therefore, control is sent back to the
Sum += i
statement after Next has done its job. We continue this pattern of execution until the termination condition is met. That is, we continue until i has been incremented to a value that is greater than Terminate .
I strongly urge you to single-step this program to observe how the program control flow is affected. Try values for Terminate that are 1, 0, and “1 to see how each value affects the loop. When you can accurately predict how the loop will behave with different values for Terminate , you're ready for the next section of the chapter.