In this episode, we will discuss

a second contol structure, namely the iteration statements

So, what is the use of this new control structure?

Let's imagine, for example, that we wish to print the square

of the five first integers. Namely, we wish to print

"0 squared is 0", "1 squared is 1",

"2 squared is 4" and so on,

until "4 squared is 16".

Naturally, we could use five printing instructions.

However, in such a case, we can - we must! -

use an iteration, also known as a "for loop".

Such a code will result in the desired printing by

looping, iterating, on a single printing instruction.

Now, let us probe this code for the hows and whys.

An iteration, also known as a "for loop",

simpy begins with the keyword "for".

Then comes the declaration and intialization

of a variable. This variable will control how many

times we loop.

This declaration and intialization is only

executed once before entering the loop.

Then comes something you're already familiar with, for it is

a condition. You encountered conditions

during the lesson about conditional branches.

This condition will be tested before entering the loop.

If it is true, we will keep on

executing the loop. If it is false, we will exit the loop.

Then comes an increment statement.

In our example it is "++i".

Please be reminded the increment operator "++",

used on a variable i, is strictly equivalent to the instruction " i = i +1".

That means we add 1 to the variable i.

This increment statement will have the variable i

evolve, thus controlling the number of iterations.

It is executed once at the end of every iteration.

The declaration and initialization of the controlling variable,

the condition and the increment statement

are written in parentheses and are separated

by semicolons. Then comes

the block of instructions;

it is the loop's body and contains

the instructions repeated at every iteration.

Similarly to the conditional statement "if",

the braces are mandatory only

when several instructions are to be repeated,

namely, when the loop's body contains several instructions.

Therefore, if we only wish to have instruction repeated -as we do here-

we can discared the braces here and here.

However,

even in such a case, we strongly advice to use braces. Namely,

adding an oepning brace here, before the instruction,

and a closing brace here, after the instruction.

Now, let us detail step by step the execution

of our first "for loop" example.

First of all, the loop

declares a variable i and initializes it

to the value 0.

Then, we test the condition. Here, the condition is "i strictly less than 5".

The condition is true. Indeed, i is 0 and

0 is strictly less than 5. Therefore, we can enter the loop's body.

Entering the loop's body means executing

this instruction here. In our example, it is the only instruction within the loop's body.

This instruction will print "the square of"

following by i's value, which is 0,

followed by "vaut" (= "is"/"has value"), followed by the value of the expresion

i*i, which is simply 0.

Then, we reach the end of the loop's body.

So we jump back to this line,

more precisely to the increment statement ++i.

We will add 1 to i's value : i is now 1.

Now, we test the condition

"i strictly less than 5", which is

still true and we can thus enter the loop.

Entering the loop means repeating this

printing instruction : "the square of", then i, which is now 1,

then "vaut" and, finally, the value

of the expression i*i, which is obviously 1.

Now we reach

the increment statement again: i will go

from 1 to 2 and so on until

i has the value 4 and we execute

the increment statement, rising i's value to 5.

We will test the condition

"i strictly less than 5". This time,

the condition is false, for 5 is not strictly less than 5.

Since the condition is false, we will exit the loop.

That means we will resume right after the loop

and execute these instructions.

By the way, the variable i, declared here does not exist anymore.

Therefore, we cannot use it anymore.

i does only exist within the loop.

Let us review, the "for loop"'s syntax.

First of all, the keyword "for".

then, in parentheses, the declaration and initialization of

a variable -its type is not necessarly int, by the way.

Then the condition which should, a priori, relate

to this variable, though it is not mandatory.

Then an increment statement, which should relate to the variable aswell.

Finally comes the block of instructions,

constituting the instructions repeated by the loop.

Please remember that the three

elements inside the parentheses of the

"for loop" are separated by semicolons but that there is no semicolon here.

The loop repeats the instructions inside

the block as long as the condition is true.

If the conditions does not even

become false, these instructions will be repeated indefinitely.

Let us move on to another example a "for loop."

Let us suppose that I wish to print

the multiplication table of 5. Without using a "for loop", I would have

to repeat almost the same instruction ten times.

Namely this instruction, printing "5 times 1 is 5*1",

followed by "5 times 2 is 5*2",

and so on until "5 times 10 is 5*10".

Again, in such a case, one has to use a

"for loop". The loop will be coded as follows.

We will declare the variable controlling

the number of iterations and initialize it to 1.

Our condition will be "i less or equal to 10".

By the way, remember that the operator "less than or equal to" is formulated

by the less-than sign (<) followed by the equal-to sign (=).

Finally, we will use the increment statement ++i.

The variable i, will thus take the values from 1 to 10.

This "for loop" is thus equivalent to all

the ten printing instructions

and will print the multiplication table of 5.

The block of instructions of a "for loop" can contain

any instruction whatsoever, a conditional statement, for example.

Let us have a quiz regarding

a conditional statement inside a "for loop".

In your opinion, what will this code print upon execution?

The correct answer is

the answer A. Let us explain why.

The loop begins by declaring a variable i and intializes it to 0.

The condition is "i strictly less than 5". The increment statement

is ++i. Therefore, i will take the values from 0 to 4.

The first instruction within the loop's body

prints i's value: right now, the value 0.

By the way, we used the instruction "print" and not "println" which means

that the next printed information

will be printed right after the 0, right here.

Now we move to the conditional statement, testing if i modulo 2

is equal to 0.

To calculate i modulo 2, we start

by dividing i by 2. i is 0.

0 is 0 times 2 plus 0 .

Therefore, i modulo 2 is 0. All this is 0

and the condition is true. We can thus enter the conditional

statement and execute this here instruction printing the character "p".

Finally, we move on to the last instruction in the loop's body,

printing a blank space which we will represent this way.

Now we reach the end of the loop's body and jump back here.

i goes from 0 to 1.

We execute the loop's body again, in other words we resume

with this instruction printing's value, which is now 1.

We move on to the conditional statement, testing if i modulo 2 is equal to 0.

To calculate i modulo 2, we divide i,

that is 1, by 2. i can be written 0 times 2 plus 1.

Therefore i modulo 2 is 1.

We conclude that the condition is false.

We will thus skip this instruction and

move on to this instruction, printing a blank space.

Now we reach the end of the loop's body and jump back here again.

i goes from 1 to 2.

And we resume inside the loop's body.

We print the value of i, that is 2.

We move on to the conditional statement and calculate i modulo 2.

i is 2 and it so happens that

2 can be written 1 times 2 plus 0.

Therefore, i modulo 2, is equal to 0, this 0 here.

The condition is thus true.

Thus we enter the conditional branch,

executing this instruction, printing the character "p".

We move on to

this instruction, printing a blank space and so on.

We now see that the end result is none other than answer A.

This condition with the "modulo" here can be a bit complicated

to understand for beginners but it can be interpreted easily.

Actually, it only tests the remainder

of a number's division by 2

In plain words, it means we are testing

if this value - i's value - is even.

This condition is thus equivalent to "is i even"?