THE WHILE LOOP
It is an entry controlled loop. The variable should be initialized before the loop begins as uninitialized variable can be used in an expression. Then variable is updated inside the body of while Loop.
Syntax: while(expression)
{
loop body
}
Program:
#include<iosteam.h>
#include<conio.h>
void main()
{
int num=1;
while(num<10)
{
cout<<num<<" ";
num++;
}
getch();
}
Output: 1 2 3 4 5 6 7 8 9
The loop will run as long as the condtion is true i.e. as along as num<10. When it becomes false, the loop will terminate.
No comments:
Post a Comment