C++ do() while control execution statement

The do() while is another control execution statement in C++.Unlike the ‘if() else’ statement whose decision making is based on each of the expression provided to ‘if() or else if()’ statement the ‘do() while’ statement can iterate a code or program until certain condition is fulfilled.The rule of ‘do() while’ is straightforward,if the expression under ‘while()’ statement returns false the code under do statement stops executing.

Link :C++ while statement

A pictorial representation is given below.


The syntax of ‘do() while’statement is given below.

//code1 :the code to be executed
while( expression1 );  //do not forget ';' after the closing bracket

As long as the expression ‘expression1’ returns true or 1 the code1 will keep on executing.Here is simple code example.

int i=0 ;

  cout<< i << endl ;
  ++i; //i value keeps on increasing
while( i<3 ); 

The output is,


What the above code does is simple,the value of ‘i’ is printed out as long as ‘i’ is smaller than 3.The ‘++’ operator in ‘++i’ is known as increment operator and it’s function is to increase the variable value by 1 and in this case ‘i’.


Another code example is given below.It find the sum of the first 10 natural number.

int i=1 , sum=0 ;

while( i<11);

cout<<"The sum of first " << --i << " natural number is:" << sum ;


The sum of first 10 natural number is:55

Note the ‘do() while’ statement is similar to ‘while()’ statement,in fact most of the time ‘while()’ is used instead of ‘do() while’ statement.

Leave a Reply

Your email address will not be published. Required fields are marked *