For Loop
There is one other loop that we need to look at: the for loop.
The for loop is another pre-test loop. It is basically a specialised version of the while loop, and is mainly used to repeat a block of code a set number of times. You can think of it like a counting loop, with it counting from a start value to an end value. The for loop has a control variable that stores the number of times the loop has currently run, which the loop uses to see if it should end.
For
— when, why, and how
The for loop itself is controlled by three aspects: an initialiser, condition, and post-loop increment.
- The initialiser appears first in the parenthesis after the
for
keyword. This is used to set the initial value for the control variable (usuallyi
as we have noted before). If you were using a while loop, this code would appear before the start of the loop. - The condition is the expression used to determine if the body of the loop should run. This would be the same as the condition in an equivalent while loop.
- The post-loop increment updates the control variable. This code runs after the body of the loop. In an equivalent while loop, this would be the increment of the control variable at the end of the body in the while loop.
Any time you have some code that you want to repeat a set number of times, a for loop is most likely what you want to use. It provides you with the mechanics you need to achieve this in a more compact and clear format than a while loop.
In C
The standard format of a for loop is: for (i = 0; i < size; i++) {...}
. This can be read in parts as:
- for
i
, starting at 0 (the initialiser) - while
i
is less thansize
, do the body of the loop (the condition) - then increment
i
(usingi++
) (the post-loop increment)
This will then give you a loop that runs size
times.
The example above (for (i = 0; i < size; i++) {...}
) assumes that the variable i
has already been declared.
Many languages, including C#, allow you to declare loop’s control variable within the loop itself, not just initialise it.
This is the most common way control variables are created, and neatly packages all of the loop’s data within the loop structure.
Using this approach, our previous example would become for (int i = 0; i < size; i++) {...}
.
You can tweak all three elements of a for
loop.
For example, there is no restriction on what value the control variable can be initialised to (assuming it’s a valid value for the variable’s data type).
Similarly, the condition of a for
loop can be any valid expression.
Lastly, the post-loop increment can be any assignment statement.
For example, you can count backwards, or in increments of 2.
It is even possible to skip the condition and increment elements of a for
loop entirely!
This is not done often, but the flexibility can be useful.
How does for
work?
Simple for
loop
The example we used for the while loop was a counting loop, so it is perfect to transform to a for loop. The looping part of this example is shown below, with comments pointing out the three aspects we need to code into the for loop.
The following code shows the example translated to use a for loop instead of a while loop. Notice that you can match up the initialiser, condition, and increment with the code in the while loop. This will run in the same way as the while loop did, it just brings these three parts together more clearly.
Use the following images to see how this works.
Reversed for
loop
Compare the for
loop shown above with that below, which moves in a numerically reverse order.
In this case the post-loop increment element subtracts 1 from the control variable, (incrementing it by a negative number). We have also changed the initialisation to ask for input from the user, rather than being a hard-coded value.