For Loop
2002-01-05 19:07:03
Category: c:general
Description: Example of a for(;;) loop
Author: mind
Viewed: 2136
Rating: (1 votes)


/* for.c by mindsport [mind@infract.net]
 *
 * example on a for() loop
 *
 * 01/05/02
 *
 */
 
 
#include <stdio.h>
 
int main() {
  int count; /* declare count as an integer */
  int max = 10; /* declare max as an integer of 10 */
  char msg[]="Hello!\n"; /* define msg as a string with text Hello! */
 
  for(count = 0; count < max; count++) /* loop until count reaches 10 */
    fprintf(stderr, "%i: %s", count, msg); /* foreach incriment print 'msg' 10 times */
 
  /* note ++ is used to increase an integers size, -- is used to decrease an
   * integers size.. for a more realistic count change 0 to 1 */
 
  /* you could also use for(;;) as an endless loop */
 
  /* as you may have noticed I didn't add a opening { or closing } bracket
   * in my for() statement, Because I only have one expression following it
   * but if you have more than one expression following your for() statement
   * add an opening and closing bracket to your for() statement:
   *
   * for(count = 0; count < max; count++) {
   *   fprintf(stderr, "%i: %s", count, msg);
   *   fprintf(stderr, "Line number: %i\n", count);
   * }
   *
   */
}