while and do-while Loops

while Loop

Problem: A program should read in one positive number and then output a value that tells how many times the given number has to be tripled in order to become greater than 100. Note: if the given number is greater than 100 on input, then no triplings are required and the result should be 0.

This problem is quite similar to the previous one. We could use a do while loop and add a counter to count the triplings. However, a do while loop examines the condition at the end of each iteration, while this task requires no iterations if the value on input is greater than 100. Perhaps we could use an if statement to check whether the given number is immediately greater than 100?

There is a simpler solution: a while loop is very similar to a do while loop, except that the condition is examined at the start of each iteration, including the first iteration. This is the program that solves the given task using a while loop:

The statement

num *= 3;

multiplies the value of variable num by 3. It has the same effect as the statement

num=num*3;
#include <iostream>
using namespace std;

int main()
{
    double num=0;
    cout <<"Enter a positive number: ";
    cin >> num;

    double count=0;
    while (num <= 100)
        {
        num *= 3;
        count++;
        }

    cout << "It needs to be tripled " << count << " times." << endl;
}

Notice that this while loop will iterate while num is less than or equal to 100. The output should look like this:

Enter a positive number: 20
It needs to be tripled 2 times.

Or, if we enter a number greater than 100:

Enter a positive number: 999
It needs to be tripled 0 times.
Warning! A while loop should not be terminated by a semicolon. This is akin to a for loop.

Perhaps our program gives a somewhat sparse output. Let us print out the result of each tripling, too. We only need to add the following statement just below the count++ statement:

        cout << "At step " << count << " the number equals " << num 
             << endl;

Here is one possible output:

Enter a positive number: 2.3
At step 1 the number equals 6.9
At step 2 the number equals 20.7
At step 3 the number equals 62.1
At step 4 the number equals 186.3
It needs to be tripled 4 times.

Obviously, all three kinds of loops, for, while and do while, share many similarities in the C++ language. They are so similar to each other that we might ask if we really need all three of them. The answer is that, in theory, we do not.

Still, having all three seems to be quite convenient, because very often one kind of a loop suits the task much better than the other two. That is the reason why the authors of the C++ language included all three kinds of loops, and the other languages are no different in this respect. It is customary and recommended to use a for loop when the number of iterations is given or known in advance. Otherwise, use a while or a do while loop. For example, a while loop should be used when the number of iterations needs to be calculated, as was the case in the last given problem.

Words while and do are keywords in the C++ language.