5.10 break and continue Statements

In addition to selection and iteration statements, C++ provides statements break (which we discussed in the context of the switch statement) 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 iteration statements.

5.10.1 break Statement

The break statement, when executed in a while, for, dowhile or switch, 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 loop or to skip the remainder of a switch (as in Fig. 5.11). Figure 5.13 demonstrates a break statement exiting a for.

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.

Fig. 5.13 break statement exiting a for statement.

Alternate View

 1   // Fig. 5.13: BreakTest.cpp
 2   // break statement exiting a for statement.
 3   #include <iostream>
 4   using namespace std;
 5
 6   int main() {
 7      unsigned 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 loop if count is 5
12          }
13
14          cout << count << " ";
15      }
16
17      cout << "
Broke out of loop at count = " << count << endl;
18   }

1 2 3 4
Broke out of loop at count = 5

5.10.2 continue Statement

The continue statement, when executed in a while, for or dowhile, skips the remaining statements in the loop body and proceeds with the next iteration of the loop. In while and dowhile 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.

Fig. 5.14 continue statement terminating an iteration of a for statement.

Alternate View

 1   // Fig. 5.14: ContinueTest.cpp
 2   // continue statement terminating an iteration of a for statement.
 3   #include <iostream>
 4   using namespace std;
 5
 6   int main() {
 7      for (unsigned 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 << "
Used continue to skip printing 5" << endl;
16   }

1 2 3 4 6 7 8 9 10
Used continue to skip printing 5

Figure 5.14 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).

In Section 5.3, we stated that while could be used in most cases in place of for. This is not true when the increment expression in the while follows a continue statement. In this case, the increment does not execute before the program evaluates the iteration-continuation condition, so the while does not execute in the same manner as the for.

Software Engineering Observation 5.1

Some programmers feel that break and continue violate structured programming. Since the same effects are achievable with structured-programming techniques, these programmers do not use break or continue.

 

Software Engineering Observation 5.2

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, apply the following rule of thumb: First, make your code simple and correct; then make it fast and small, but only if necessary.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset