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.
There are many times when one is doing simple counting loops of n items, such as the following.
0 to n-1 (by 1)
1 to n (by 1)
n-1 down to 0 (by 1)
n down to 1 (by 1)
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 ) {
statementscondition-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;
}
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
A break statement exits the loop.
A continue statement continues with the next iteration of the 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.
Reasoning about the code is more difficult.
Showing (informal and formal) correctness of the code is more difficult.
Subtle hard-to-trace bugs happen when control flow is disrupted.
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
}
Is the for loop control ending condition evaluated once or every time?
What happens if the for loop variable is changed within the for loop?
Does the for loop control variable have a value or a valid value after the for loop ends?
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.