In a previous episode we saw that to program is to describe a task that we want the computer to execute, and that this task can be broken down into data that will represent real life variables on the one hand, and a number of computations, manipulations, and operations on the other hand. We have seen how some computations could operate on the data. But if we really want it to be a program, the data must be able to influence the computations. This is what are called control structures, and it's what we are going to have a look at in this episode. Until now, our programs were simply made of instructions that were executed one after the other, and the data had no influence on the processing. To have influence, so that the values of some of the data, some of the variables, are able to alter the strictly linear flow of the execution, we use what are called "control structures". Fundamentally, there exists three types of control structures. First of all, the conditional branches, which we are going to have a look at in this episode, and which allow to select a sequence of code or another, depending on certain values. Next, we have the iterations, which allow to repeat an entire sequence of instructions. Finally, we have the conditional loops, which also let us repeat a sequence of instructions, but with a condition. Let's begin with the conditional branches, which let you skip certain parts of the program, or let you choose what parts of the program to execute. Let's take a concrete example. Suppose for example you want to do one thing or the other depending on an a value entered by the user. Suppose for example that you want to execute a certain sequence of instructions if n is less than five, for example to print out that the number is less than five, and contrariwise if n is greater or equal to five, then you want to execute another sequence of instructions. Well this right here is what we call a conditional branch, and we are now going to detail it's use. To this effect, you first have a part that begins with the keyword "if", that will make this branching. Here, this "if" will let you make a choice. If the condition that follows it is true, the "if" will execute the instructions indicated below. You could optionally have an "else part", which is used with the keyword "else", which is here. If the condition isn't true, then this second part will be executed. The general structure is the following. We have the keyword "if", that is followed by a condition given in parentheses. We'll come back later on the conditions. Then we have an opening brace, a block of instructions that we want to execute if the condition is true. When the condition is true, this block of instructions will be executed. Then there's a closing brace. Then, we can optionally have the keyword "else", another opening brace, another block of instructions, here in blue, that will be executed if the condition is false. So that's the general structure, but let's have a step- by-step look at a particular example. In this example, the control structure, the conditional statement that we are interested in, will be found here. Before, we start by declaring a int-type variable n that we didn't initialize, so here we don't know it's value yet. Then we print the message "entrer un nombre" (= "enter a number") on the terminal, followed by a line-break. Then we wait for an input of type integer from the user. Lets' suppose the user inputs the value 3. So now n has value 3. We now come to our conditional branch, which is introduced by the keyword "if", here, and which has it's condition in parentheses: n smaller than five. We ask ourselves, is n stricly smaller than 5? 3 is strictly smaller than 5. Here the condition is thus true, and we execute the code, the block that immediately follows the "if". So, what happens is that we arrive at the execution of cout, "votre nombre est... (= "your number is...) ...plus petit que 5" (= ...smaller than 5"), is the message that will be displayed. The execution then continues after the block. In other word we skip directly to the instruction here that prints the message "Au revoir" (= "Bye"), and the program ends. What about if at this step here, the user had entered the value 7? At this stage, the program will check again, whether 7 is strictly smaller than 5. But now the condition is false. 7 is not strictly smaller than 5. As the condition is false, the if will this time skip to the else part, and completely ignore the previous part and branch directly (hence the name "conditional branch"), to branch directly here at this display line that will print that the number n is greater or equal to 5. Then we continue the execution of the program after the block, the part of the program that follows the conditional branch, and display the last message "Au revoir". The curly braces are used to define the block of instructions that is going to be monitored, either the block of instructions that is executed when the condition is true, or the block of instructions that is executed when the condition is false. The block of instructions is the set of instructions that we want to monitor, on which we want to branch, and we can gather many instructions, as many as we like, in one block. Let's suppose for example that we not only want to print that the number is smaller than five, but also print the value of the number. In that case, when the condition is true, we'll simply, in the block monitored by the condition, when the condition is true, here, add a line to this block. This is perfectly possible and lets us monitor many instructions with our conditional branch. When a block includes a single instruction, the curly braces are absolutely optional. I urge you however to always use these blocks and always put these curly braces, in order to keep the code readable and maintainable. But I'm just pointing out that these curly braces are optional, in case you come upon this kind of code. Likewise, the else block is also totally optional, we can have an if without any else. In that case if the condition isn't true, then we'll simply continue the execution of the program. We'll do a particular thing only if the condition is true. If the condition is true, then we'll additionally execute the block monitored by the if. If I take an example, again with the input value typed on a keyboard of an integer, with the test without the else, so if the input value is strictly smaller than five, for example three, in this case we'll branch here and execute the display of "Votre nombre est plus petit que cinq", then we'll continue with the rest of the program, so here we'll print "Au revoir". Contrariwise, if we input a greater value, strictly greater or equal to 5, for example the value 7, at that moment the test is false and we simply don't execute anything in particular, we simply continue by ending the program by printing the message "Au revoir". To sum up, the general structure of a conditional branch in C++ is the following : we have an if instruction followed by a condition in parentheses, then a block of instructions here, in curly braces, which are the instructions that are going to be executed if the condition is true, and optionally the keyword "else" followed by a block of instructions which are executed if the condition is false. And the instructions here that are in the blocks are absolutely ordinary C++ instructions, they can in particular contain if statements themselves, which are so-called nested statements. Let's take an example, suppose for example that we have three values x, y and z, and we want to test their equality, test if x is equal to y, test if x is equal to z, or test if the three variables are equal. We'll start for example by testing if x is equal to y, and here you see we'll have a block going all the way to here, so this is the case where x is equal to y and if x is different from y, at that moment this other block will be executed. If x is equal to y, then we can enter a block of instructions and have another instruction which contains its own sub-block of true conditions, here, and optionally an else for a sub-block of false conditions. So if y is equal to y, we enter the block, and if y is equal to z, then in this case we print that the three values are equal. Otherwise, if y is equal to y but y isn't equal to z, in this case we print this, only the two first values are equal. We could also do so in the case where x is different from y. Obviously, we shall not abuse this kind of solutions. In practice, if you have more than three levels, then the programs becomes quickly unreadable. Let us now illustrate concretely the previous example with, for example, the case where we have three variables x, y and z, all with value one. We begin with the first if: is x equal to y? Yes, one is equal to one, so we enter the block here, so we are going to execute our second if here : is y equal to z? So we ask ourselves the following question : are the two values equal? In this case they are, 1 is equal to 1, so we enter here this line, and message that is going to be printed is "les trois valeurs sont égales" (= "the three values are all equal"). Then the program will continue and execute what follows. Here, there isn't anything to execute afterwards. Execute the following of the first block, of higher level, there still isn't any following, so the program end here. Now suppose we have different values, for example that we have the value 2 in z and y, and the value 1 in x. So here the first condition will be tested, is 1 equal to 2? Of course not, so the condition is false and we branch here on the corresponding else statement, and we execute this block here, with the test of a second condition : is x equal to z? Is 1 equal to 2? This is also false; so this second if will branch on a block that will be executed, which is this block here, which begins with a third if statement, which will evaluate its condition. Is y equal to z? 2 is indeed equal to 2. So in this case we execute the block that is monitored by the true condition, so here we print that the two last values are equal, then we end the program's execution, which means we end the execution of the instructions here that followed the if. There isn't anything, then the end of the instructions of this block, there isn't anything either, and finally the instructions of the higher level block, nothing either in this particular case, and that's how the example's program would execute.