/* */ Click to Join Live Class with Shankar sir Call 9798158723

Do-While Loop in C++


Do-While loop is similar to the while loop.when a set of statements have to be repeatedly executed at least once untile a certain condition is reached. When we do not know exactly how many time a set of statement have to be repeated do while can be used.


Syntex :-

do {
// Statements
}while(Boolean_expression);

How Do-While Loop Works in C++.

  • step 1:-
    The initialization statement is executed first.
  • step 2:-
    Then, the test expression is evaluated. If the test expression is evaluated to false, then the while loop gets terminated and the control comes out of the while loop.
  • step 3:-
    if the test expression is evaluated to true,The statements defined inside the while loop will repeatedly execute until the given condition fails.
  • step 4:-
    After successful execution of statements inside the body of the while loop, the update expression is executed.
  • step 5:-
    Again the test expression is evaluated.


Q.) Example of do-while loop in C++ .
    #include < iostream>
using namespace std;
int main() {
	// Local variable 
	int y = 1;
	do {
		cout << "y is: " << y << endl;
		y = y + 1;
	} while (y < 5);
	return 0;
}
Output
y is 1
y is2
y is 3
y is 4