for Statement

When a loop is being used for part of a function, it happens very often that this loop uses some loop variable which is first set to an initial value and then tests that the current loop variable is within range. If it is within range it executes the statements in the loop, then adjusts the value of the loop variable, and then goes to the top of the loop to test again. If or when the loop variable is not in range, then the loop statements are skipped and control continues beyond the loop.

As a simple example, let us say we wanted to print out a list of the even numbers between 0 and 99. This could be done without a loop, but it is not practical (especially for larger ranges).

This could be done by the following loop:

	n=0;
	if (n<100) {
		printf("%d\n\",n);
		n+=2;
	}

Our print example could then be expressed with a "for" statement as:

	for(n=0;n<100;n+=2)
		printf("%d\n\",n);

This structure, and more general forms of it, occurs so often, that it has been given a name, called: the "for" statement or "for" loop. The general form of which is:

	for (e1; e2; e3)
        s;

which in words is - execute expression statement 'e1' once, then test expression 'e2'; if 'e2' is true, do statement 's' and then do expression statement 'e3' and loop to the testing of 'e2'.

This can be written in an equivalent form using a "while" statement as the following:

	e1;
	while (e2) {
		s;
		e3;
	}

Which itself has the following equivalent using an "if" and "goto":

	e1;
	loop_top:
	if (e2) {
		s;
		e3;
		goto loop_top;
	}

There is no restriction at all on the nature of 'e1', 'e2' or 'e3'; they do not even have to be related in the sense of involving the same loop variable.

The structure of the "for" statement has the great advantage of clearly identifying which statement or statements are in the loop. Also, it puts in one place: what the initial condition is, what the loop test is, and what the loop variable step or adjustment is.

A more complex example of a "for" loop which prints out the 8 bits of a variable 'c' of type "char", msb first, is the following:

	char c; int n;
	...
	for(n=1<<7;n>0;n>>=1)
		putchar((c&n)?'1':'0');

The flow chart for the general "for" statement is:

         |
         v
      +----+
      | e1 |
      +----+
         |
         v
         +--<------------------------\
         v                           |
         ^                           |
        / \  T   +---+    +----+     |
       < e2>-->--| s |-->-| e3 |-->--/
        \ /      +---+    +----+
         v
         | F
         v
         |

Note that exactly two semicolons, ';', must always appear within the parenthesis for every "for" statement, even if one or two or all of the three expressions, 'e1', 'e2', or 'e3', are null. For 'e1' and 'e3', a null expression is precisely that - nothing is done. When 'e1' and 'e3' are null, the "for" loop degenerates into a plain "while" loop:

	for(;e;) s;

is the same as:

	while(e) s;

If 'e2' is left blank or null, then the compiler will assume not null (which is meaningless and an error) inside the conditional expression, but will take the constant '1' - identically true. Although this is a kind of 'C' idiom, it is suggested that you explicitly show 'e2' as '1':

	for(e1;1;e3) s;

© 1991-2008 Prem Sobel. All Rights Reserved.