The for Statement

As in QuickBasic or QuickPascal, the for statement in C is often used to repeat
a statement a set number of times. Let's begin with a simple example. The
FORLOOP.C program, shown below, uses for to repeat a printf statement
five times.

/* FORLOOP.C: Demonstrate for loop. */

#include <stdio.h>

main()

{

int test;

for( test = 10; test > 0; test = test - 2 )

printf( "test = %d\n", test );

}

FORLOOP.C gives the same output as WHILE.C and DO.C:

test = 10

test = 8

test = 6

test = 4

test = 2

Figure 3.3 shows the parts of the for loop in FORLOOP.C.

A for statement is more complex than a while or do statement. The part in parentheses can contain three expressions separated by semicolons:

An “initializing expression” that often initializes a loop counter

A “test expression” that states how long the loop continues

A “modifying expression” that often modifies a loop counter

Like the test expression in a while statement, the test expression in a for statement causes the loop to continue as long as the test expression evaluates as true.

All of the expressions in the parentheses of a for statement are optional. If you omit the test expression (the second one), the statement repeats indefinitely. In the following program, for instance, all of the expressions in the parentheses of the for loop are empty:

main()

{

for( ; ; )

printf( "Hi, Mom!\n" );

}

The loop above repeats indefinitely because it has no test expression that specifies when to end the loop. It has the same effect as the following while loop, whose test expression is always true:

main()

{

while( 1 )

printf( "Hi, Mom!\n" );

}

You can use multiple expressions for either the initializing expression or the modifying expression, as in FORLOOP1.C:

/* FORLOOP1.C: Demonstrate multiple expressions. */

#include <stdio.h>

main()

{

int a, b;

for( a = 256, b = 1; b < 512; a = a / 2, b = b * 2 )

printf( "a = %d b = %d\n", a, b );

}

The output from FORLOOP1.C appears below:

a = 256 b = 1

a = 128 b = 2

a = 64 b = 4

a = 32 b = 8

a = 16 b = 16

a = 8 b = 32

a = 4 b = 64

a = 2 b = 128

a = 1 b = 256

In the FORLOOP1.C program, the initializing expression of the for loop initializes two variables ( a and b ) instead of one. The modifying expression changes the values of the same two variables. Use commas to separate multiple expressions in cases such as this.

Although for and while might seem quite different, they're interchangeable in most cases. The FORLOOP2.C program demonstrates this principle. Both loops, while constructed differently, produce the same effect—printing the numbers from 0 through 9.

/* FORLOOP2.C: Demonstrate similarity of for and while. */

#include <stdio.h>

main()

{

int count;

for( count = 0; count < 10; count = count + 1 )

printf( "count = %d\n", count );

count = 0;

while( count < 10 )

{

printf( "count = %d\n", count );

count = count +1;

}

}

The two loops in FORLOOP2.C function identically. The for loop prints the numbers from 0 through 9:

for( count = 0; count < 10; count = count + 1; )

printf( "count = %d\n", count );

as does the while loop:

count = 0;

while( count < 10 )

{

printf( "count = %d\n", count );

count = count + 1;

}

Most programmers prefer for over while in a case like this, because for groups all the loop-control statements in one place. The for statement is also appropriate when you need to initialize one or more values at the beginning of the loop. The while and do statements are more appropriate for cases in which the value used in the test expression has already been initialized.