egsoftweb@gmail.com
 facebook.com/egsoftweb

while Loop

 c tutorials |  Admin

Syntax:

       while (condition) statement;

 

where statement is either an empty statement, a single statement, or a block of statements.

 

Examples:

// printing in while loop

#include <stdio.h>

int main()

{

      int i = 0;

      while (i < 10)

      {

            printf("i = %d\n", i);

            i++;

      }

      getchar();

      return 0;

}

 

// reverse direction with while

#include <stdio.h>

int main()

{

      int i = 10;

      while (i > 0)

      {

            printf("i = %d\n", i);

            i--;

      }

      getchar();

      return 0;

}

 

//infinite while loop

#include <stdio.h>

int main()

{

      while(1) printf("this is an infinite loop\n");

      // it will never return here

      return 0;

}