<<

Lecture Transcript Loops and Assignment Expressions

Hello and welcome back in this lecture we’re going to look at the use of assignment expressions. This is a new kind of expressions. We look at their use in loops and also at other places in ++ programs. Here’s a quick recap of some of the relevant topics we already studied. We have looked at Iteration idioms and programming, we’ve looked at the fact that they are sometimes necessary and they certainly provide a convenient way of programming repetitive actions. We’ve 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’ve 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’re going to take a closer look at for loops. In particular, we are going to see - how assignment expressions and its variance 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 in C++ looks something like this. There is a part of the program before the iteration and then there’s the 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 to separate these three parts, semicolons do not denote end of the 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 at the end of this last part, which are the instructions that had to be executed at the 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 we must have an expression here. So does this is 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+b*c for an initialization expression or an update expression. What exactly would be initialized by the value of a+b*c? What exactly would be update by the value of a+b*c?

So at first sight it looks like there is some difficulty in implementing initialization and updation with expressions. Because it’s not clear what is to be initialized and what is to be updated when

1 I write an expression like this. However in C++ 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 assignment operator, these expressions have side effects, which are the actual assignment as it happens in an assignment statement. So let’s look at an example, suppose I have this assignment x assigned y plus z,

Now, if I put a semicolon after this this becomes an assignment statement and the value of the expression y+z is evaluated, and this value is assigned to the variable x. However, if I don’t use the 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 this statement as assignment statement. So the side effect here is that - the value of the expression y+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 assignment operator. So in this case, it is the same as the type and value of y+z.

So now, if you go back and look at our for statement, when we write programs like this with the 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 is a statement, which is in incrementing the value of count, but remember this semicolons are not be used as the demarcators at the end of an executable statement. They are just separating the three parts of the for loop and these can now we 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 an- other 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 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 + 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 precedence than assign(=). So, b +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 side effect which is a is assigned the value of the expression to right hand side of equal sign(=), which is b+c. And the type of this

2 entire assignment expression is the type of the expression that appears to the right hand side assignment operator. So therefore it is same as the type of b+c. And similarly, the value of this assignment expression is the same as the value of b+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 operator that we’ve 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-one 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+1 that will be evaluated first, then b assigned this assignment expression c assigned a+1 that will be evaluated next, and finally a assigned this entire assignment expression which is b assigned, c assigned, a plus-one will be evaluated finally. And if you work through 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+1.

Therefore, the type and value of this assignment expression is also the same as that of (a+1). And therefore, the type and value this assignment expression will also be the same as that of (a + 1). Now just like we have this simple assignment operator, there are some special assignment operators that are used in C++. 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 one.

The only difference with this expression is that - in this case the value of this expression x++ is the same as that of x before incrementing. So x++ 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++, then x++ here is being treated as an expression with the post increment operator. If the value of y before this statement is 10 and the value of x before the statement is executed is 2, then this entire expression x++ will evaluate to the value of x before it’s incremented, so it will evaluate to 2. Whereas value of x gets 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 ++ before x and once again it is similar to x is assigned x+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 ++x and if the value of y before this instruction is executed is 10 and if the value of x before its executed is 2. Then the value of this expression is the same as the value of x after implementing it, so the value of 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, you can also have decrement operators. So we can have post decrement like x - -, which is similar to x is assigned x -1. But it’s value will be the same as that of x before decrementing and here we have a simple example that is worked out.

3 You can look at it and you’ll be able to easily realize how the values of x 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 -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 += (y+z), this is just a shorthand for x is assigned x + (y+z), or I could write x -= (2*w) which is the same as x is assigned x - (2*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 compond 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++ 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’s 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 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’s 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 effect 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+2 and the value of this entire expression will be the same as the value of this right most expression which is z+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 3 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 expression will be the same as that of right most one in it.

So in summary, we used the assignment statement as both a statement and as an expression, and we looked at various variance of the assignment statement, and we saw their usage in loops, and other places too in C++ programs.

4 Thank You

5