The break- and the continue-Statements

  1. The break-statement can be used within a loop to exit it prematurely.

  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;
          }

          sum = sum + number;
       }

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

       return 0;
    }

  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;

       return 0;
    }

    The output:

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

  6. 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.

  7. 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;

       return 0;
    }

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