while


while Statement

while is simplest of all looping structures. while is an entry controlled loop. Here, first the condition is evaluated and if the condition is true then the statement block will be executed as many times until the condition becomes false.

Syntax:
while(test condition)
{
  body of the loop;
}
            statement-x;
The body of the loop must contain some increments or updates on loop-control variable. After the execution of the body of the loop, the test condition is once again evaluated and if it is true, the body is executed once again. This process is repeated until the test condition becomes false and the control is transferred out of the loop. On exit, the program continues with the statements immediately after the body of the loop. The body of the loop may have one or more statements. The braces are needed only if the body contained two are more statements.


Sample Program for printing numbers from 1 to 100

void main()
{
 int i;
 i=1;
 while(i<=100)
 {
  printf("\t%d",i);
  i++;
 }
}     // here i s known as loop-control variable.

No comments:

Post a Comment