In a previous episode, we have discussed control statements, that is, C++ instructions letting data influence, choose or repeat, the executed processing. These control statements work along blocks. These blocks impact the use of variables. This impact is called the "scope of a variable" ; and this is what we will study in this episode. In C++, instructions can be regrouped within blocks, independently of any control statement. All it requires is a pair of curly braces bording a sequence of instructions. For example, we start a block here, declare an int-type variable called i declare a double-type variable called x, print a message asking the user to enter the values of x and i and finally printing the result on-screen. Now the closing curly braces signify the end of the block. In C++, blocks have much autonomy. They can delcare their own local variables, as we have seen previously. Local variables can even be declared in such a block, assiociated with an "if" instruction. For example, this variable j, used in this here block. This variable is local which means that once we reach the end of the block, we cannot use j anymore. A local variable can only be used in the block where it has been defined. Such a variable, defined in a block is called a local variable. The variable j, for example, is a variable local to this block here, controlled by the "if" instruction. Variables declared outside of any block, even outside the main - this is perfectly allowed - are called global variables. However, we will not give you examples of global variables. Indeed, a good practice rule is never to declare global variables, never ever! Why? Because, precisely, global variables are accessible from anywhere in the program. Thus, any instruction, anywhere in the program, can, for example modify this variable. It then becomes extremely tedious to keep track of the value of this variable. And that is why you are never to declare global variables. For that matter, we advise you to declare variables the closest to their utilization. In other words, the declaration line should be as close as possible to the line where this variable will be used for the first time. For example, in the previous "if" example, we had a variable j, we used inside the block of instructions controlled by the "if" and we had declared j in the previous block. Since j was not used in this section here, we should have moved this line closer to the variable's usage. j is only used in this block, and we assume that we do not use j anywhere else in the program. Therefore, such an instruction, declaring the variable outisde the block, does not follow the recommandation to declare as close to the usage as possible. We will thus move the declaration closer to the first instruction using j. Of course, we consider that j is only used within this block and nowhere else. This notion of the location where a variable can be used has a name: it is called the "scope of a variable". The scope of a variable is defined as all the lines of code where this variable is accessible; where we can use it. Let us take an example. We here have two nested "if" instructions, each one with its own block. Here the block of the first "if" instruction . And here the block of the second "if" instruction . In the block of the first "if "instruction, we declare a variable j. Thus, we can use this variable anywhere in the block where it has been declared. So, we can use j here, or here. We could also use it in this block here. However, once we exit the block concerned, we cannot, here, use j. Similarly, we will declare a variable k, here, closer to its utilization inside its own block. Thus, we can use k here, or here in this block. But we will not be able to use k afterwards. We say that the scope of k is this block here, the block where it has been declared. The scope of j is this block, where it has been declared. In C++, we can have variables with the same name but a different scope. Therefore, we need rules in order to know which variable we're talking about. Those are called the "scoping rules" (or "scope rules"). In C++, the scoping rule is very simple, we resolve to the nearest scope. Now what does it mean? Let us take an example We have two blocks, here. The first one is controlled by a higher level "if" instruction. Inside it, we declare a variable j. We will call it "j 1" or "red j". Here, we have a second block controlled by another "if" instruction. Inside it, we declare another j variable. By the way, this is not a very good idea and we do not advise you to adopt these practice in your programs. We're merely doing it here for explanation purposes. So, we have a second variable j; let's call it "j 2" or "blue j". The scope of this j is all the lines of code where it has been defined, declared, where it can be used. It is the block where it has been declared. The scope of the higher level j is all the lines of code where it is usable: it is the whole higher level block. So, in the deepest block - the blue block - the name j is ambiguous. Does it refer to the higher level j? Or does it refer to the lower level j? According to C++ scoping rules, we resolve to the nearest scope. Thus, j here refers to the j corresponding to the nearest block. The j that has been declared in this very block. The higher level j is perfectly valid here, but it is masked inside this block, and cannot thus be summoned. It will only reappear at the end of the block where "blue j" was declared. In the deepest block, "blue j" masks "red j". This was to illustrate the C++ scoping rules. However, what you should remember from this explanation is to avoid using multiple times, the same variable name: dodge ambuiguity. Indeed, we do not write programs for machines only. If this were the case, we would still write them in assembly language. We use "evolved" languages so that our fellow humans may understand, maintain, correct or transmit our programs. For that matter, avoid all ambiguity and strive to render your programs as clear as possible : do not call different things with the same name. A specific case of scope you should know is the scope of variables declared within an iterative loop, a "for loop". In this "for loop", we have declared a variable i. The scope of this i is the block controlled by the "for instruction", aswell as the condition and increment statement of the "for loop". However, the scope of i, does not reach any further. Starting from there, the variable i cannot be used anymore. Let us now finish with an example, which you should not encounter unless you call different things with the same name, which you shouldn't. This is merely to check if you are now familiar with the scoping rules. We have here a complete program. In the main, we declare an int-type variable i and intialize it to the value 120. Then comes a "for loop" iterating around a variable i, going from 0 to 5. We print the value of i in the loop. And, here, print the value of i at the end of the program. The question is: what does this program print? Let us examine the program step by step. First of all, we declare a variable i and initialized it to 120. Then, we enter the loop, declaring another variable i, initialized to 0. We test the condition: "i less than 5". Now which i is it? It is of course the i related to the loop; this i here. Thus, this value is indeed less than 5. We can enter the loop and print the value of i. Again, which i is it? The i with the closest scope is the i related to the loop, that is, this i here. Consequently, this line will print the value 0. Now we execute the increment statement. Once again, which i is it? Resolving to the closest scope, it is, as before, the i related to the loop. This i will thus go from 0 to 1, via this "++i" instruction. Now we retest the condition. We reach this line of code and test the condtion: "is i less than 5? " Yes. Thus, we print the value of i, that is still the i realted to the loop, which is 1. Now we execute the increment statement. i goes from 1 to 2. We test the condition again. It remains true. And we print the value of i, 2. And so on with 2, 3, 4. Now what will happen once i contains 4? We reach the increment statement, i goes from 4 to 5. The condition is now false. Indeed i is not less than 5, 5 is not strictly less than 5. Since the condition is false we exit the loop. We reach this point, at the end of the loop. Now we execute the instruction, following the loop, printing i. Which i is it, though? According to scoping rules, this i is the main. The i belonging to the level we're in. Do not forget that the i from the loop has its scope reduced to the loop. Thus, this i does not exist outside the loop. Therefore, the i we're talking about is the i from the main block. The starting i. It is now obvious that we will print 120.