While Loop
2002-01-05 19:10:38
Category: c:general
Description: Example of a while loop
Author: mind
Viewed: 6064
Rating: (20 votes)


/* while.c by mindsport [mind@infract.net]
 *
 * example on a while() loop
 *
 * 01/05/02
 *
 */
 
 
#include <stdio.h>
 
int main() {
  int count = 0; /* declare count as an integer of 0 */
  int max = 10; /* declare max as an integer of 10 */
  char msg[]="Test!!\n"; /* define msg as a string with text 'test' */
 
  while(count < max) { /* loop the below statement until count = or > max */
    fprintf(stderr, "%i: %s", count, msg); /* foreach loop print the results */
    count++; /* increase count by 1 */
  }
 
  /* ++ is used to increase an integers size, -- is used to decrease an
   * integers size.. for a more realistic count change 0 to 1 */
 
  /* for an endless while statement use while(1) { }
   *
   * while(1) {
   *   fprintf(stderr, "test-> ");
   * }
   *
   * exit() to break a while statement */
}