 In the previous segment, we discussed the assignment statement further or rather we discussed how values can be reassigned and especially reassigned inside a loop to generate sequences and do interesting computation in general and we also discussed some operators such as plus plus and star equal to. Now we are going to discuss one idea which is related to how you write programs and how you define variables or rather where you define variables. So some technical terms first, the code that you write inside braces is called a block. So far you have seen blocks along with repeats. So a repeat is repeat then in parenthesis the count of how many times the repeat has to be followed by a block which gives you the body. But you can define blocks otherwise also and inside blocks also you can define declare variables. So here is how I might choose to write the summing program slightly differently. So in this summing program the main difference is that this term variable has been defined inside, otherwise the entire program is the same. So this program is equivalent to the previous program but it really executes slightly differently and it really says that look I want a term variable and it is being used over here so I might as well put the definition of it close by. So I can quickly tell that it was not initialized to anything and that it is an integer variable. So this is the device for bringing the definitions close to the uses. So to that extent the program becomes a little bit more readable. But the execution of this code is a little bit tricky. So how the definitions in a block is secured well the rules are as follows. So a variable which is defined inside a block will get created every time the control reaches the definition. Then all the variables defined in a block are destroyed every time control reaches the end of the block. So this term variable which we just saw will get created at this point and then destroyed again at this point. So this as you can see does not really hurt us because it really is useful only within this region. So you read in a value and you add it to S. S stays around because S is not defined inside this. S was defined outside. So the rule of getting destroyed does not apply to S. But whatever value we read in we actually got it into S already. So we do not care if term gets destroyed. But note that so this means that this program is going to work exactly like the way we wanted to. S will accumulate the values that get typed. It will sum the sum will get placed in S. But notice that there is one more implication that the term variable term will not be available over here. So in some sense it is sort of a private or a local variable for this loop. That is in fact also a term that often gets used. The term is now made local to this loop, to the body of this loop. Now when you talk about creating you may think that look is not that a lot of work. But in this case creating is not really much work or is not really any work. Basically the compiler from that point onwards starts using a certain piece of memory which it has anyway. Compiling is also not any work because the compiler stops using that piece of memory. But yes in some other cases which we might see later creating variables inside might have some cost. And for that reason you may choose to not have variables defined inside but rather have the variables defined once and for all outside if that is indeed what you want. But in this case it is a good idea to define the variable inside because it really says that that variable term is not really important for the final result. It is something that is being used only locally whereas S is an important variable. It is a variable which is more global. Alright now once we have mechanisms to define variables at different places we need to carefully understand what a certain variable name refers to. So if a variable is defined outside a block then it can be used inside the block just as the variable S was defined outside and it got used inside the block. However, this can happen only if no variable of the same name is defined inside the block. If a variable of the same name is defined then from the point of definition to the end of the block the newly defined variable gets used. So what is happening is that the new variable is said to shadow the old variable. The region of a program where a variable defined in a particular definition can be used is said to be the scope of the definition. So the scope of a variable defined inside a block starts at the point of the definition and ends at the end of the block. Well, why do we care about all these things? So in some sense programming is also like writing text, say you are writing a long document. So in a single English language document you might write let X denote in several paces. So if you write it on page 5 and on page 7 then there is this implicit understanding that the X on page 5 is different from the X on page 7. So essentially you can say that the scope of the definition on page 5 is maybe from that page 5 to page 37 or something like that or maybe it is from that page 5 to the end of the chapter or maybe even the end of the section. So in English language the scope is not very formally specified, it is sort of left as understood but in programming since the computer has to do something with it we have to be really careful and talk about the scope. Then furthermore if you do not have an intervening X denote then you can say that maybe the two X's are the same. Of course you really do not want the same X to be used so far apart or at least if you are using it so far apart then you will warn in an English language document. But in a program unless there is an intervening X and if the X is not inside a block which is like a chapter where the scope ends the two X's really can be considered to refer to the same concept or in case of the program the same variable. So this is the motivation why we are being so finicky about scope and shadowing. If you do not use the same names then this really does not apply but you will see that it does make sense to use the same names and so this discussion will actually come in useful. So here we will take an example. So we have a variable X, so if you print it out then 5 will get printed and this X will refer to that definition. Now if you print it again, again 5 will get printed but now if you write int X equals 10 since it is a new block we have started a block this definition is actually allowed. If we had not started a new block then this definition is actually not allowed. C++ will say that look you already have a variable called X, how can you define another variable? But since we have started a new block C++ knows that you can have another variable. So now if you print X it will print 10. So say the block ends over here and suppose over here you print X in this case 5 will get printed. Why? Because the scope of this X was limited to this region and over here or this point is in the scope of this definition so 5 will get printed. So when you come out the old value of X name X will start referring to this variable over here. Now I will leave you with a question if instead of this int X equal to 10 suppose I wrote X equals 10 then what would change? I would like you to think about this and then write this program and check your answer. I am not going to tell you the answer. So some remarks we have come to the end of this lecture and I want to summarize what we have done in this. So first we define variables as regions of memory in which we are going to store values. Variables have type and this type is used to interpret how the bits stored in that region are going to be interpreted. Then your advice to choose variable names so that they describe the purpose for which the variable is defined and we said that when we use a variable then that name if it appears on the left hand side of an assignment actually refers to the variable itself, it refers to the region of memory. On the other hand if it appears on the right hand side then you really do not care about the memory but you are saying give me its value. So there is this sort of dual view or the name is interpreted depending on its context and you should keep this in mind or maybe you already have this in mind. But anyway some more remarks expressions in C++ are similar to those in mathematics except that the value may get converted from integer to real or vice versa and truncation might happen. Truncation may also happen when values get stored into a variable and sequence generation and accumulation are very common idioms increment decrement operators and compound assignment operators are also commonly used. Then we said that variables can be defined inside any block, variables defined outside a block may get shadowed by variables defined inside and these are basically the main ideas covered in this lecture and at this point we have seen some rather interesting programs that you could write and indeed by now you have come to a point at which you can write several quite interesting programs and we will see these programs in the next lecture. Thank you.