|Modern Java - Writing Code|
|Written by Mike James|
Page 4 of 6
The if statement is the basic conditional and in theory we don't need any other sort of conditional - but in practice it is easier to have some flexibility in the language.
The if statement determines if a block of instructions will be carried out or not. The if..else statement selects one of two blocks of instructions.
and if the condition is true then instructions 1 are carried out and if it is false instructions 2 are carried out.
Notice that in any situation only one of the blocks is carried out - the condition has to be either true or false and hence either the first or the second block of instructions is carried out. Beginners sometimes think that because the second set of instructions are below the first they are carried out by default - they aren't.
In most cases the if else which selects between two alternatives is enough but there are other Java statements that will select in fairly complicated ways between multiple blocks of instructions.
If you want to know more about these look up "else if" and the "switch" statement.
We will return to these more sophisticated control statements later.
In most situations if and if..else are all you need to write a program.
The for loop
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.
Most language also use a particular form of the for loop that was introduced by an earlier language C. The only 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:
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 two 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.
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.
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 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.