0:00

Let's begin this episode with a few more

"for loops" examples. This loop starts by

declaring a variable p and intializing it to 0.

The condition is "p strictly less than 10" and the increment statement

is "p += 2 ". Be reminded that

"p += 2" is strictly equivalent to "p = p + 2 ".

That means we add 2 to the variable p at every iteration.

p will initially have the value 0.

At the end of the first iteration, we will

add 2 to p; p will thus be 2.

Then p will be 4 and so on until

p will have the value 10. When this happens, the condition will become false

and we will exit the loop. Therefore, p will have the

values 0, 2, 4, 6 and 8. In this second

example, we declare a variable k and initalize it to 10.

The condition is "k strictly greater than 0."

Be careful, the sense of comparison has been reversed here!

The increment statement is

" --k" which is strictly equivalent to "k = k - 1 ".

That means we will substract 1 from the variable k

at every iteration. k will initially have the value 10.

At the end of the first iteration, k will be 9, then 8

and so on until k will have the value 0.

When this happens, the condition will become false

and we will exit the loop. In this example, k will have

the values 10, 9, 8, ..., until 1.

In this last example, we start by declaring

a variable i, initialized to 0. The condition is

"i greater or equal to 0". The increment statement

is " ++i " which means we will add 1 to i

at every iteration. i will initially have the value 0.

Then, i will have the value 1.

The condition will remain true.

Then i, will have the value 2.

The condition will still be true. Actually, in this

case, the condition will always be true, in principle at least.

Therefore, we will never exit the loop.

But why did we say "in principle" ?

Because, for technical reasons

we will not explain, it is not entierely true;

but we will still repeat the instructions many times.

A "for loop" can thus keep looping forever, if the condition is always true.

A few reasons, can explain this.

As in the last example, mayhaps we

made a mistake in the condition.

Once again though,

in such a case, we will still exit the loop

for technical reasons which will not be discussed here.

But it will happen only after a large number of iterations.

Another possible mistake is to err in the increment statement.

For example, here, the condition is about the variable i.

We made a mistake

and incremented the variable j instead. Since i is intialized to 0,

the condition will always be true and we won't ever exit the loop.

Now, let us move on to a few mistakes, frequent among beginners.

First of all, please note that there is no semicolon

at the end of this line, starting with the keyword "for".

Namely, there is no semicolon

here.

However, it so happens that the compiler will accept

your program if you were to add a semicolon here.

Yet, your program would behave rather strangely.

More precisely, with the semicolon, this code

will only print the word "bonjour" once. (TN: "bonjour" means "hello")

Why is that?

Because in fact, the semicolon here will be

interpreted as an empty instruction, an instruction doing nothing.

Therefore, the code will be interpreted like that :

only the semicolon will be considered as inside the loop's body.

Consequently, the loop will repeat the empty instruction 10 times.

Namely, doing nothing ten times.

On the other hand, the printing instruction

here, will be considered as being outisde

the loop and will be thus be executed but once.

This is why, the code will only print "bonjour" only once.

One must also be wary regarding braces.

For example, this code will result in this printing.

This instruction will indeed be repeted five times.

However, this instruction

will only be executed once? Why is that?

Because this code will be interpreted like that:

this instruction is indeed inside the loop,

while this one is outisde the loop.

Thus, pay attention! The indentation, that is

moving these two lines to the right,

won't be enough to have both instructions to be inside the loop.

If I want both instructions to be inside the loop, the only way is to add

an opening brace before the first instruction

and a closing brace after the last instruction.

And then, we will obtain the desired printing : " i égal 0, Bonjour, i égal 1...

...Bonjour", et cetera.

Also, please refrain from modifying the variable controlling the

number of iterations, inside the loop's body.

Namely, please refrain from doing as in this example where

we start by declaring a variable i intialized it to 0.

This variable i is used in the condition and in the increment statement.

But the variable i is also modified inside the loop's body.

Now, why should we avoid such a practice?

First of all, because it probably won't do what you expect.

Do not forget that the "for loop" itself will modify

the value of i through the increment statement.

Also, doing such a thing will make it much more difficult for a human reviewer

to understand what your program is doing. Indeed,

they will probably not exepect the variable declared in

the "for loop" to be modified within the loop's body.

Now, let us move on to an example

which should highlight the problems we can encounter

regarding "for loops".

In this example, we are trying to write a program asking 4 grades

to the user and printing the average of these 4 grades.

Without resorting to "for loops", we could write the program like that,

repeating four times these two instructions asking the user to input

a value and storing the inputted value in a variable.

The first variable is called "note1". (TN: "note" means "grade").

Then "note2", "note3" and "note4".

At this point, the variables "note" 1, 2, 3 and 4

contain the four values inputted by the user.

Now, I can calculate the sum and

store the result in a variable called "somme".

Finally, we only have to divide the sum by 4 in order to obtain the average.

Now, the fact that we used a different variable

for every grade prevents us from directly introducing a "for loop".

Therefore, we will start by rewriting the code

so that we only use one variable to store the four grades.

Therefore, the reworked code will appear like this.

We still have a variable "somme".

We ask the user to input a first value

which we store in the variable "note".

Now, we have this assignment : "somme = somme + note",

which we will explain soon,

then, we will repeat this code, using

the same "note" variable to store the values inputted by the user.

We can see that, at this point,

the variable "somme" does indeed contain the sum

of the four grades.

As before, we only need to divide "somme" by 4 to obtain the average.

Now, let us detail this new version of the code

to make sure it is still doing the same thing.

To that end, let us suppose that the user

is inputting the grades 5, 4, 6 and 4.

The program starts by declaring a variable "somme" and initializes it to 0.

We move on

to this instruction, printing a message

inviting the user to type a value.

This value will be read through keyboard interaction

and stored in the variable "note", declared here.

We decided that the first value would be 5.

Now, we move to this assignment,

first computing the value of "somme + note".

"somme" is 0. "note" is 5.

Therefore, the value of this expression is 5.

This value will be stored inside "somme".

Now to the second grade inputted by the user.

We supposed that this value was 4.

This assignment

will thus compute this expression, namely

"somme", which is 5, "+ note", which is 4.

We get the value 9, which will be stored inside the variable "somme".

10:54

Now to the third grade, which is a 6.

This expression will now be "9 + 6", that is 15.

We will store this result inside "somme".

At this point of the program, you should see

that the variable somme will indeed contain

5 + 4 + 6 + 4, which is

the sum of the four grades, 19.

Therefore, this instruction here will

print the average of the four inputted grades.

This new version will make it easier

to introduce a "for loop", thus skippping

the unnecessary repetitions of this three instructions,

which will be repeated four times, once for every grade.

We will thus move these three instructions

inside a "for loop".

Just like this.

Our loop will use a variable going from 1 to 4.

And thus, this program is equivalent to the former.

We can verify this easily through an example.

We should not forget to initialize the variable "somme" to 0.

Which we did here.

Now, at the first iteration, i is 1. Let us suppose that

the first grade inputted by the user is the value 5.

This instruction will thus store the value 5 inside the variable "note".

Moving to this assignment,

we will calculate the value of this expression,

as before, " 0 + note",

which is 5.

We get the value 5 for this expression

and the assignment will store this value inside "somme".

Namely, we will replace the 0, here,

by the value 5. The process is identical to the previous version,

only we have added a "for loop".

Therefore, at this point of the program, the variable "somme"

will contain the sum of the four grades inputted by the user.

Now, let us suppose that we wish to let the user choose

how many grades he wants to input.

How should we modify the program?

First of all, we will introduce a variable

called "nombre de notes" (TN: "nombre de notes" means "number of grades").

First of all, we have a message

inviting the user to input the number of grades.

Via this instruction, we will read the inputted value through

keyboard interaction, and store it inside the variable "nombre de notes".

Now, all we have to do is to replace this "4" by the variable "nombre de notes".

Similarly, we will replace this 4, which will allow us

to calculate the average. And we are done!

