C++ do while Loop for beginners

1 0
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
Happy
Happy
100 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

One thought on “C++ do while Loop for beginners

Comments are closed.