C++ do...while loop Statement
A
do...while loop is similar to a
while loop, except that a
do...while loop executes statement within do block first and then check the condition of while. It guaranteed to execute at least one time of statement.
In do while loop, while statement (loop) ends with a semicolon (;).
Syntax
do{
statement(s);
}
while(condition);
Working Process of do...while loop
- The statement inside the do body of the loop is executed.
- Then the condition expression is checked.
- If the condition expression is true, then do body of the loop is executed. This process continues until the condition expression becomes false.
- When the condition expression is false, do...while loop is terminated.
C++ do...while loop Example
This program print counting from 1 to 10.
#include <iostream>
using namespace std;
int main()
{
int i=1;
do{
cout<<i<<"\n";
i++;
}
while (i<=10);
return 0;
}
Output