Programming Loops in C/C++/Java
Created | Updated Jan 28, 2002
Loops are one of the foundation stones of programming and they are pretty well
universal. This is a guide to the basics of writing a loop statement, and the
format these statements take.
For the initial section on higher level language programming loops I will be using example code in the format of the C programming language.
The While loop
This is one of the more popular forms of loop because it is the most adaptable. It
basically works on the basic premise:
WHILE(Condition is true)
{
do this;
}
Now with WHILE loops you have to remember that the condition has to be based on
a variable that is effected by the code in the loop, otherwise the loop will never
end. For explaination of the use of the semi-colon look at Aspects of Loop design.
The For Loop
The FOR loop is different, it is primarily used for counting loops or loops that need to work om a definedly changing value,
the condition is altered to make sure the loop ends in the start of the statement, for example:
FOR(The Condition Values Start Value ; The condition where the loop ends ; the change of the condition value)
{
do this;
}
The Condition Values Start Value
This is where you give the value the condition is based on the value it has when the
program first enters the loop.
(Example: i=0)
The condition where the loop ends
This is where you use LOGICAL and NUMERIC Operators to create an instance where the
loop will continue.
(Example: i==29 [this means that the loop will not stop while the value i is equal to 29])
The change of the condition value
This is the part which defines by what way the condition value changes with each
time around the loop.
(Example: i++ [This means the value i will be increased by 1 each time around the loop])
The DO WHILE loop.
This loop is different from the while loop as the condition isn't to enter the loop
but to repeat it, or leave it depending on how you look at it.
In other words it must perform the code at least once, and loops until the condition is no longer met.
The format looks like this:
DO
{
this;
}WHILE(repeat if this case is true);
Aspects of Loop Design
The Variable the Condition is Based on.
Now this is one of the most important parts of a loop, as we have to be able to enter a looping state,
but we also have to be able to leave it. This means the condition has to change within the boundaries of a loop
, and change in a way that will allow the loop to finish.
Further Information on Variables, and Variable Manipulation
The use of the Semi-colon in programming.
The semi-colon defines the end of an piece of code, it never appears after these {} brackets except with
structs.
Warning!
Watch out! if you put a semi-colon in the wrong place it can cause an infinite loop. Example of a
bad semi-colon:
WHILE(i less than 10);
{
i=i+1;
}
THIS IS AN INFINITE LOOP! Because it thinks the loop ends at the WHILE line and so doesn't
bother with the brackets, and so the value of i never changes and (assuming i is less than 10) the loop never ends.
However in Java the assembler issues a error if it meets this type of statement, but it is still worth watching out for.
Other Loop Entries
An Introduction to Programming: Programming LoopsProgramming Loops in Assembly Language