Example Using the While Statement

In the following program, item[index] triples each time the value of the expression ++index is less than MAX_INDEX. When ++index evaluates to MAX_INDEX, the while statement ends.

#define MAX_INDEX  (sizeof(item) / sizeof(item[0]))
#include <stdio.h>
 
int main(void)
{
   static int item[ ] = { 12, 55, 62, 85, 102 };
   int index = 0;
 
   while (index < MAX_INDEX)
   {
      item[index] *= 3;
      printf("item[%d] = %d\n", index, item[index]);
      ++index;
   }
 
   return(0);
}


while Statement