 Hello and welcome back. In this lecture, we are going to look at the use of assignment expressions. This is a new kind of expressions. We will look at their use in loops and also at other places in C++ programs. Here is a quick recap of some of the relevant topics we have already studied. We have looked at iteration idioms in programming. We have looked at the fact that they are sometimes necessary and they certainly provide a convenient way of programming repetitive actions. We have looked at three different constructs in C++ for implementing iteration. These are the while construct, the do while construct and the for construct. And we have also seen how break statements can be used in loops to jump out from the middle of a loop when certain conditions are satisfied. In this lecture, we are going to take a closer look at for loops. In particular, we are going to see how assignment expressions and its variants are usually used in for loops. And we are also going to look at comma separated expressions and their use in for loops. Now recall from the earlier lecture that a for loop in C++ looks something like this. There is a part of the program before the iteration and then there is a part of the program after the for loop. And in between is your for loop where you use the keyword for. And then there is iteration initialization, loop condition and instructions to execute at the end of every iteration. We use semicolons to separate these three parts. These semicolons do not denote end of executable statements. They are just separating the three parts in this for statement. Since they are just used for separating the three parts, we do not have a semicolon at the end of this last part which are the instructions that are to be executed at the end of every iteration. And of course, this is the main body of the for loop where we can have a block of statements. Now if you look at the C++ standard, it specifies that a for loop must look something like this. Instead of the initialization code, it says that you must have an initialization expression. And instead of instructions to execute at the end of every iteration, it says that you must have an update expression. So, this looks a bit strange. We wanted to do some initialization over here. For that, we needed some statements, but the standard says that we must have an expression here. And here also, we wanted to execute some instructions at the end of every iteration, but the standard says that we must have an expression here. So, does this appear nonsensical? We needed assignment statements both for initialization and update, but the standard says that we must have expressions there. Is this meaningful? What if I wrote a plus b star c for an initialization expression or an update expression? What exactly would be initialized by the value of a plus b star c? What exactly would be updated by the value of a plus b star c? So, at first sight, it looks like there is some difficulty in implementing initialization and updation with expressions. Because it is not clear what is to be initialized and what is to be updated when I write an expression like this. However, in c plus plus, it turns out that we can use the assignment not only as in a statement to actually assign the value of an expression to a variable, but you can also use an assignment as an operator to build an expression. And when we write such expressions with the assignment operator, these expressions have side effects which are the actual assignment as it happens in an assignment statement. So, let us look at an example. Suppose I have this assignment x assigned y plus z. If I put a semicolon after this, this becomes an assignment statement and the value of the expression y plus z is evaluated and this value is assigned to the variable x. However, if I do not use a semicolon and I want to use this as an expression, then this equals symbol is used as an operator in this expression. So, this becomes an assignment expression using the assignment operator and this expression like any other expression has a type and value, but this expression also has a side effect. The side effect of this expression or the side effect of evaluating this expression is exactly the same effect as you would get if you executed the statement as an assignment statement. So, the side effect here is that the value of the expression y plus z will get stored in the variable x and what is the type and value of this entire assignment expression? It is the type and value of whatever appears on the right hand side of the assignment operator. So, in this case it is the same as the type and value of y plus z. So, now if we go back and look at our four statement, when we write programs like this with a for loop where I say count assigned 1.0 as part of the initialization code and count assigned count plus 1 as part of the instructions that are to be executed at the end of every iteration. I could view this as a statement which is initializing the value of count 1 and I could also view this as a statement which is incrementing the value of count, but remember these semicolons are not to be used as demarcators at the end of an executable statement. They are just separating the three parts of the for loop and these can now be viewed as expressions with the assignment operator and which have side effects and the side effects are precisely the initialization and the increment, but in accordance with what the C++ standard requires us to do, we now have an expression here in particular this is an assignment expression. We have another expression here that is also an assignment expression, but in the process of evaluating this expression we are going to have the side effect where the variable count will be initialized to 1 here and here the value of the variable count will be incremented. So, now you see that when we look at a for statement with count assigned 1.0 and count assigned count plus 1 here these are not really assignment statements, they are assignment expressions with side effects which actually update the values of variables. Now well if you are going to look at assignment as an operator then you must talk about the precedence of the assignment operator. If I write an expression like this which has both the plus operator and the assignment operator how is this expression going to be evaluated. So, it turns out that the precedence of the assignment operator is lower than that of all the arithmetic and logical operators that we have studied so far. So, in this case if I write this expression plus will have higher precedence than assign. So, B plus C will be evaluated first and then the value of this expression will appear to the right hand side of this assignment operator and then this operator the assignment operator will be evaluated and when this assignment operator is evaluated we have a side effect which is A is assigned the value of the expression to the right hand side of the equal sign which is B plus C and the type of this entire assignment expression is the type of the expression that appears to the right hand side of the assignment operator. So, therefore, it is the same as the type of B plus C and similarly the value of this assignment expression is the same as the value of B plus C. Now, just like precedence if you want to treat assignment as an operator you also have to worry about its associativity. Now, unlike any of the operators that we have seen so far the assignment operator is right to left associative which means if I write an expression like this first of all plus has higher precedence than assignment. So, A plus 1 is going to be evaluated first now among the three assignment operators because it is right to left associative the right most one will be evaluated. So, this is the assignment expression C assigned A plus 1 that will be evaluated first then B assigned this assignment expression C assigned A plus 1 that will be evaluated next and finally A assigned this entire assignment expression which is B assigned C assigned A plus 1 will be evaluated finally and if you work through the rules for the type and value of an assignment expression that we just discussed in the earlier slide you will see that the type and value of this assignment expression is the same as that of A plus 1. Therefore, the type and value of this assignment expression is also the same as that of A plus 1 and therefore, the type and value of this assignment expression will also be the same as that of A plus 1. Now, just like we had the simple assignment operator there are some special assignment operators that are used in C plus plus. So, for example, we have special increment operators and in fact, there are two flavors of it, post increment which is used like this, you take a variable x and then you put plus plus. So, this plus plus denotes increment and because it appears after the variable x, we call it post increment. Now, when you have an expression like this with the post increment operator, this is very similar to having an expression like x is assigned x plus 1. The only difference with this expression is that in this case, the value of this expression x plus plus is the same as that of x before incrementing. So, x plus plus is an expression which will have the type and value same as that of x before incrementing, but it will have a side effect which will increment the value of x. So, for example, if I have an assignment like this, y is assigned x plus plus then x plus plus here is being treated as an expression with the post increment operator. If the value of y before the statement is 10 and if the value of x before the statement is executed is 2, then this entire expression x plus plus will evaluate to the value of x before it is incremented. So, it will evaluate to 2, whereas the value of x will get updated, incremented as a side effect of evaluating this expression. So, after this instruction is executed, y will have the value 2 which is the value of this expression and x will have the value 3 as a result of the side effect of evaluating this expression. Similarly, just like post increment, we can have pre increment where we put plus plus before x and once again this is similar to x is assigned x plus 1, except that the value of x here, the value of the expression here is the same as that of x after incrementing. So, in this example, if I said y is assigned plus plus x and if the value of y before this instruction is executed is 10 and if the value of x before it is executed is 2, then the value of this expression is the same as the value of x after incrementing it. So, the value of this expression is 3. So, y gets assigned the value 3 and x's value is incremented as a side effect when you evaluate this expression. Just like increment, we can also have decrement operators. So, we can have post decrement like x minus minus which is similar to x is assigned x minus 1, but its value will be the same as that of x before decrementing and here we have a simple example that is worked out. You can look at it and you will be able to easily realize how the values of x and y are obtained after the execution of this instruction and similarly, we could have pre decrement which is once again similar to x is x minus 1, but its value is the same as that of x after decrementing. So, in this particular case, since the value of y and x before the execution of this instruction at 10 and 2, when you execute this instruction, the value of this expression is the same as the value of x after decrementing. So, it is 1. So, the value of y gets assigned 1 and x is of course, decrement is a side effect. So, it also gets the value 1. Now, just like the increment decrement operators, we can have compound assignment operators which basically increment decrement the value of a variable by an expression. So, I could write something like x plus equals y plus z. This is just a shorthand for x is assigned x plus y plus z or I could write x minus equals 2 star w which is the same as x is assigned x minus 2 star w. So, this whole thing is an expression. It has the side effect and the type and value of this expression is the same as that of x after this assignment happens. So, similarly, I could have compound assignment operators with multiplication division and also remainder and the interpretation is exactly as you would expect. Now, with so many kinds of assignment operators, increment operators, decrement operators, we need to worry about the precedence and associativity. I do not want to go through this slide. You can read it at your leisure because this is just taken from the C plus plus standard which says this has the same precedence as that, this has higher precedence than something else. This is left to right associative, something is right to left associative and it is a lot of complication. The moral of the story here is that try not to mix increment decrement operators with other operators if possible. The precedences and the associativities are quite complex. These operators are provided as convenient idioms for incrementing and decrementing use them mostly for that purpose. Now, once we talk about assignment operators being used to build assignment expressions which can be used inside a for loop, it is also appropriate to talk about the comma operator which is basically used to build a big expression which has several other expressions within it. So, in places where we are only allowed to use one expression but we want to sort of have the side effects of multiple expressions, we can build a compound expression with the comma operator in this manner. So, in this expression for example, the different component expressions will be evaluated in left to right order. So, there will be two side effects, x will be incremented, the value of y will be updated to that of z plus 2 and the value of this entire expression will be the same as the value of this rightmost expression which is z plus 5 and similarly for its type. So, this is often used in initialization and update code for for loops. For example, in this example, you can see that there are two assignment expressions here separated by comma. So, this whole thing will be treated as one expression. So, in a for loop I can only put one expression here. So, I could do this similarly there are three update expressions or assignment expressions over here, but in a for loop I can only use one expression, but I could use all of these three by building a compound expression and this will evaluate to a single expression with two side effects and the value of this expression, the type of this expression will be the same as that of the rightmost one. So, in summary we used the assignment statement as both a statement and as an expression and we looked at various variants of the assignment statement and we saw their usage in loops and in other places too in C++ programs. Thank you.