Monday, November 30, 2015

The While and Do-While Loops

Interested in learning the C++ programming language? Try doing it with One Hour a Day.

Doing repetitive tasks can be accomplished with a programming loop. This article will cover the while and do-while loops. They appear in many different programming languages and often follow the same construct.

Let us first consider the while loop. When you need to keep on repeating the sames tasks, you can put those tasks within a while loop and it will execute as long as some condition is true:

while (condition)
{
    // do these statements as long as the condition is true
}

In the while loop, first the condition is checked. The condition is anything that yields a Boolean value (either true or false). If the condition in the while loop is true, then the statements within the braces are executed. Otherwise, the loop terminates.

To determine when the loop terminates, a loop-control variable is often used. For example, it could be a variable that keeps a count. As long as the count matches a certain criteria, then the loop would keep on running. As soon as that condition no longer holds, then the while loop terminates.

Consider the following while construct in C++:

// Declares an integer variable and assigns it the value 1
int n = 1;

while (n <= 10)
{
    cout << "The value of n is " << n << endl;

    // This line is important as it makes sure the loop is not infinite
    n++;
}

The above code will produce the following output:

The value of n is 1
The value of n is 2
The value of n is 3
The value of n is 4
The value of n is 5
The value of n is 6
The value of n is 7
The value of n is 8
The value of n is 9
The value of n is 10

Going through the code, a variable n is declared and assigned the value 1. Then comes the while loop: first the condition is checked -- is 1 less than or equal to 10? Yes, it is. Since the condition is true, the statements within the braces are executed: the current value of n is displayed and then the value of n gets incremented. The increment part is very important. If we did not have that line, then the loop would going on infinitely. Infinite loops are to be avoided at all costs because they never end! The program above makes sure the loop ends by adjusting the variable n accordingly in every loop iteration. The condition -- that n is less than 10 -- will eventually become false and thus the loop will terminate.

Let us a now consider the do-while loop:

do
{
    // do these statements (at least once)
} while (condition);

The do-while loop works just like the while loop, but the statements within the braces get executed at least once. That is, when the program reaches the do-while, it starts executing the block statements right away without check the condition! Then, after executing the statements within the braces for the first time, it checks for the condition within the while parenthesis. If that condition is true, the loop statements get executed again. This pattern will keep on repeating as long as the condition is true. When the condition finally becomes false, then the loop terminates and the program goes on to do other stuff.

Consider this program in C++:

int m = 2;

do
{
    cout << "The value of m is " << m << endl;
    m++;

} while (m < 2);

First, a variable is declared and defined with the integer value 2.

The program hits the do-while construct and starts executing the statements right away: it displays the value of m, which is 2. Then, it increments m (i.e. increases its value by 1). The variable m now holds the value 3. The condition in the while parenthesis is then checked (for the first time) and evaluates to false because 3 < 2 is not true. The loop then terminates right away.

Output:
The value of m is 2

So the do-while loop executes the statements at least once, even if the condition within the while parenthesis is false right from the beginning.

In general, while loops are often used when you do not exactly know how many repetitions you have to make in order to accomplish a task. Loops that you know exactly how many times to iterate are often implemented using a for loop.

Learning the C++ programming language? How about doing it with One Hour a Day.

No comments:

Post a Comment