Page 4 of 5
The 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 Java programmers delight in finding new ways of using it. Good style suggests that you should stick to simple ways of for loops.
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 Java conditional loop is the While loop and as its name suggests it keeps repeating instructions while a condition is true.
will repeat the instructions over and over again until the condition is false. For example:
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.
There is a variant on the while loop that is worth knowing about - the do-while loop. To see why a variant is needed consider what the minimum number of times a while loop can be carried out. For example:
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.
At this stage it isn't easy to invent realistic examples of how to use control statements without introducing a lot more Java.
However to close without seeing some real code in action isn't satisfying. So lets write a simple program that demonstrates the if, if else, for, while and do while loop. These aren't realistic or convincing but they are simple.
Enter the code or download the program from the CodeBin and try it out. Modify it and see if you are happy with the result. If you are a complete beginner to programming absorbing the way that control statements work is hard but once you have done it learning the rest of programing is easy.