Breaking out of the Middle of a C++ while Loop

By Stephen R. Davis

Sometimes the condition that causes you to terminate a while loop doesn’t occur until somewhere in the middle of the loop. This is especially true when testing user input for some termination character. C++ provides these two control commands to handle this case:

  • break exits the inner most loop immediately.

  • continue passes control back to the top of the loop.

The following Product program demonstrates both break and continue. This program multiplies positive values entered by the user until the user enters a negative number. The program ignores zero.

//  Product - demonstrate the use of break and continue.
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
    // enter the number to calculate the factorial of
    cout << "This program multiplies the numbersn"
         << "entered by the user. Enter a negativen"
         << "number to exit. Zeroes are ignored.n"
         << endl;
    int nProduct = 1;
    while (true)
        int nValue;
        cout << "Enter a number to multiply: ";
        cin  >> nValue;
        if (nValue < 0)
            cout << "Exiting." << endl;
        if (nValue == 0)
            cout << "Ignoring zero." << endl;
        // multiply accumulator by this value and
        // output the result
        cout << nProduct << " * " << nValue;
        nProduct *= nValue;
        cout << " is " << nProduct << endl;
    // wait until user is ready before terminating program
    // to allow the user to see the program results
    cout << "Press Enter to continue..." << endl;
    cin.ignore(10, 'n');
    return 0;

The program starts out with an initial value of nProduct of 1. The program then evaluates the logical expression true to see if it’s true. It is.

There aren’t too many rules that hold in C++ without exception, but here’s one: true is always true.

The program then enters the loop to prompt the user for another value to multiply times nProduct, the accumulated product of all numbers entered so far. If the value entered is negative, then the program outputs the phrase “Exiting.” before executing the break, which passes control out of the loop.

If the value entered is not negative, control passes to the second if statement. If nValue is equal to zero, then the program outputs the messages “Ignoring zero.” before executing the continue statement which passes control back to the top of the loop to allow the user to enter another value.

If nValue is neither less than zero nor zero, then control flows down to where nValue is multiplied by nProduct using the special assignment operator:

nProduct *= nValue;

This expression is the same as:

nProduct = nProduct * nValue;

The output from a sample run from this program appears as follows:

This program multiplies the numbers
entered by the user. Enter a negative
number to exit. Zeroes are ignored.
Enter a number to multiply: 2
1 * 2 is 2
Enter a number to multiply: 5
2 * 5 is 10
Enter a number to multiply: 0
Ignoring zero.
Enter a number to multiply: 3
10 * 3 is 30
Enter a number to multiply: -1
Press Enter to continue . . .