O'Reilly logo

C Primer Plus, Fourth Edition by Stephen Prata

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.11.

Listing 6.11. The sweetie2.c Program
// sweetie2.c -- a counting loop using for
#include <stdio.h>
int main(void)
{
  const int NUMBER = 22;
  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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required