Thursday, March 3, 2011

Do - while loop in cplusplus , using Do-while in c++

A do-while loop is a loop which runs at least once , because it first runs a chunk of code and at the end checks the condition so of the code wasn't true , even then it runs once , here is a simple use of do-while loop in cplusplus (c++).

#include"iostream.h" // input output stream
#include"conio.h" // console input output

void main()
{
int n;


char c;

do
{
cout<<"\n\nEnter a number"; // cout is used to print something on screen
cin>>n;
cout<<"You entered :: "<cout<<"\n\nDo you wanna enter number again ? press 1 for yes and 2 for no";
c=getch();
}
while(c=='1');


getch();
}


In the above program , user enters a number and the program prints the entered value on screen , then the program asks whether user want to enter more values or not , if user enters 1 , he is again asked to enter a new value else loop exists...