5.7. break and continue
Statements
In addition to the 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 in a repetition
statement.
break Statement
The break
statement, when executed in a while, for,
do...while or switch statement,
causes immediate exit from that statement. Program execution continues with the
next statement. Common uses of the break
statement are to escape early from a loop or to skip the remainder of a
switch statement (as in Fig.
5.10). Figure
5.13 demonstrates the break statement (line 14) exiting a
for repetition statement.
Fig. 5.13. break statement exiting a
for statement.
1 // Fig. 5.13: fig05_13.cpp
2 // break statement exiting a for statement.
3 #include <iostream>
4 using std::cout;
5 using std::endl;
6
7 int main()
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 )
14 break; // break loop only if x is 5
15
16 cout << count << " ";
17 } // end for
18
19 cout << "\nBroke out of loop at count = " << count << endl;
20 return 0; // indicate successful termination
21 } // end main
|
1 2 3 4
Broke out of loop at count = 5
|
When the if statement detects that count is
5, the break statement executes. This terminates the
for statement, and the program proceeds to line
19 (immediately after the for statement),
which displays a message indicating the control variable value that terminated
the loop. The for statement fully
executes its body only four times instead of 10. Note that the control variable
count is defined outside the for statement header, so that we can use the control variable
both in the loop's body and after the loop completes its execution.
continue Statement
The continue statement, when
executed in a while, for or do...while statement, skips the remaining statements in the body
of that statement and proceeds with the next iteration of the loop. In
while and do...while statements, the loop-continuation test
evaluates immediately after the continue statement executes. In the
for statement, the increment expression executes, then the
loop-continuation test evaluates.
Figure
5.14 uses the continue statement (line 12) in a for
statement to skip the output statement (line 14) when the nested if
(lines 11–12) 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
header (line 9) and loops five more times.
Fig. 5.14. continue statement terminating a
single iteration of a for statement.
1 // Fig. 5.14: fig05_14.cpp
2 // continue statement terminating an iteration of a for statement.
3 #include <iostream>
4 using std::cout;
5 using std::endl;
6
7 int main()
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 cout << count << " ";
15 } // end for
16
17 cout << "\nUsed continue to skip printing 5" << endl;
18 return 0; // indicate successful termination
19 } // end main
|
1 2 3 4 6 7 8 9 10
Used continue to skip printing 5
|
In Section
5.3, we stated that the while statement
could be used in most cases to represent the for
statement. The one exception occurs when the increment expression in the
while statement follows the continue
statement. In this case, the increment does not execute before the program tests
the loop-continuation condition, and the while
does not execute in the same manner as the for.
Good Programming Practice 5.8
|
Some programmers feel
that break and continue violate
structured programming. The effects of these statements can be achieved by
structured programming techniques we soon will see, so these programmers do not
use break and continue. Most
programmers consider the use of break in switch statements
acceptable. |
Performance Tip 5.5
|
The break and
continue statements, when used properly,
perform faster than do the corresponding structured
techniques. |
Software Engineering Observation 5.1
|
There is a
tension between achieving quality software engineering and achieving the
best-performing software. Often, one of these goals is achieved at the expense
of the other. For all but the most performance-intensive situations, apply the
following rule of thumb: First, make your code simple and correct; then make it
fast and small, but only if
necessary. |