C++ do while loop

do while loop is post-test loop that execute statement or set of statements at least one time either condition is false. do while loop has much similar structure to while loop. It is also used to implement iterative structure.

Syntax:

Initialization ;

do {

    statements ;

    increament / decrement ;

} while ( condition ) ;

To understand  Initialization, increament / decrement, condition and statements read while statement.

Example:

#include <iostream>
using namespace std;

int main(int argc, char** argv) {
	// initialization.
	int a = 1;
	
	// do while loop statement.
	do {
		
		// output statement.
		cout<<"IMS\n";
		
		// increment.
		a = a + 1; // a++ can also be used.
		
	} while ( a <= 5 ); // semicolon is used after condition.
	return 0;
}

Output:

output of do while loop in C++Difference between while and do while loop:

Sr. while loop do while loop
1 It does not start execution until given condition true. It execute instructions at least once even condiotn is false.
2 Semi colon is not placed after condition. Semi colon is placed after condition.
3 It is used where we do not want to execute statements in advance. It is used where we want to execute statements in advance.

 

Comments
Login to TRACK of Comments.