The do-while loop is similar to a while loop. The only difference is that the condition occurs at the end of the loop. Having the loop condition at the end of the loop gives the guarantees that the body of the loop always executes at least once.
Test condition:
The test condition must be enclosed in parentheses and followed by a semicolon. Semicolons are also placed at the end of each statement within the blocks. The body of the loop is enclosed in curly braces to indicate the body of the loop. This method also helps to increase the readability of code.
Exit condition loop:
The do-while loop is an exit-condition loop that means the body of the loop will always be executed first. After that, the test condition is evaluated. If the test condition is true the body of the loop is executed. If the loop condition is false, the loop terminates and the program execution continues with the statement following while.
Example:
char ans;
do
{
cout<< "Do you want to continue (Y/N)?\n";
cout<< "You must type a 'Y' or an 'N'.\n";
cin >> ans;
}
while((ans !='Y')&&(ans !='N')&&(ans !='y')&&(ans !='n'));