Section 6.8. Using the Exit Statement in Repetition Statements


6.8. Using the Exit Statement in Repetition Statements

The Exit statement can be used to alter a program's flow of control. There are many forms of the Exit statement, designed to exit different types of code blocks. In this section, we focus on using the Exit statement in repetition statements. The Exit 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 exit immediately from that repetition statement. Similarly, the Exit For statement and the Exit While statement cause immediate exit from For...Next and While...End While loops, respectively. Execution continues with the first statement after the repetition statement. Figure 6.15 demonstrates the Exit For, Exit Do and Exit While statements in their respective repetition statements.

Figure 6.15. Exit statement in repetition statements.

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

 1 2 3 4 Broke out of For...Next at counter = 5 1 2 3 4 Broke out of Do Until...Loop at counter = 5 1 2 3 4 Broke out of 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 1012) determines whether the control variable, counter, is equal to 5. For the first four iterations of the loop (counter values 14), this condition is false, so program control proceeds to line 14, which displays the current value of counter. Once the value of counter reaches 5, the condition in line 10 evaluates to true, so the Exit For statement (line 11) executes, terminating the execution of the loop. Program control then proceeds to the output statement in lines 1718, which displays the value of counter after the loop has been exited.

Line 19 resets the value of counter for the next repetition statement. The header of the Do Until...Loop statement (line 22) indicates that the loop should continue executing until counter is greater than 10. When counter has values in the range 14, the body of the If...Then statement (lines 2426) is skipped, the current value of counter is displayed (line 28) and counter is incremented (line 29). However, when counter is 5, the Exit Do statement (line 25) executes, terminating the loop. Lines 3233 display the value of counter after the loop has been exited. Note that the program does not increment counter (line 29) after the Exit Do statement executes.

Line 34 once again resets the value of counter for the next repetition statement. The header of the While statement (lines 37) indicates that the loop should continue executing while counter is less than or equal to 10. When counter is 5, the Exit While statement (line 40) executes, terminating execution of the While statement. Lines 4748 display the value of counter after the loop has been exited.

The Exit Select statement can be used to exit a Select...Case statement. The Exit Property statement can be used to exit from a property. We encounter different forms of the Exit statement throughout the book.



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