Section 6.9. Using the Continue Statement in Repetition Statements


6.9. Using the Continue Statement in Repetition Statements

The Continue statement skips the remaining statements in the loop body of a repetition statement and causes control to proceed to the next iteration of the loop. Like the Exit statement, Continue comes in many forms. The Continue Do statement can be executed in a Do While...Loop, Do...Loop While, Do Until...Loop or Do...Loop Until statement to cause the program to skip the remainder of the current iteration of that repetition statement. Similarly, the Continue For statement and Continue While statement cause the program to skip the remainder of the current iteration of For...Next and While loops, respectively. Unlike the Exit statement, the Continue statement is used to alter program control only in repetition statements.

When Continue For is encountered in a For...Next statement, execution continues with the statement's increment expression, then the program evaluates the loop-continuation test. When Continue is used in another type of repetition statement, the program evaluates the loop-continuation (or loop-termination) test immediately after the Continue statement executes. If a control variable's increment occurs in the loop after the Continue statement, the increment is skipped.

Figure 6.16 demonstrates the Continue For, Continue Do and Continue While statements in their respective repetition statements. Each repetition statement displays the values 14 and 610; the value 5 is skipped in each case using an appropriate form of the Continue statement.

Figure 6.16. Continue statement in repetition statements.

  1  ' Fig. 6.16: ContinueTest.vb  2  ' Using the Continue statement in repetition statements.  3   Module ContinueTest  4      Sub Main()  5         Dim counter As  Integer ' loop counter  6  7         ' skipping an iteration of a For...Next statement  8         For counter = 1 To 10  9            If counter = 5  Then 10               Continue For ' skip to next iteration of loop if counter = 5 11            End If 12 13            Console.Write(counter & " ") ' output counter 14         Next 15 16         Console.WriteLine(vbCrLf & _ 17             "Skipped printing in For...Next at counter = 5" & vbCrLf) 18         counter = 0 ' reset counter 19 20         ' skipping an iteration of a Do Until...Loop statement 21         Do Until counter >= 10 22            counter += 1 ' increment counter 23 24            If counter = 5      Then 25               Continue Do ' skip to next iteration of loop if counter = 5 26            End If 27 28            Console.Write(counter & " ") ' output counter 29         Loop 30 31         Console.WriteLine(vbCrLf & _ 32            "Skipped printing in Do Until...Loop at counter = 5" & vbCrLf) 33         counter = 0 ' reset counter 34 35         ' skipping an iteration of a While statement 36         While counter < 10 37               counter +=  1 ' increment counter 38 39               If counter = 5 Then 40                  Continue While ' skip to next iteration of loop if counter = 5 41               End If 42 43               Console.Write(counter & " ") ' output counter 44            End While 45 46            Console.WriteLine( _ 47               vbCrLf & "Skipped printing in While at counter = 5") 48     End Sub ' Main 49  End Module ' ContinueTest 

 1 2 3 4 6 7 8 9 10 Skipped printing in For...Next at counter = 5 1 2 3 4 6 7 8 9 10 Skipped printing in Do Until...Loop at counter = 5 1 2 3 4 6 7 8 9 10 Skipped printing in While at counter = 5 



The header of the For...Next statement (line 8) indicates that the body of the loop is to execute 10 times. During each execution, the If...Then statement (lines 911) determines whether the control variable, counter, is equal to 5. If it is not, line 13 displays the current value of counter and the repetition statement continues to iterate. When the value of counter reaches 5, the Continue For statement (line 10) executes, which terminates the current iteration of the loop, causing the display of the current value of counter to be skipped. The control variable is incremented, then the program evaluates the loop-continuation test. The value of counter is now 6, so the For...Next statement continues to loop, eventually printing the values 14 and 610.

Line 18 sets counter to 0, so that we can loop through the values 110 again, this time using a Do Until...Loop statement. Line 21 indicates that the loop is to continue executing until counter is greater than or equal to 10. Line 22 increments counter. The If...Then statement in lines 2426 tests whether counter is 5. If it is, the Continue Do statement (line 25) executes, causing the display of the variable counter to be skipped. The program then immediately evaluates the loop-termination test (line 21). The value of counter is still 5, which is not greater than or equal to 10, so the loop continues. During the next iteration, the value of counter is incremented to 6, so the loop executes to completion, eventually displaying the values 14 and 610. We placed the increment at the beginning of the loop (line 22) so that it would not be skipped during the iteration when the Continue statement executes. If we had placed the increment after the If...Then statement, the value of counter would reach 5 and never be incremented in subsequent iterations. The If...Then condition would repeatedly evaluate to true, causing an infinite loop.

Line 33 once again sets counter to 0. The While statement in lines 3644 executes while counter is less than 10. Once again, the increment appears at the beginning of the loop. When the value of counter is 5, the If...Then statement's condition (line 39) evaluates to true, and the Continue While statement (line 40) executes, terminating the current iteration of the While statement. Note that the While statement also displays only the values 14 and 610.

The Exit and Continue statements can be used in nested control statements. For instance, Exit For or Continue For can be used in a While statement, as long as the While statement is itself located in a For...Next statement. In such an example, the Exit or Continue statement would be applied to the proper control statement based on the keywords used in the Exit or Continue statementin this example, the For keyword is used, so the For...Next statement is the control statement whose flow of control will be altered. If there are nested loops of the same type (e.g., a For...Next statement within a For...Next statement), the statement that immediately surrounds the Exit or Continue statement is the one affected.

In Section 6.3, we stated that While could be used in most cases in place of For...Next. The one exception occurs when the increment expression in the While follows a Continue statement. In this case, the increment does not execute before the program evaluates the repetition-continuation condition, so the While does not execute in the same manner as the For...Next.



Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net