Looping While a Condition is True in C++ - dummies

Looping While a Condition is True in C++

By Stephen R. Davis

Branch statements allow you to direct the flow of a C++ program’s execution down one path or another. This is a big improvement but still not enough to write full-strength programs.

Consider the problem of updating the computer display. The typical PC must update well over a thousand pixels for each row as it paints an image from left to right. It repeats this process for each of the thousand or so rows on the display. It does this by executing the same small number of instructions, millions of times — once for each pixel.

The simplest form of looping statement is the while loop. Here’s what the while loop looks like:

    // ...repeatedly executed as long as condition is true

The condition is tested. This condition could be if var > 10 or if var1 == var2 or any other expression you might think of as long as it returns a value of true or false. If the condition is true, the statements within the braces are executed. Upon encountering the closed brace, C++ returns control to the beginning, and the process starts over.

If the condition is false, control passes to the first statement after the closed brace. The effect is that the C++ code within the braces is executed repeatedly as long as the condition is true.

If the condition were true the first time, what would make it be false in the future? Consider the following example program:

// WhileDemo - input a loop count. Loop while
//             outputting astring arg number of times.
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
    // input the loop count
    int nLoopCount;
    cout << "Enter loop count: ";
    cin  >> nLoopCount;
    // now loop that many times
    while (nLoopCount > 0)
        nLoopCount = nLoopCount - 1;
        cout << "Only " << nLoopCount
             << " loops to go" << 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;

WhileDemo begins by retrieving a loop count from the user, which it stores in the variable nLoopCount. The program then executes a while loop. The while first tests nLoopCount. If nLoopCount is greater than 0, the program enters the body of the loop (the body is the code between the braces), where it decrements nLoopCount by 1 and outputs the result to the display.

The program then returns to the top of the loop to test whether nLoopCount is still positive.

When executed, the program WhileDemo outputs the results shown in this next snippet. Here, a loop count of 5 is entered. The result is that the program loops five times, each time outputting a countdown:

Enter loop count: 5
Only 4 loops to go
Only 3 loops to go
Only 2 loops to go
Only 1 loops to go
Only 0 loops to go
Press Enter to continue...

If the user enters a negative loop count, the program skips the loop entirely. That’s because the specified condition is never true, so control never enters the loop. In addition, if the user enters a very large number, the program loops for a long time before completing.

A separate, less frequently used version of the while loop known as the do … while appears identical except the condition isn’t tested until the bottom of the loop:

    // ...the inside of the loop
} while (condition);

Because the condition isn’t tested until the end, the body of the do … while is always executed at least once.

The condition is checked only at the beginning of the while loop or at the end of the do… while loop. Even if the condition ceases to be true at some time during the execution of the loop, control does not exit the loop until the condition is retested.