- 4.1 Introduction
- 4.2 Essentials of Counter-Controlled Iteration
- 4.3 for Iteration Statement
- 4.4 Examples Using the for Statement
- 4.5 Application: Summing Even Integers
- 4.6 Application: Compound-Interest Calculations
- 4.7 do... while Iteration Statement
- 4.8 switch Multiple-Selection Statement
- 4.9 C++17 Selection Statements with Initializers
- 4.10 break and continue Statements
- 4.11 Logical Operators
- 4.12 Confusing the Equality (==) and Assignment (=) Operators
- 4.13 Objects-Natural Case Study: Using the miniz-cpp Library to Write and Read ZIP files
- 4.14 C++20 Text Formatting with Field Widths and Precisions
- 4.15 Wrap-Up
4.10 break and continue Statements
In addition to selection and iteration statements, C++ provides break and continue statements to alter the flow of control. The preceding section showed how break could be used to terminate a switch statement’s execution. This section discusses how to use break in iteration statements.
break Statement
Executing a break statement in a while, for, do…while or switch causes immediate exit from that statement—execution continues with the first statement after the control statement. Common uses of break include escaping early from a loop or exiting a switch (as in Fig. 4.6). Figure 4.8 demonstrates a break statement exiting early from a for statement.
1 // fig04_08.cpp 2 // break statement exiting a for statement. 3 #include <iostream> 4 using namespace std; 5 6 int main() { 7 int count; // control variable also used after loop 8 9 for (count = 1; count <= 10; ++count) { // loop 10 times 10 if (count == 5) { 11 break; // terminates for loop if count is 5 12 } 13 14 cout << count << " "; 15 } 16 17 cout << "\nBroke out of loop at count = " << count << "\n"; 18 }
1 2 3 4 Broke out of loop at count = 5
Fig. 4.8 break statement exiting a for statement.
When the if statement nested at lines 10–12 in the for statement (lines 9–15) detects that count is 5, the break statement at line 11 executes. This terminates the for statement, and the program proceeds to line 17 (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. Note that we could have initialized count in line 7 and left the for header’s initialization section empty, as in:
for (; count <= 10; ++count) { // loop 10 times
continue Statement
Executing the continue statement in a while, for or do…while skips the remaining statements in the loop body and proceeds with the next iteration of the loop. In while and do…while statements, the program evaluates the loop-continuation test immediately after the continue statement executes. In a for statement, the increment expression executes, then the program evaluates the loop-continuation test.
1 // fig04_09.cpp 2 // continue statement terminating an iteration of a for statement. 3 #include <iostream> 4 using namespace std; 5 6 int main() { 7 for (int count{1}; count <= 10; ++count) { // loop 10 times 8 if (count == 5) { 9 continue; // skip remaining code in loop body if count is 5 10 } 11 12 cout << count << " "; 13 } 14 15 cout << "\nUsed continue to skip printing 5" << "\n"; 16 }
1 2 3 4 6 7 8 9 10 Used continue to skip printing 5
Fig. 4.9 continue statement terminating an iteration of a for statement.
Figure 4.9 uses continue (line 9) to skip the statement at line 12 when the nested if determines that count’s value is 5. When the continue statement executes, program control continues with the increment of the control variable in the for statement (line 7).
Some programmers feel that break and continue violate structured programming. Since the same effects are achievable with structured-programming techniques, these programmers prefer to avoid break or continue.
Perf There’s a tension between achieving quality software engineering and achieving the best-performing software. Sometimes one of these goals is achieved at the expense of the other. For all but the most performance-intensive situations, you should first make your code simple and correct, then make it fast and small—but only if necessary.