The for Loop

The for loop gathers all three actions into one place. By using a for loop, you can replace the preceding program with the one shown in Listing 6.9.

Listing 6.9 The sweetie2.c program.
/* sweetie2.c -- a counting loop using for */
#include <stdio.h>
#define NUMBER 22
int main(void)
{
  int count;
  for (count = 1; count <= NUMBER; count++)
       printf("Be my Valentine!\n");
  return 0;
}

The parentheses following the keyword for contain three expressions separated by two semicolons. The first expression is the initialization. It is done just once, when the for loop first starts. The second expression is the test condition; it is evaluated before each potential execution of a loop. When the expression is false (when count is greater than ...

Get C Primer Plus®, Third Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.