Saturday, 28 February 2015

do while loop in C++

do while loop in C++ do while loop in C++ is also use for repletion but in do while loop the statement is executed at least o... thumbnail 1 summary

do while loop in C++



do while loop in C++ is also use for repletion but in do while loop the statement is executed at least once if the condition is true or wrong
Syntax
do
{
Statement or group of statement
Increment or decrement
}
while(condition);


Example
int a=3;
do
{
cout<<"Pakistan";
a=a+1;
}
while(a<2);
Output: Pakistan

In this example the condition is wrong but it still print the statement use in the while loop body. In the do while loop the condition is define at the end of the loop so there for the loop first executed the statement used in body of while loop and then its check the condition. There for the loop excited the statements at least one time.



Example 1
#include<iostream.h>
int main()
{
int a=1;
do
{
cout<<"Pakistan";
a=a+1;
}
while(a<5);
system("pause");
return 0;
};
Output: pakistan pakistan pakistan pakistan pakistan

 Example 2
 
#include<iostream.h>
int main()
{
int a=4;
do
{
cout<<"Pakistan";
a=a+1;
}
while(a<2);
system("pause");
return 0;
};
Output :Pakistan
 In this example the loop condition is wrong but the statement will executed and the condition will then checked.
Example 3
 
#include<iostream.h>
int main()
{
int a=0;
do
{
cout<<a;
a=a+1;
}
while(a<10);
system("pause");
return 0;
};
Output: 0 1 2 3 4 5 6 7 8 9 10

 Int the above example the 10 also will display in the output of program.