Read Time:47 Second
The do-while loop is a variant of the while loop. This loop will always execute a block of code at least once, and then it will repeat the loop as long as the specified condition is true. This loop will always be executed at least once, even if the condition is false, because the code is executed before the condition is tested. As the condition is checked at the end of the loop, it is also called exit-controlled loop.
Syntax
do { //code to be executed } while (condition);
Example:
See the program below.
#include <iostream> using namespace std; int main() { int i = 1; //do-while loop do { cout << "The number is " << i << endl; i = i + 1; } while (i <= 5); return 0; };
Output:
The number is 1 The number is 2 The number is 3 The number is 4 The number is 5
[…] do-while loop – a variant of the while loop which will always be executed at least once, even if the condition is false, because the code is executed before the condition is tested. […]