Thursday, 7 November 2013

Do While Loop

Practise Programe of  Do While Loop.
 
# include<iostream>
using namespace std;
void main()
{
int a,b;
char ch;
do
{
cout<<"Enter a";
cin>>a;
cout<<"Enter b";
cin>>b;
a=a+b;
cout<<"output is"<<a<<endl;
cout<<"Are you want to continue"<<endl;
cin>>ch;
}
while(ch=='Y'||ch=='y');
}

The basic difference b/w while and do while loop is,do while loop enters into its body first time if its condition is wrong.........!
Syntax:-
"do",will come at the start.....!
"do" means,Commands in loop body will be executed whatever the condition is(either it is right or wrong).
and while at the end.......!
so,the program first ask user to enter two numbers and then display the result.......!
Then,after it will ask whether you want to continue the program or not?
if you enter Y or small y then it will again ask you to enter two numbers and so on.....!
but if you enter any other character(rather then Y or y),then the program stops(mean loop end)...!
Note that,in do while loop semi colon is place after while......!

0 comments: