< Prev
Next >



Do-While Loop in C++





Another looping structure of C++ that helps in decision making is do-while loop, which contains a block of statements or the body of loop always comes before the condition of do-while loop is tested.







do while loop example


// C++ do-while example 
 
#include<stdio.h>

using namespace std;
int main()  // main method starts 
{
int i=0;

do 
{
	
	cout<< "i = " << i <<"\n";
	i++;

}while(i<6);

return 0;
}


Output


i = 0
i = 1
i = 2
i = 3
i = 4
i = 5





do while loop is executed at least once


The body of do while loop is always executed during its first execution, whether the condition of loop is evaluated to be false or true, take a look at our next example -
// do-while second example 
 
#include<iostream>

using namespace std;
int main()  // main method starts 
{
int i=10;

do 
{
	
	cout<< "i = " << i <&;t;"\n";
	i++;

}while(i<1);

return 0;
} // main method ends and so does the program 


Output


i = 10




Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter




< Prev
Next >
< For Loop
Do While Loop>
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
Python

Ad2