While and Do while Loops in C++ 

The other loops are while and do while loop. The while loop is also an entry controlled loop.

Syntax of while Loop :

while (test expression)
          body of the loop

⇒The while loop execute the body of the loop if the test expression result into true, otherwise the control passes to the line after the body of the loop code.
⇒The initialization expression or the loop control variable must be initialized before the loop begins because the loop control variable is used in test expression.
⇒The update expression should be updated inside the body of the while loop.

Flow Chart of while Loop :

While Loop Flow Chart-(While and Do while Loops )

Example of C++ Program to illustrate the working of while loop – 
Program to print the factorial of a number –

#include<iostream.h>
int main()
   {
    int i,num,fact=1;
    cout<<"Enter the number to calculate the factorial = ";
    cin>>num;
    i=num;
    while (num>0)
        {
         fact=fact*num;      //statement-1
         --num;              //statement-2  (update expression)
        }
    cout<<"\n The factorial of "<<i<<" is = "<<fact;
    return 0;
   }
Output :
Enter the number to calculate the factorial = 6
The factorial of 6 is = 720

Program Illustration : 

  • Initialization expression : num
  • Test expression : num>0
  • update expression : –num
  1. num is initialized to a value which is entered by a user to calculate the factorial of num.
  2. Then, the test expression num>0 will be evaluated and results a true value as value of num=6. So it executes the body of the while loop, i.e. statement-1 and statement-2.
  3. statement-2 is an update expression which decreases the value of num by 1.
  4. Then after updating the expression, test expression is again evaluated. This process continues till the test expression gets failed.
  5. The test expression fails at num=0 as (0>0) results false value. So, while loop is terminated.

Variations of while loop –

1. Empty Loop - An empty loop does not contain any statement in its loop 
   body. For example, 
   long wait = 0;
   while( ++wait < 1000) ;      //null statement
   The above loop is a delay loop and is useful for pausing the program.

2. Infinite Loop - A loop without an update expression is said to be 
   infinite loop. So, a while loop can be infinite if there is no update 
   expression inside its body. For example, 
   j=0;
   while (j<=n)
       cout<<"\n"<<j*j;
   j++;
   The above loop is an infinite loop as the update expression is not in 
   the body of the loop and therefore, the value of j never updates.So, 
   to avoid such situation, use curly braces {..}.
   j=0;
   while (j<=n)
      { 
       cout<<"\n"<<j*j;
       j++;
      }

Do While Loop –

The do while loop is an exit controlled loop i.e. test expression is evaluated at the bottom of the loop after executing its loop body elements. This means that, the do while loop always executes at least once.

Syntax of Do while loop –

do
  {
   statement;
  }   while (test expression);

The braces are not necessary if there is a single statement.

Flow Chart of do while Loop –

do while loop flowchart(While and Do while Loops )

Example of C++ Program to illustrate the working of do while Loop – 
Program to accept a number and then adds the value of its digits an then prints that value – 

#include<iostream.h>
int main ()
   {
    int rmd,num,sum=0;
    cout<<"Enter the number = ";
    cin>>num;
    do
      {
       rmd = num % 10;        //extract rightmost digit(rmd) (statement-1)
       sum + = rmd;           //sum of digits                (statement-2)
       num / = 10;            //move next digit into rightmost position (statement-3)
      }   while (num>0);
    cout<<"\nThe sum of digits is = " <<sum;
    return 0;
   }
Output :
Enter the number = 721
The sum of digits is = 10

Program Illustration :

Lets see how the result will be 10. The number is 721. There are 3 variables used in the above program.

  • rmd is a variable which is used to extract the rightmost position.
  • num is the variable which is used for inputting a number from user to calculate its digits sum.
  • and sum is a variable which is used to do the addition of digits in num.

After inputting from user, it will enter the do loop, the following table illustrates how the result is 10

while (num>0) rmd = num % 10; sum + = rmd; num / = 10;
Num>0 Value of Rmd Value of sum Value of Num
721 > 0 721 % 10 = 1 0 + 1 = 1 721 / 10 =72
72 > 0 72 % 10 = 2 1 + 2 = 3 72 / 10 = 7
7 > 0 7 % 10 = 7 3 + 7 = 10 7 / 10 = 0
0 > 0 False; The loop gets terminated

So, in the end,

  • Initialization expression : num   //entered by user
  • Test expression :  num>0
  • update expression : num/=10      //as it changes the value of control variable(num)
     

Incoming search terms:

  • Advantage and disadvantage of dowhile and while loop

Leave a Reply