break and continue Statements
In addition to selection and repetition statements, C# provides statements break and continue to alter the flow of control. The preceding section showed how break can be used to terminate a switch statement's execution. This section discusses how to use break to terminate any repetition statement.
break Statement
The break statement, when executed in a while, for, do...while, switch, or foreach, causes immediate exit from that statement. Execution continues with the first statement after the control statement. Common uses of the break statement are to escape early from a repetition statement or to skip the remainder of a switch (as in Fig. 6.9). Figure 6.12 demonstrates a break statement exiting a for.
Figure 6.12. break statement exiting a for statement.
1 // Fig. 6.12: BreakTest.cs 2 // break statement exiting a for statement. 3 using System; 4 5 public class BreakTest 6 { 7 public static void Main( string[] args ) 8 { 9 int count; // control variable also used after loop terminates 10 11 for ( count = 1; count <= 10; count++ ) // loop 10 times 12 { 13 if ( count == 5 ) // if count is 5, 14 break; // terminate loop 15 16 Console.Write( "{0} ", count ); 17 } // end for 18 19 Console.WriteLine( " Broke out of loop at count = {0}", count ); 20 } // end Main 21 } // end class BreakTest
|
When the if nested at line 13 in the for statement (lines 1117) determines that count is 5, the break statement at line 14 executes. This terminates the for statement, and the application proceeds to line 19 (immediately after the for statement), which displays a message indicating the value of the control variable when the loop terminated. The loop fully executes its body only four times instead of 10 because of the break.
continue Statement
The continue statement, when executed in a while, for, do...while, or foreach, skips the remaining statements in the loop body and proceeds with the next iteration of the loop. In while and do...while statements, the application evaluates the loop-continuation test immediately after the continue statement executes. In a for statement, the increment expression executes, then the application evaluates the loop-continuation test.
Figure 6.13 uses the continue statement in a for to skip the statement at line 14 when the nested if (line 11) determines that the value of count is 5. When the continue statement executes, program control continues with the increment of the control variable in the for statement (line 9).
Figure 6.13. continue statement terminating an iteration of a for statement.
1 // Fig. 6.13: ContinueTest.cs 2 // continue statement terminating an iteration of a for statement. 3 using System; 4 5 public class ContinueTest 6 { 7 public static void Main( string[] args ) 8 { 9 for ( int count = 1; count <= 10; count++ ) // loop 10 times 10 { 11 if ( count == 5 ) // if count is 5, 12 continue; // skip remaining code in loop 13 14 Console.Write( "{0} ", count ); 15 } // end for 16 17 Console.WriteLine( " Used continue to skip printing 5" ); 18 } // end Main 19 } // end class ContinueTest
|
In Section 6.3, we stated that the while statement can be used in most cases in place of for. 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 application evaluates the repetition-continuation condition, so the while does not execute in the same manner as the for.
|