The break- and the continue-Statements

  1. The break-statement can be used within a loop to exit it prematurely. It is the break-statement that was used to break out of the switch-statement to prevent it from falling through.

  2. When the break-statement is executed within a loop, the loop ends immediately and execution continues with the statement following the loop statement.

  3. In a nested loop, the break statement interrupts only the loop it is placed in.

  4. The following example from the textbook illustrates how the break-statement is used:

    // Sums up to 10 negative numbers entered from the keyboard,
    // or until a positive number is entered.
    // Make sure that the positive number is not included in computing the sum

    #include <iostream>
    using namespace std;

    int main( )
    {
       int number, sum = 0, count = 0;
       cout << "Enter 10 negative numbers:\n";

       while (++count <= 10)
       {
          cin >> number;

          if (number >= 0)
          {
             cout << "ERROR: positive number"
                << " or zero was entered as the\n"
                << count << "the number! Input ends "
                << "with the " << count << "th number.\n"
                << count << "the number was not added in.\n";
             break;    // to break out of the loop
          }

          sum += number;
       }

       cout << sum << " is the sum of the first "
                << (count - 1) << " numbers.\n";

    }

  5. This is another example illustrating how the break-statement is used in a for-loop:

    // Using the break statement in a for loop.

    #include <iostream>
    using namespace std;

    int main( )
    {
       int number;

       for ( number = 1; number <= 10; number++)
       {
          // if number is 5, terminate the loop

          if (number == 5)
          {
             break;
          }

          cout << number << " ";
       }
       cout << "\nBroke out of loop when number became " << number << endl;

    }

    The output:

    1 2 3 4
    Broke out of loop when number became 5

  6. The above examples show acceptable use of the break-statement. However it is very often misused to produce inefficient or even incorrect code.

  7. Note that the break-statement does not break out of an if-else block.

  8. The continue statement, when executed within a while, for or do-while structure, skips the remaining statements in the body of that structure and proceeds with the next iteration of the loop. In while and do-while structures, the loop-continuation test evaluates immediately after the continue statement executes. In the for structure, the increment expression executes, then the loop-continuation test evaluates.

  9. The following example illustrates how the continue-statement is used in a for loop:

    // Using the continue statement in a for loop.

    #include <iostream>
    using namespace std;

    int main( )
    {
       int number;

       for ( number = 1; number <= 10; number++)
       {
          // if number is 5, skip that round of the loop

          if (number == 5)
          {
             continue;
          }

          cout << number << " ";
       }
       cout << "\nUsed continue to skip printing the value 5" << endl;

    }

    The output:
    1 2 3 4 6 7 8 9 10
    Used continue to skip printing the value 5


  10. In general, a break is a very poor way of exiting a loop. Misuse of a break caused the failure of an AT&T 4ESS telephone switch on January 15, 1990. The failure propagated through the entire U. S. network, rendering it nearly unusable for about nine hours. A programmer had used a break to terminate an if statement. Unfortunately, break cannot be used with if, so the program execution broke out of the enclosing loop, skipping some variable initializations and running into chaos.

  11. Do not get confused with the fact that the break can and is often used to break out of a switch statement, preventing execution to fall through to the following case.