Advertisement



< Prev
Next >



C++ Do While Loop





Another looping structure of C++ that helps in decision-making is do-while loop, containing a block of statements which always comes before the condition of do-while loop is tested. This is the only looping construct which allows us to execute its associated block of statements(atleast once), irresepective of the result of its boolean condition. Let's us see the working of do-while loop.



Working of do-while loop




Note :


The body of do-while loop is always executed in its first run, even when the condition of do-while loop is false.





Advertisement




do while loop example


In the upcoming example, we are going to use do-while loop to print a series of numbers, starting from 0 to 5.

// 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 -





< Prev
Next >
< For Loop
While Loop>



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement