|Fundamental C - Program Structure|
|Written by Harry Fairhead|
|Monday, 17 October 2016|
Page 3 of 4
All languages have some way of repeating a block of code and it is fundamental to programming. While it is theoretically possible to do everythign you need to do with just one type of loop most language provide more for ease of use. C has just three types of loop - the for loop, the while loop and the do while loop. Lets take a look at each one in turn.
If you want to do an instruction three times you could achieve that simply by writing it three times:
However this isn't a clever way to work and it quickly becomes increasingly difficult as the number of repeats goes up.
To allow you to repeat an action a set number of times most modern languages provide a for loop - the reason for the name will become apparent very soon.
The problem is that the C style for loop is very flexible but it can seem complex to the beginner.
It is easier to look at an example before seeing the general form:
this will repeat the single instruction but it is more common to use a compound instruction to repeat a block of code:
Note: you can only declare the variable counter within the loop in C99 and later.
When the for loop is obeyed the counter variable is first set to 1 as specified by the first part of the loop:
Then the test specified:
is evaluated. If it is true the "body" of the loop is obeyed. If it is false then the loop comes to an end and if this is the first time the for loop has been encountered the body of the loop is skipped.
If the condition is true then then the instructions in the loop are carried out. When these are completed the counter has one added to it as specified by the final part of the for:
The ++ operator is the increment operator and simply adds one to the value stored in counter. Then the condition counter<11 is evaluated again and if it is true the loop repeats i.e. the instructions in the body are carried out all over again but this time with counter storing the value 11. If it is false then the loop is complete.
If you think about this mechanism you should be able to see that this will cause the instructions to be repeated for values of "counter" starting at 1 and ending at 10.
Why is 10 the last value?
Simply because when you add one to ten you get 11 and 11 is not smaller than 11 and so counter<11 is false and the loop ends.
So you can just think of the for loop as being a way to repeat a block of code n times:
Notice that now the loop starts from 0 and it repeats n times. The counter now counts from 0 up to n-1 and hence the loop repeates n times.
General for loop
You can think of
as being equivalent to "repeat for values of counter from start to end".
If you don't like the "end+1" in the condition you can write the for loop as:
where counter<=end is true if counter is smaller than or equal to end. It's all a matter of taste.
The most general for loop is:
and in this case the initial instruction is performed once when the loop starts. The condition is evaluated at the start of each repeat of the loop and the loop only continues if it is true. The increment instruction is performed after the loop has executed all of the instructions in the body of the loop and before the next repeat is contemplated.
This form of the for loop is capable of being used in many wonderful ways and some C programmers delight in finding new ways of using it. Good style suggests that you should stick to simple ways of for loops.
If you find the for loop difficult to grasp or to remember then don't worry. Most uses of the for loop are simple and just about repeating a block of instructions a number of times. For loop are also made to go with another programming idea - the array. In this case the counter or index variable is used to specify which of a list of values you are processing - more of this later when we introduce the array.
The for loop is designed to allow you to repeat a block of code a set number of times, but this isn't the most general form of loop.
The most general is the conditional loop that repeats a block of code until a condition is satisfied.
The for loop is like the instruction to
"eat three sweets"
but the conditional loop is more like
"eat sweets while you are hungry".
The basic C conditional loop is the while loop and as its name suggests it keeps repeating an instruction while a condition is true.
will repeat the instruction over and over again until the condition is false. That is the condition is evaluated, if it is true the instruction is obeyed, then the condition is evaluated and if it is true the instruction is obeyed and so on. The repeat stops when the condition evaluates to false.
Repeating a single instruction isn't as common as repeating a block of instructions using a compound instruction:
will repeat the instructions over and over again until the condition is false.
This is also how NetBeans will automatically format a while loop for you. This assigns one to a it then tests to see if a is less than 10, it is so the loop is carried out and a is incremented i.e. a has two stored in it. The loop goes back to the start and the condition is evaluated and again a is less than 10 so the loop proceeds.
You should be able to see that the loop is repeated until a reaches 10 so the loop repeats for values of a from 1 to 9.
In this case the while loop is equivalent to a for loop i.e.
but in general you can write while loops that are not simple "repeat a set number of times". It is in this sense that a while loop is more general than a for loop.
Do ... while
There is a variant on the while loop that is worth knowing about - the do-while loop.
The most basic form is
This repeats the instruction while the condition is true. However order fo the test is slighly different - now the instruction is obeyed and the condition evaluated, if it is true the instruction is obeyed again and the condition evaluated and so on..
Of course the more common form of the do-while loop makes use of a compound statement to repeat a block of code:
Again the block of code is obeyed while the condition is true.
To see why this variant on the while loop is needed consider what the minimum number of times a while loop can be carried out.
In this case because a isn't less than 10 when the loop starts the body of the loop isn't carried out at all i.e. it is skipped.
Compare this to the
In this case the test that ends the loop comes at the end of the loop. What this means is that the instructions in the loop will be obeyed at least once.
This is the only difference between the while and the do while loops - the test is either at the start or the end of the loop respectively.
This in turn means that the while loop can skip over the loop and so the minimum number of repeats is zero but the do while loop has to execute at least one repeat.
You will quickly learn which form of the loop you need even if it seems a bit confusing at first. If you need to do something once before the condition can be tested than you need a do while otherwise you probably need a while.
|Last Updated ( Tuesday, 11 September 2018 )|