We now have a generic program

which will work for any number of grades.

However, we have just introduced a bug. Where is it, in your opinion?

The bug will occur if the user

enters the value 0 for the number of grades.

Providing we're not careful, we will divide by zero, here.

To avoid this nuisance, there are several solutions.

An idea is to test, before we calculate

the average, if the number

of grades is equal to 0.

Therefore, we can modify our program like this.

We simply added a conditional statement,

testing if the number of grades is strictly positive,

that is, not equal to 0.

Now, we can do the computations as before.

I just need to close the braces, here.

And if the user inputs a number

of grades equal to 0, we won't execute the division.

Let us conclude with the so-called "nested loops".

To that end, we will go back to the example

regarding the 5 times table, which used a "for loop"

to print the complete table. Now, let us suppose

that we wish to print all times tables from 2 to 10.

Easily enough, we simply need to move this code inside another "for loop"

and to modify it slighlty. Thus, we'll obtain all multiplication tables from 2 to 10.

First of all,

we will move this code inside another "for loop".

Which we did here.

Here is the previous code,

namely the one printing the 5 times table, which we

moved inside another "for loop". This second loop has a variable j, varying

from 2 to 10. j will thus take nine values,

from 2 to 10.

This whole code here will print

9 times the multiplication table for 5.

Since we would rather have all times tables

from 2 to 10, we simply have to replace

this 5 here, by the value of the variable j,

since j goes from 2 to 10.

Also, we will replace

this 5 aswell, by the value of j.

We thus obtain this code here,

with j appearing here and here.

We are stating that this code

will print all multiplication tables from 2 to 10.

We are now going to check this,

by exploring the code step by step.

We start with a first "for loop",

declaring a variable j and initializing it to 2.

The condition is "j less or equal to 10".

The increment statement is "++j".

Thus, j will take the values 2, 3, 4 and so on up to 10.

For now, j will be initialized

to 2.

We now enter this "for loop" and reach this new

instruction, which we added to print what table we're about to display.

Namely, we will print "table de multiplication par ", (TN: means "multiplication table for ")

then we print the value of j, then a colon.

Since j is 2, we will simply print 2, for now.

We now enter another

"for loop", declaring a variable i

and initializing it to 1.

The condition is "i less or equal to 10".

The increment statement is "++i".

i will thus take the values 1, 2 up to 10.

For now, j has the value 1.

We enter

this "for loop",

executing this printing instruction,

printing the value of j,

that is, 2,

then "multiplié par " (means "times"), then the value of i, that is, 1,

then "vaut" (means "is"), then the value of this expression, which is 2 times 1,

which is

2.

Since this is the only instruction in the "i for loop",

we'll jump back here and execute this

increment statement. Thus, i will go from 1 to 2.

The condition is still true.

Therefore we enter the "i for loop" again,

which means we'll execute this instruction; this time with

the value of i being 2. In the meantime, j is still 2. We print "multiplié par",

then the value of i, which is now 2.

Then we print "vaut" and the value

of this expression, which is 2 times 2,

that is 4.

And so on, until i will take the value 10.

There, we will execute the increment statement "++i".

Thus i will take the value 11 and the condition will become false.

So, we will exit the loop,

reaching this point,

at the end of the "for loop" j.

Now, we execute the increment statement,

"++j". j will go from 2 to 3.

The condition is still true.

And we enter the (body of) "j for loop" again.

Thus, we will the execute body of the "j for loop" again.

We print this message with, this time,

the value of j being 3. And we enter

this "for loop" again.

We restart with i being 1. Thus, we execute the instruction

inside the body of the "i for loop",

this time with the value of j being 3. We get the printing

"multiplié par" then the value of i, which is 1 again,

then the value of this expression j times i,

that is, 3 times 1: simply 3.

And so on until i takes the value 10.

We clearly see that for each

value of j, we will unfold completely the inside loop,

the "i for loop".

i will go from 1 to 10, thus printing the complete multiplication

table for the current value of j. This will keep

going until j takes the value 10.