How does a do-While loop execute?

  1. Control falls into the do-while loop.
  2. The statements inside the body of the loop get executed.
  3. Updation takes place.
  4. The flow jumps to Condition
  5. Condition is tested. 
    • If the Condition yields true, go to Step 6.
    • If the Condition yields false, the flow goes outside the loop
  6. The flow goes back to Step 2.
  7. The do-while loop has been ended and flow has gone outside the loop.

C++ Do/While Loop

Loops come into use when we need to repeatedly execute a block of statements. Like while the do-while loop execution is also terminated on the basis of a test condition. The main difference between a do-while loop and a while loop is in the do-while loop the condition is tested at the end of the loop body, i.e do-while loop is exit controlled whereas the other two loops are entry-controlled loops. 

Note: In the do-while loop, the loop body will execute at least once irrespective of the test condition. 

Syntax:

do
{
   // loop body

   update_expression;
} 
while (test_expression);

Note: Notice the semi – colon(“;”) in the end of loop.

The various parts of the do-while loop are: 

  1. Test Expression: In this expression, we have to test the condition. If the condition evaluates to true then we will execute the body of the loop and go to the update expression. Otherwise, we will exit from the while loop.  
  2. Update Expression: After executing the loop body, this expression increments/decrements the loop variable by some value. 
  3. Body: It is the Collection of statements i.e, variables and functions, etc. The condition is not satisfied until the condition is executed automatically after a successful iteration. do-while loop, code can be used to print simple names, execute complex algorithms, or perform functional operations.

Similar Reads

How does a do-While loop execute?

Control falls into the do-while loop. The statements inside the body of the loop get executed. Updation takes place. The flow jumps to Condition Condition is tested.  If the Condition yields true, go to Step 6. If the Condition yields false, the flow goes outside the loop The flow goes back to Step 2. The do-while loop has been ended and flow has gone outside the loop....

Flow Diagram of do-while loop

...