Send Close Add comments: (status displays here)
Got it!  This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
For loops


1. For loops
There are many times when one is doing simple counting loops of n items, such as the following. In such cases, it can be convenient to use a for loop. Otherwise, it is often better to use a while loop.

2. Usefulness
The while loop always works. The for loop is often more complicated, hard to understand, and has some subtle semantics (effects) that vary from language to language (if that language even has a for loop).

3. While and for loop form
Here are the general form for the C while loop.
condition-init while ( condition-check ) {    statements    condition-change    }

If the while loop meets the above pattern, one can use the following for loop.
for (condition-init; condition-check ; condition-change) {    statements    }


4. Example
Here is an example while loop, with color coding, following later by some actual program examples.
int n1 = 8; int i1 = 0; while ( i1 != n1 ) {    printf("%d\n", i1);    i1 += 1;    }

Here is the corresponding for loop.
for (int i1 = 0; i1 != n1 ; i1+= 1) {    printf("%d\n", i1);    }


5. Convention
Again, here is the corresponding for loop.
for (int i1 = 0; i1 != n1 ; i1+= 1) {    printf("%d\n", i1);    }

Here is how it is often written.
for (int i1 = 0; i1 < n1 ; i1++) {    printf("%d\n", i1);    }

Note:

6. For to while loop
Again, here is the for loop (second version).
for (int i1 = 0; i1 < n1 ; i1++) {    printf("%d\n", i1);    }

Here is the corresponding while loop.
int n1 = 8; int i1 = 0; while ( i1 < n1 ) {    printf("%d\n", i1);    i1++;    }


7. Reverse transformation
One can also reverse the process to covert a for loop statement into the corresponding while loop. If the for loop is not a simple for loop, then it can take a lot of mental effort to determine what the for loop is actually doing.

8. Break and continue
C has the following statements which can only be used inside a loop Both statements disrupt the normal flow of execution and should not be used in a beginning programming course.

9. Beginning programming
In a beginning course, one should learn to write code without disrupting the normal flow of execution. Once one learns how to do this well, one can later add the break and/or continue statements.

10. For loop semantics

for (condition-init; condition-check ; condition-change) {    statements    }

This behavior can change between languages. Such issues do not arise in while loops. On for loops: In formal program verification, for loops are converted to their equivalent while loops. While loops (and do while) always work and have a clear and consistent semantics which does not change. For loops are more complicated in that they have subtle semantics that can change between languages.

11. Advice
I recommend (to students and others) that one should only use a for loop for 0 to n-1 or 1 to n, unless you are an expert and know what you are doing.

Iterators follow this form, that is, iterate over everything in the collection.

12. Programming language design
A good programming design principle (used by, for example Python, Lua, etc.) is that there should (preferably) be one and only one way to do something.

The C language has many ways to do the same thing, which in one of the things that makes the C language hard to program.

13. Actual programs
Below are some actual programs that use for loops. To see the similar while loops, see Pseudo-code and loops .

14. Count up using for loop
Here is a loop that counts from 0 to 3 using a for loop.

Here is the C code.


15. Output
This loop counts from 0 to n1-1.

Here is the output of the C code.


16. Count up loop variation
Here is a minor variation of the above loop code. What are the differences? Check the output too.

Here is the C code.


17. Output
This loop counts from 1 to n1.

Here is the output of the C code.

Study the differences in the code.

18. Count down using for loop
Here is a loop that counts down from 4 to 1 using a for loop.

Here is the C code.


19. Output
This loop counts down from 4 to 1.

Here is the output of the C code.


20. Count down loop variation
Here is a loop that counts down from 3 to 0 using a for loop.

Here is the C code.


21. Output
This loop counts down from 3 to 0.

Here is the output of the C code.

Study the differences in the code.

22. End of page

23. Multiple choice questions for this page