In a previous episode, we have discussed control structures; they are Java instructions making it possible for data to influence, choose or repeat, different parts of the program. We have seen that these control structures are closely related to the block notion. This block notion will impact the use of variables. We call this the "scope of variables", and this will be the subject of this episode. In Java, instructions can be regrouped in blocks, and this intependently of any control structure. To this end, one simply needs to regroup the instructions in braces. For example, here, we have a block, starting with an opening brace and ending with a closing brace. Inside, we have declared a Scanner-type variable keyb, an int-type variable i and a double-type variable x. We can use them, for example, to read the value of i, the value of x or to print the value of x, the value of i. These variables belong to the block where they have been declared. You can see that, in Java, blocks have great autonomy, meaning that they can contain their own variables. Now to a simpler example: we have a block controlled by an "if" instruction. The block thus begins here and ends with the closing brace here. In this block, we declare an int-type variable j, which we can then use inside this block. Once we have exited the block, starting from this point, we cannot use the variable j anymore, since it belonged to the block. Variables declared in a block, like this, are only accessible within this block, we say that they are variables local to this block. We can also declare variables outside any block, inside the class we are currently working with. We call those, variables global to the class. In the object-oriented programming course we will differentiate "instance variables" from "class variables". However, this would now take us a little too far. One advice we can give you is to declare your variables the closest possible to their utilization. What does it mean? Let us take an example. Let us reuse the previous example with the block here controlled by an "if" instruction. Inside it, we use the variable j and we suppose that we have no use for the variable j after this block. In this case, we will write the declaration the closest from its utilization, namely just when we need it, just before the first line where we will use the variable j. In this same situation where we need here a variable j, which is not used anymore after the block controlled by the "if" instruction, if we declared this variable here, before the block, it would be valid. However, it would not have been in accordance with the principle of declaring variables the closest to their utilization. Since this variable j, would have been declared outside the block, it could still be used later. The idea is to declare it as close as possible to the place where we use it. This notion of a place where we use a variable or rather of a place where we can use the variable, is what we called the "scope". The scope of a variable is all the lines of code where this variable accessible, where it can be used. Let us take an example. We have here a first "if" instruction, controlling its own block of instructions. Then, we have a second "if" instruction controlling its block of instructions aswell. In the higher level block, we will declare a variable j. In the deeper block, we will declare a variable k, which we will use. The scope of k, that is, all the lines where k is defined will be the block of the second "if" instruction. In the meantime, the scope of j, that is all the lines of code where j is defined, usable, is this block here, where j has been defined. Here, you can see that j and k have different scopes, indicated by these two blocks. A question we could ask is, what would happen if we had declared a variable j inside this block? In Java, such a thing is not allowed; it is not valid to have two variables with the same name in the same scope. For example, if we had declared, in the second block, a variable j. we would encounter a compiler error informing us that it is forbidden, that there is name ambuiguity. Therefore, in Java, it is forbidden. You cannot have, in the same scope. two different objects with the same name. However, it is possible in other programming languages. A particular scope case, which you should definitely be familiar with, is the scope of variables in iterative loops, "for" loops. Let us take an example. We have here a "for" loop, declaring an int-type variable i. This variable can of course be used inside the block. the scope of i will indeed be the block controlled by the "for" instruction, including the condition and the increment statement. However, this variable i will be local to the "for" loop, to the iterative loop. After the end of the block controlled by the "for" instruction, we won't be allowed to use this variablle; it is local to the "for" iteration.