Monday, August 23, 2010

C while loop

The while loop creates a loop the repeats until the test expression becomes false or zero. It is an entry-condition loop, which means that the decision to go through one pass of the loop is made before the loop is traversed. Thus it is possible that the loop is never traversed. The statement part of the form can be as simple statement or a compound statement.

Form

while (expression)
statement

The statement portion is repeated until the expression becomes false or zero.

examples

1. while (n++ <100)
     printf(" %d %d\n", n, 2*n+1);

2. while(fargo<1000)
{
 fargo = fargo + step;
 step = 2* step;
}

3 sample program


#include< stdio.h >
#define NUMBER 22
main()
{
int count = 1;                                   // initialization

while(count < = NUMBER)            // test
     {
      printf("Be my Valentine!\n"); // action
      count++;                                    // increment count
     }
}

0 comments: