while loop some some examples( coding and output),some while loop code to display a series and use of brackets
//use header files syntax according to the compiler
case 1 :
#include<iostream.h>
#include<conio.h>
void main()
{
int i=1;
while(i<=10)
{
cout<<i<<" ";
i++;
}
}
// In the above code loop executes 10 times ie. i=1 to i=10
hence the output :
1 2 3 4 5 6 7 8 9 10
after every number one space is printed as mentioned in the code.
case2:
#include<iostream.h>
#include<conio.h>
void main()
{
int i=1;
while(i<=3)
{
cout<<"hello Anand ";
i++;
}
getch();
}//
In the above code loop will execute 3 times and the word "hello Anand" will be displayed 3 times but
with one space or in a single line as .
Output:
hello Anand hello Anand hello Anand
case 3:
#include<iostream.h>
#include<conio.h>
void main()
{ int i=3;
while(i>0)
{
cout<<"hello";cout<<"anand";
cout<<endl;
i--;
getch();
}
}
//
In the above code loop will execute 3 times and both the words "hello"
and "anand" will be displayed. After helloanand there is a new line so output
will be:
helloanand
helloanand
helloanand
case 4:
#include<iostream.h>
#include<conio.h>
void main()
{
unsigned int fact=1;
int num;
cout<<"enter a number ";
cin>>num;
while(i<=num)
{
fact=fact*i;
i++;
}
cout<<fact;
getch();
}
Read:do while loop coming soon.