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, just like here. Obviously, we could reach this goal by using five print instructions. However in such a case, we can, or even must use an iteration statement: the "for loop". Indeed, this code will do that printing by iterating, looping through the print instruction. We will thoroughly examine this code and explain the whos and whys. A "for loop" simply begins with the keyword "for". Then follows the declaration/initialization of a type int variable. This initialisation will be executed only once before entering the loop. Then appears something which you're already familiar with, for it is a condition, already discussed throughout the condition statement episode. This condition will be tested before each iteration. If it is false, we exit the loop. Then comes what we call an "increment" statement. Remember that the ++ operator, applied to the variable i, is strictly equivalent to the statement i = i + 1, and will thus add 1 to the variable i. This increment statement is executed at the end of every iteration. This allows to change the value of the variable related to the "for loop". Please note that the declaration and initialization, the condition and the increment statement are all separated by semicolons and are placed between parenthesis. Then comes the block of instructions which is the loop's body. It contains the instructions which will be repeated, executed at every iteration of the loop. As with the "if", the curly braces are mandatory for the instruction block only if several instructions must be repeated. In case there is only one instruction, here for example, the curly braces can be omitted. Namely, we are not compelled to place the curly braces here, at the beginning of the loop, at the start and end of the block. But, as was the case with "if", it is advised to keep the curly braces, which I did in the previous example. Now let's detail, step by step, the execution of our first example of a "for loop". The "for loop" starts, here, with the declaration and initialization of a variable i, here initialized to the value 0. This variable will control the number of iterations. Then, the condition "i less than 5" will be tested. i is equal to 0, 0 is less than 5 : the condiction is thus true and we will keep going, entering the loop body. Namely, we will execute this instruction : a good old printing which will print "le carré de" (= "the square of") followed by the value of i, namely 0, followed by "vaut" (= "has value") and followed by the value of i times i : simply 0. It is the only instruction in the loop body. The loop will now...loop and resume here. The increment statement will be executed. Namely, we will execute "++i" and i will go from the value 0, to the value 1. Then, we will once again test the condition "i less than 5". i is equal to 1, 1 is less than 5. The condition remains true and we will, again, enter the loop body, but this time with i containing the value 1. Again, we will execute this instruction which will simply print "the square of" followed by i's value, which is now 1, followed by "vaut" (="has value"), followed by the value of the expression i times i which is, obviously 1. Again we will loop, namely resume here and execute the increment statement. Thus, i will go from the value 1 to 2 and so on. At a certain point, i will contain the value 4. We will, again, execute the increment statement and i will go from the value 4, to the value 5. Then the condition "i less than 5" will be be tested. 5 is not strictly less than 5. Thus, the condition is now false. Since the condition is now false, we will exit the loop. Exiting the loop means jumping to the the instructions after the loop. Note, by the way, that the variable i, declared at the beginning of the loop, will not be usable after the loop. Indeed, the variable i exists only within the loop body, that is, this part here. The "for loop" follows the following syntax. First of all, the keyword "for" then, in parentheses, the declaration and initialization of a variable, not necessarily of the type int, then a condition, a priori regarding the aforementioned variable, though it is not mandatory then, an increment statement which should impact this variable aswell. And, finally, a block of instructions, the instructions of which will be repeated throughout the loop. Also, we remind you that the three elements in parentheses of the "for loop" are separated by semicolons and that there is no semicolon here. The "for loop" will repeat the instructions within the block as long as the condition is true. If the condition is never to become false, these instructions will be repeated indefinitely. By the way, in C++ 2011, there is another form of "for loop" which will be discussed in the lessons dedicated to arrays and vectors. Let us resume with another example regarding the "for loop". Let us suppose we wish to print the multiplication table 5. Without a "for loop", we will almost have to repeat 10 times the same instruction. Namely, this instruction here which prints 5 times 1 is 5*1. Followed by the instruction 5 times 2 is 5*2, and so on until 5 times 10 is 5*10. In such a case, it is required to use a "for loop" in order to avoid all these repetitions. This "for loop" will be written like this : We initialize the variable in charge of controlling the number of loops, this time to 1. As condition we will use "i smaller or equal to 10". Be reminded that the symbol "smaller or equal to" is represented by the less-than sign (<) followed by the equal-to sign (=). Thus, with our initialization to 1, the condition "smaller or equal to 10" and the increment statement at every iteration, i will take, consecutively, the values from 1 to 10. The loop body will be presented this way: at every iteration, we will print the value of i and the expression 5 times i. As expected, this "for loop" will print the complete multiplication table of 5. The block of instructions, repeated by a loop, may contain any instruction. For example, a conditional statement. Here's a quiz relating to a conditional statement in a "for loop". In your opinion, what will this code print upon execution? The correct answer is the answer A. Let us explain why. First of all, you can see that i will take the values 0, 1, 2, 3 and 4. At the first iteration, i will contain the value 0, This instruction will thus print i's value, namely 0. Be careful, there is no line break here. Thus the next thing printed here, will be printed right after the 0. Next, we pass to the conditional statement which tests if i modulo 2 is equal to 0, that is, if the remainder of i divided by 2 is 0. Since i contains 0, this condition is true and, thus, the instruction within the conditional statement, will be executed, namely printing p. We then exit the conditional statement and end up here printing a blank space, which we will represent this way. We are now at the end of the loop. The loop will now start anew with, this time, i containing the value 1. This instruction will be executed again. Since i contains 1, we will print 1. Then, we encounter the condition statement again, testing if the remainder of 1 divided by 2 is equal to 0. it appears that this condition is now false since 1 modulo 2 is equal to 1 and thus not equal to 0; we will skip this instruction and jump to this instruction here printing 1 blank space and so on. You can plainly see that the code will print what stands under the answer A, namely this. Let us ask a subsidiary question : Can you explain, in plain English, what this conditional statement does ? Mathematically, it tests if the remainder of i divided by 2 is equal to 0. In plain words, it means : is i an even number ? Self-evidently. Thus, this condition tests if i is even.