In a previous episode, we saw that to program consists of describing a task for the computer to execute. This task being decomposed on one hand in a number of data, that is, physical quantities of the real world which we want to represent in the program; and a number of computations, that is, operations and manipulations of these data. We have already seen how to manipulate the data with operators. But in order to truly have a program, the data should be able to influence on the computations to be done. This is what we are going to study in this episode. It's what are called "control structures". Indeed, until now, the programs we executed were simply sequences of instructions one behind the other. Theses computations weren't conditioned by the values of the variables. Control structures precisely allow to choose, for example, if we want a certain sequence of instructions or another, depending for example on the value of a variable. That is what we call a "conditional branch" (or "conditional statement"). There exists in fact three control structures : the conditional branches, which allow to choose between the execution of a piece of code or the execution of another piece of code, depending on certain conditions on the data. There exists the iterations, which are simply loops on a set of code snippets to be repeated a number of times; and finally the conditional loops, which also repeat instructions a number of times, but depending on a condition which is evaluated every time. In this episode, we are going to have a look at the conditional branches, which allow to skip certain parts or to choose certain parts if various conditions are fulfilled. Now let's have a better look at all this. Let's suppose, for example, that we want to do this or that, depending on the value of an integer that was entered on a keyboard, here by using the "scanner". We are going to use what we call a conditional branch, which I will detail in a moment, but for example if the value is less than five, we are going to do a certain thing, and if the value is greater than five, we are going to do another thing. That's the idea of a conditional branch, in which we choose, depending on the fact that n is greater than or less than five, this or that piece of code to execute. The lines in question are these three lines here, which are organized according to the general following syntax. We begin with the keyword "if". Then in parentheses we here have a condition. Then we have here in curly braces a block of instructions which will be executed when the condition is true. We'll then have, optionally, another keyword, "else", which will itself monitor another block of instructions, which will be executed if the condition we tested at the beginning is false. Here we see that depending on the condition, we are going to execute either a block of instructions, or another. This is what we call a "conditional branch" depending on the condition. The general structure of a conditional statement is the following: we begin with the keyword "if". Then, we put a condition in parentheses. We'll return later in the next episode on what the conditions are and how to write them. We then have a block of instructions which executes when the condition is true. Then, we optionally have the reserved word "else", followed by another block of instructions, which will be executed when the condition is false. Let's have a look, on a concrete step-by-step example, how this works. Let's suppose we have the following code, with the conditional statement situated here. But first, we begin by asking the user a question, to input an integer. So let's suppose for example that the user inputs the integer 3. A this point, we'll get to this conditional statement and ask ourselves: is 3 less than 5? 3 is indeed less than 5, so the condition here is true and therefore the block of instructions which will be executed, is the block that immediately follows the condition, which means that in the program, in this case, in which the user has entered 3, is going to print the message : "votre nombre est plus petit que cinq." (= "your number is less than 5") Then, the program will continue after the conditional statement here; it will continue the normal progress and print here: "fin du programme." (= "end of program") Now what happens in the case where the user enters a number greater than 5? For example, if he enters the value 7, we'll get to the condition in the conditional statement, and evaluate the condition, is 7 less than 5? The condition is of course false, which means that the branch will be made here on the block monitored by the keyword "else", which is the block used when the condition is false. We'll therefore print here : "votre nombre est plus grand ou égal à cinq". We'll then continue the execution after the conditional statement block, so here we'll once again end by printing "fin du programme". The curly braces are used to control a set of instructions, whether it's the block of instructions executed when the condition is true, or whether it's the block of instructions executed when the condition is false. These curly braces are used to regroup the instructions that we want to put together to execute in this or that condition. We can of course add as much instructions as we want in a block of instructions. Let's suppose for example that we want to print a message which indicates the value of the number, when the number is less than 5. So, what we are going to do, is, in the block conditioned by the condition "n less than 5", add here a line which will let us print an additional message. By the way, when a block contains a single instruction, it isn't strictly necessary to use the curly braces. However, I recommend you to always use a block structure, that is, to always use curly braces. This makes the code not only more readable, but also more maintainable; it facilitates adding instructions later on: if we want to add an instruction here, we can do it without any other hassle, as the block is already there. However, you could encounter here and there, in external code, this kind of syntax without curly braces. This is why it is useful to know it, even if I don't encourage you to use it. I rather recommend you to systematically use the block syntax, that is, with curly braces. Furthermore, you must know that the else-block is totally optional. We can absolutely write a block of conditional statement, here, without any "else". Simply, what happens is that when the condition is false, nothing in particular happens, and we continue to execute the program normally. In such a case, something in particular will be executed only when the condition is true. Let's have a look at an example. Let me come back to the previous example with the value input. Let's assume that the user has entered the value 3. We arrive here at the conditional statement. We test the condition: is 3 less than 5? This condition is, of course, true, which means that we execute the block here monitored by the conditional branch, and therefore we print here the message : "votre nombre est plus petit que cinq." (= "Your number is less than five") And then, we continue the execution of the program as usual and we print the ending message here. What if the user had entered a value that is strictly greater than or equal to 5, for example 7. It is greater than or equal to 5, so the condition is false this time. In this case, nothing special happens. We ignore the conditional statement block and continue the execution after the conditional statement block, which means that in this case, we only execute the ending line, the ending message. In Java, conditional statements follow the following general structure: we start by the reserved word "if"; then in parentheses, here, we write the condition which we want to evaluate. Then we put here a block of instructions which will be executed if the condition is evaluated to true. Then we could put here an optional else-block with a block of instructions which will be executed if the condition is evaluated to false. These blocks of instructions can contain any part of a Java program, and can, in particular, also contain other if instructions. In this case, there are what are called nested choices. Let's have a look at an example. Let's suppose that we have three variables x, y, and z, of which we want to test the equality. For example, is x equal to y, is y equal to z, or are they the three equal to each other? What we can do here, is to begin by a block of instructions which will test if x is equal to y. This block extends right to here, with its associated else and the else-block, and in the block, for example, monitored by the first test, when the first test is true, we see here likewise a second if, which is perfectly possible, with its own block for true conditions and its block for false conditions. In the same way, in the first else, we see here again an if with its block for true conditions and its block for false conditions; and we see that in the last block of conditions, we can even have a last if. You know understand why you should not abuse of this kind of construction. It quickly becomes a little difficult to understand, difficult to read. Above three levels, I think that it becomes difficult to understand, if not even totally unreadable. Now let's have a detailed step by step look at the example. Let's suppose we have here three variables, x, y, and z which each contain the value 1. We begin with the first if statement, here, which will test : "is... ...x equal to y?". We effectively have, here, a 1 that is equal to a 1. The condition is true, which means that we arrive at this second if statement, here, which will evaluate whether the value of y is equal to the value of z. We therefore ask ourselves again: "is 1... equal to 1?", which is indeed true, which means that we enter the block here and we print the message : "les trois valeurs sont égales." (= *the three values are equal") Then, we continue the execution at the end of the block considered. Here, there isn't anything in particular to do. Then, we also continue the execution after this block here. That is, we arrive here at the end of the program. Now, let's look at what happens when we have, for example, the values y and z which each have the value 2, and which are different from x. The program begins, like before, by evaluating: "is the value of x... ...equal to the value of y?", And in this case, of course, the answer is no and therefore the condition here is false, so we execute the corresponding else-block. We therefore enter here the else-block at the bottom of the program, which means that we enter here in the test of this second condition: "is... ...the value of x (1) equal to the value... of z (2)?". Of course, this condition is false and therefore this "if" will branch to its else-block, and we will find ourselves evaluating this third condition: "does y have a value equal to z?". 2 is equal to 2: here, we have of course a condition that is true, which means that this condition, this if block, this conditional branch will execute the block here conditioned by the fact that the condition is true, and will therefore print: "seules les deux dernières valeurs sont égales" (= "only the last two values are equal"). We will then continue the execution from there, we will continue after this block here, that we are executing. There isn't anything special. We then continue the execution after this conditional branch. There isn't anything special. We then continue the execution after this branch and we arrive at the end of this example.