Writing Java Code - Methods
Writing Java Code - Methods
Written by Ian Elliot   
Thursday, 20 January 2011
Article Index
Writing Java Code - Methods
The flow of control
Conditionals - if
Try it out


The general For loop

You can think of

for(int counter=start;counter<end+1;counter++)

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:

for(int counter=start;counter<=end;counter++)

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:

for(initial;condition; increment)

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.

While loop

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.

That is


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.

Do While

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.

Some Examples

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.






Last Updated ( Saturday, 21 May 2016 )

RSS feed of all content
I Programmer - full contents
Copyright © 2016 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.