7.1 Repeat, please!
Imagine you have to make a program to print all the numbers from 1 to 10.
Well, you could use ten println instructions. But what if you must print the numbers from
1 to 100, or even to 1000! That could take a while...
A few special instructions are available to solve these situations.
They can repeat one instruction or a block of instructions for a specified number
of times, or until a condition is satisfied.
7.2 While instruction
The first one is called while and here is an example
that shows how we can write our counting program using this instruction.
Run it and it will display all numbers from 1 to 10.
So, as you can see, the print and incremet instruction block is repeated until
the value of our variable exceed 10, and the condition from the while will become
false. At that point, the program no longer repeats and continue execution
from the next instruction, in this case the print of the "done" text.
Here is the while instruction flow scheme and how it can be written only with "if" and "goto" instructions:
One repeating cycle is called a loop.
The while instruction is characterized by testing
the looping condition first and only then, if the
condition is true, it executes the looping block.
It is your job, as a programmer, to make sure your loops will eventually come
to an end, and will NOT repeat forever. If in the above example you forget to increment
the value of the variable i, the looping condition will never become false, the
program will loop forever, and you will be doomed! So make sure, you always add
necessary code to end the looping after a finite number of repetitions.
Here is a bad example of an infinite cycle:
7.3 Do while instruction
The second looping instruction is called do while
and here is how our counting program can use it.
Note the difference from the previous looping instruction. The do
while does a repeating cycle first, and then it tests for the condition.
So, no mater of the condition result, the looping block gets executed at least
once. See that now we must test with i<10 and not with i<=10, because,
by the time the value of i is 10, it has already been printed.
Here is the do while instruction flow scheme:
7.4 For instruction
The last looping instruction is called for. The difference
from previous instructions is, that it can declare and initialize the counter variable
and include the condition and the counter increment, in it's format.
That makes it most fitting for our kind of program.
The syntax of the for instruction has three internal parts, called
initialize (usually an initialize of a counter variable),
condition (usually a test of the counter) and
increment (usually an increase or a decrease of the counter).
In our example, the initialize is i=1, the condition is i<=10 and the increment is i++.
The form above is the most common use of the "for" instruction, but the three
parts can contain more complex code, and some of them can even be omitted.
Here is the for instruction flow scheme:
7.5 Break and continue
Now, that you understand how the looping instructions work, you should know that
you can break the looping by force, or, at one point,
you can skip the rest of the looping block and continue
with the next loop. Here is an example:
If you run this example you will see that the print of 2 is skipped, as the continue
instruction commands, and that the looping is breaked after printing the value 8.