 In the previous segment we discussed the assignment statement and rules for expression evaluation. So in this segment we are going to look at reassignment. This looks simple enough, basically changing the value given to a single variable. But this actually turns out to be a rather powerful and very, very common operation and it needs to be understood properly. So the rule for evaluating expressions is that when a variable appears in a statement its value at the time of the execution is used. So suppose I write p equal to 3, q equal to 4, r and I write r equals p plus q. So the values of p plus p and q at the time of executing this are 3 and 4. So what is going to happen is that those values are going to get used and 7 will be stored. So if I print r out then 7 will get printed. Now suppose I use r, I write r equals p times q then the current values of p and q will be used and 12 will get stored into r. Now if I print out r the current value of r which is 12 will get printed. Now reassignment can lead to some interesting expressions. So suppose I have in p equals 12. What happens if I write p equals p plus 1? Is it even legal? So in c plus plus it is actually legal. So the rule for executing such a statement is actually something that we have already discussed. So the usual rule says first evaluate the value on the left hand side. So the current value of p is 12, you add that to 1 so you get 13. And then whatever the result is you store into the variable on the left hand side. So 1 is added to 12, the current value of p and the result 13 gets stored in p. So basically this will cause the value of p to increase by 1. Now this statement is sometimes found confusing by people because writing p equal to p plus 1 is nonsensical in mathematics and the equal to operator is very very strongly linked in our minds to mathematics. So this says that the equality in mathematics and in programming is quite different. In mathematics it is more like equality. In programming it means this says take the value of the expression on the right hand side and put that value into the variable whose name appears on the left hand side. So basically equal to in c plus plus is different from the equal to in math and you should be aware of this very much. Now reassignment turns out to be quite interesting with repeats. So here is a program I am going to have int i equals 1, repeat 10 times and let us say I write c out i endl and then I write i equal to i plus 1. So what happens when I execute this? So in the first iteration when the loop is when the repeat statement body is entered then I will have value 1. So because of that 1 will get printed and the i equal to i plus 1 statement will change i to 2. In the second iteration what is going to happen well the value that i now has is 2. So 2 will get printed and i equal to i plus 1 will change that 2 to 3. So at this point I will have the value 3 and if you keep on going in this manner in the 10th iteration 10 will get printed and i will change to 11. So something quite interesting has happened over here this set of statements has been able to print out the numbers between 1 and 10 for us. So basically this EDM we can call a sequence generation EDM. So if you want to ever generate the sequence of numbers 1 through 10 then this EDM will do it. Which EDM? Repeat 10 times and i equal to i plus 1. The c out i is not really an essential part of the EDM we put that there only because we wanted the sequence to be seen. Now suppose you put in this statement turtle sin and we put in the statement forward i times 10 and write 90. What do you think this does? So notice that in this green portion we are not changing the value of i at all. So I will have the same values in each iteration as it did earlier but this means that the distance by which the turtle goes forward is going to be different in each iteration. So first it will be 10, next it will be 20, then it will be 30 and so on. So this is sort of like drawing a square but the side length is increasing. So what do you think happens? So this is going to cause something like a rectangular spiral. Let us take a look at this. So this program also I have keyed in and it is called spiral.cpp. So here is the program and I guess what I have not put in over here is this weight. Let me indent this properly because otherwise it does look a little confusing. So is it what we have written over there? Yes it is, it is also going to print i but it is also going to cause movement. So let us see what this does. So let me compile it and let me run it. So it did a spiral just as we expected. Now you should realize that we do not really need to just generate sequence 1 through 10. You can generate other sequences also. So it is an easy exercise for you to generate the sequence 1, 3, 5, 7 and so on or you can also generate the sequence 1, 2, 4, 8, 16. Basically you can generate these sequences by making slight modification to the previous program. Maybe change what you add, maybe instead of add do something else. So just try it out. Now using the assignment statement or rather the reassignment statement inside a repeat loop can allow you to do other things as well. So here is a program which reads 10 numbers and adds them together. So we declare variables in term and s. So s is going to be 0, s is going to have the sum. Maybe we should actually have called that variable sum but that is okay. So we are going to do repeat 10 times and so we are going to have c in term. So the value goes into term and then we add this value to s. So whatever value we get we are going to add to s. So notice that initially s is 0 but in the first iteration of this loop s will change and whatever you receive from the keyboard is going to be added to s. What happens in the next iteration? In the next iteration whatever the user types from the keyboard the second time around will be stored into the variable term and that will get added to s. So at the end of two iterations s will have the sum of two variables. So at the end of 10 iterations s will have the sum of the 10 variables. So if you type s at the end of it you will get the sum of the 10 variables. Now s is serving like an accumulator. It accumulates by summation whatever values are read from the keyboard and so this EDM we might as well call the accumulation EDM. So you could do other kinds of accumulation as well. You could accumulate by using products for example or you can take the maxima and keep accumulating the maxima. So all kinds of other things are possible. Now an interesting thing happens we can compose these two EDMs and write a program which calculates n factorial. So here is our basic program. So this is the first EDM. So this is the sequence generation EDM and it is generating the sequence 1 through n. Now what do we need to calculate n factorial? Well we want that sequence but we just do not want to leave that sequence alone. We want to multiply all those numbers. So this is easily done by writing a new variable out called n fact. It is initially 1 and then we simply write n fact equals n fact times i. So the first time around n fact will get multiplied by 1, the second time around n fact will get multiplied by 2, 3, 4 all the way till n. So at the end if we print n fact we will get n factorial printed. So the EDMs as you can see are quite powerful we can sort of mix them up as well. Now because the assignment statement and the reassignment statement is so commonly used C++ defines some additional operators. So here for example i equal to i plus 1 is something which appears very frequently and so in C++ it can be abbreviated as i plus plus. So plus plus is an operator and it is a unary operator and it can be written as i plus plus which simply means i equal to i plus 1. So it is an increment operator it is unary and similarly you can have minus minus which means j equal to j minus 1 and minus minus is the decrement operator. Now plus plus and minus minus are actually quite tricky. Plus plus and minus minus can be written after the variable or before the variable. So I can write plus plus i or I can write i plus plus so both are allowed. Now furthermore I can also write expressions such as k equal to plus plus i and k equal to i plus plus. So these expressions are legal and as it happens they produce different results and so here plus plus i and i plus plus are different. If I just write i plus plus and plus plus i in isolation then they change the value of i and they change the value of i in a similar manner but here they produce some different results as far as k is concerned. Now such assignments are described in the book but I believe and many programmers believe that such expressions are really difficult to understand. So it is like using really complicated words when you do not need to use them. So it is said that when you speak you should use simple words as simple words as possible. So what has happened over here is that these variables were defined in the language but more and more people are saying do not use these complicated forms like k equal to plus plus i and k equal to i plus plus. So indeed in this course we are not going to use them and I will recommend to you that do not use them in your career as well. So just stick to the simple forms same for minus minus. Now the fragment of the form sum equal to sum plus expression also occurs frequently. So c plus plus allows such fragments to be shortened to sum plus equal to expression. So you can also have star equal to minus equal to represent sum star expression or sum equal to sum minus expression. So if I write x equal to x plus equal to z then really it is x equal to x plus z so x is going to become 12 because x is 5 z is 7. Similarly I can write star equal to. So here if I write y equal to y star equal to z plus w note that z and w do not change. I mean this is not something new that I am telling you but just if you get confused remember that it is really exactly equivalent to writing y equal to y times z plus w and this when you evaluate this expression z and w do not change. So here are some exercises for you to try out based on the material that we have seen in this segment. So what have we discussed in this segment? Well we said that the value of a variable can be changed and assignment such as i equal to i plus 1 or j equal to j times 2 are allowed they are useful for generating sequences then once you are able to generate sequences you can compute expressions such as n factorial and several other uses are also there. And because we do things like i equal to i plus 1 and j equal to j times 2 operators such as plus plus and star equal to or star plus have also been provided. So we will stop at this point and continue later.