Thursday, 25 August 2016

The do-while Loop


It is an exit controlled loop. It means that it evaluated its test expression after executing the loop body because its test expression lies at bottom of the loop. It runs atleast once whether the condition is true or false. Other loops(for and while), first checks the test expression then executes accordingly.



Syntax: do
              {
                   statement;
              }
              while(test_expression);   //Put semicolon after while statement


Program: 


#include<iostream.h>
#include<conio.h>
void main ()
{
clrscr();  // For clear screen
int num=1;
do {
           cout<<num<<"\n";
           num++;
      }
     while(num<10);
getch();
}


Output: 

1
2
3
4
5
6
7
8
9
The do while loop will run as long as 'num' is smaller than 10.
You can ask questions if you have doubts regarding C++.

No comments:

Post a Comment