Lecture Transcript Loops and Assignment Expressions

Lecture Transcript Loops and Assignment Expressions

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 C++ 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 for loop 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 semicolons 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 semicolon 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.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    5 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us