Difference Between for while and do while Loop in C++

Working of for loop

  1.  Firstly, the initialization expression(which mainly contains control variables) will be executed in the for loop only once at the beginning of the for loop.
  2. Then test expression will be evaluated.
  3. If the test expression results a false value, the loop gets terminated.
  4. If the test expression results a true value, then the body of the loop will be executed.
  5. Then update expression updates the control variables value and the test expression will be evaluated again.
  6. If test expression results a true value, repeat step 4. otherwise step 3.

Flow Chart of for Loop :

flow chart of for Loop (Difference Between for while and do while Loop in C++)

Working of while loop

  1. Firstly, the initialization expression(which contains control variables) must be initialized before the loop begins because the loop control variable is used in test expression.
  2. Then the while loop containing the test expression will be evaluated.
  3. If the test expression results a false value, the loop gets terminated.
  4. If the test expression results a true value, then the body of the while loop will be executed.
  5. The update expression should be updated inside the body of the while loop. It updates the control variables value and after reaching the end of while(), the test expression will be evaluated again.
  6. If test expression results a true value, repeat step 4. otherwise step 3.

Flow Chart of while Loop :

flow chart of while Loop (Difference Between for while and do while Loop in C++)

Working of do while loop

  1. The do while  loop is different from the other two loops but it is also a very simple loop. In do while loop, body of the loop is placed before the control statement of the loop.
  2. Now, initially, the loop body will be executed unconditionally for the first time in the block code of do {..}. (The body of the do while loop is in the do {..} block)
  3. The initialization expression(which contains control variables) must be initialized before the beginning of do {..} block code.
  4. After executing the body of the loop, the test expression will be evaluated.So in do-while loop, the loop body will execute for at least one time.
  5. If the test expression results a false value, the loop gets terminated.
  6. If the test expression results a true value, then the body of the do while loop (do {..} block code) will be executed again.
  7. The update expression should be updated inside the body of the do while loop (do {..} block code). It updates the control variables value and after reaching the end of do {..} code, the test expression will be evaluated again.
  8. If test expression results a true value, repeat step 6. otherwise step 5.

Flow Chart of do while Loop :

flow chart of do while Loop (Difference Between for while and do while Loop in C++)

Difference Between for while and do while Loop in C++

Basis of Difference For Loop While Loop Do While Loop
Where to Use for Loop, while Loop and do while Loop The for loop is appropriate
when we know in advance
how many times the loop
will be executed.
The other two loops i.e. while and do while loops are more suitable in the situations where it is not known before hand when the loop will terminate.
In case if the test condition fails at the beginning, and you may not want to execute the body of the loop even once if it fails, then the while loop should be preferred. In case if the test condition fails at the beginning, and you may want to execute the body of the loop atleast once even in the failed condition, then the do while loop should be preferred.
How all the three loops works? A for loop initially initiates a counter variable (initialization-expression), then it checks the
test-expression, and executes the body of the loop if the test expression is true. After executing the body of the loop,
the update-expression is executed which updates the value of counter variable.
A while loop will always evaluate the test-expression initially. It the test-expression becomes true, then the body of the loop will be executed. The update expression should be updated inside the body of the while. However, the counter variable is initialized outside the body of the loop. A do while loop will always executed the code in the do {} i.e. body of the loop block first and then evaluates the condition. In this case also, the counter variable is initialized outside the body of the loop.
Position of the statements :

  • Initialization
  • test-expression
  • update-expression
In for loop, all the three statements are placed in one position In while and do while loop, they are placed in different position.
Syntax of Loops
for (
   initialization-exp.(s);
   test-expression(s); 
   update-expression(s)
    )
      {
       body-of-the-loop ;
      }

 

while (test-expression)
  {
   body-of-the-loop;
   update-expression(s); 
  }
do  {
     body-of-the-loop;
     update-expression(s);
    }   
while (test-expression);
Which one is Entry Controlled Loop
and
Which one is Exit Controlled Loop ?
Both loops i.e. for loop and while loop are entry controlled loop, means condition is checked first and if the condition is true then the body of the loop will executes. do while loop is an exit controlled loop, means means that condition is placed after the body of the loop and is evaluated before exiting from the loop.
Flow Chart Flow chart of for Loop – Click Flow chart of while Loop Flow chart of do while Loop
Conversion of one Loop to another Loop
or
Example : Print numbers from 1 to 10 using all the three loops.
   :
   :
for (int i=1; i<=10; i++)
  {
   cout<<"i="<<i<<"\n";
  }
int i = 1;
   : 
   :
while (i<=10)
  {
   cout<<"i="<<i<<"\n";
   ++i
  }
int i = 1;
   : 
   :
do
  {
   cout<<"i="<<i<<"\n";
   ++i;
  }
while (i<=10)
     

Incoming search terms:

  • Difference between while and do while
  • difference between while and do while loop in c pdf
  • difference between while and do while loop in tabular form
  • difference between while and do while loop in c in tabular form
  • difference between while and do while loop in c
  • difference between while and do while loop
  • difference between while and do while loop in c language
  • difference between while loop and do while loop
  • difference between while and do while in C
  • difference between for while and do while loop

Leave a Reply