Introduction to 'For' Loops in C

Syntax and Usage of For Construct in ANSI C99

© Dario Borghino

Sep 23, 2008
The for loop is, in ANSI C like in other programming languages, a very important flow control mechanism which novice programmers usually have trouble understanding.

Many novice C programmers seem to have trouble understanding the side effects originating from a basic flow control mechanism such as the 'for' cycle. This article is an attempt at clarifying these concepts once and for all.

For Loop Syntax in C-Like Programming Languages

In all C-like programming languages, the for construct is composed of three parts, each divided by a semicolon:

for(initialization; test; update) { ... }
  • In the first section, the initialization, we initialize an index variable (which is usually being named i, j or k) to its initial value;
  • In the second part, we test whether a variable (usually the same we have just initialized in the first part) satisfies a certain condition: if it does, we enter the loop one more time, otherwise we exit from it;
  • In the third and last part, we update the variable — usually by incrementing or decrementing it by 1.

How the For Loop Works

Knowing the syntax of the for loop is not enough: in fact, what is usually the major source of confusion among novice programmers is not the syntax itself, but rather the runtime behavior of the construct.

for(i = 0; i < 10; i++) { printf("%d ", i); }

The snippet above is a very basic example of such a loop. Let's see what happens when we run this code, assuming that the block enclosed within curly brackets doesn't otherwise modify the value of the index i:

  1. The variable i is initialized to 0;
  2. The test "i < 10" is performed, and the code enclosed in curly brackets is executed if the test is successful, exiting the loop otherwise;
  3. The index i is incremented (i++);
  4. The points 2-3 are repeated until the test "i < 10" fails and we exit the loop.

As a result, as it can be easily verified, the output would be:

0 1 2 3 4 5 6 7 8 9

Notice that the number 10 is not being printed, because i is first incremented from 9 to 10, and then tested. When we exit the loop, we do so because the condition i < 10 is no more true, since i is now equal to 10.

Special For Loops: While Loops and Infinite Loops

The peculiar syntax of the for loop can be adapted to a wide variety of situations, and can even used to behave in the exact same fashion as a while() loop. For instance, consider what happens when we write:

for(;i < 10;) { ... }

This somewhat cryptic code is equivalent to while(i < 10) { ... } in that, since the first and last part are absent, we simply test for i < 10 without initializing nor updating the variable. For this reason, we say that the while() loop is nothing but a special case of the for loop.

When using the for construct, novice programmers must always be careful to choose a condition that is guaranteed to terminate the loop at some point in time: the risk is of incurring in a so-called infinite loop. Consider for instance this piece of code:

for(;;);

which is a clear programming error that will loop indefinitely and consume all the available processor time until it will be manually terminated by the user.


The copyright of the article Introduction to 'For' Loops in C in C Programming is owned by Dario Borghino. Permission to republish Introduction to 'For' Loops in C in print or online must be granted by the author in writing.




Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo