In some situations we need to execute the body (statements) of the loop before testing the expression (condition). In those cases we need do-while loop.
Do-while loop can be presented in the following way
do statement while (expression);
OR
do { statement }while (expression);
It’s similar to the
while loop. The main difference is that the expression is checked after the statement is executed:
//condition is false but we can enter the do block do { cout << "I am inside even the expression is false" << endl; }while (false);
The output of above code will be
I am inside even the expression is false
The sequence of actions in do .. while loop is as follows:
Step1: Perform block of instructions.
Step2: Check condition. If condition is true go to 1. Else Stop.