21


The for statement

The C for statement has the following form:

for (expression1; expression2; expression3)

expression1 initializes; expression2 is the terminate test; expression3 is the modifier (which may be more than just simple increment);

NOTE: C basically treats for statements as while type loops

For example:

int x;

main()

...outputs the three lines:

x=3

x=2

x=1

...to the screen

All the following are legal for statements in C. The practical application of such statements is not important here, we are just trying to illustrate peculiar features of C for that may be useful:

for (x=0; ((x>3) && (x<9)); x++)

for (x=0, y=4; (x>3) && (y<9); x++, y+=2)

for (x=0, y=4, z=4000; z; z/=10)

The second example shows that multiple expressions can be separated by a ,.

In the third example the loop will continue to iterate until z becomes 0;


drago@scri.fsu.edu
Jan. 1997