 Hello and welcome. In this lecture today, we are going to look at the assignment statement and arithmetic expressions. Here is a quick recap of some of the topics we have seen earlier. We have seen the structure of a simple C++ program. We have seen variables and type declarations for these variables and we have also seen how to give names to variables and functions in a C++ program. In this lecture, we are going to look at the assignment statement and we are going to look at in a bit of depth arithmetic expressions that arise in assignment statements. Here is our friendly program with sums two numbers and outputs the result. In this program, I have highlighted over here this particular statement which basically says that C is assigned the value of a plus b. So the general form of an assignment statement is a destination then the equal to sign and then expression. However, note that when I read out this assignment statement, I did not say C equals a plus b. I said C is assigned the value of a plus b. We will see the difference between equals and assigns in a few minutes. So what does an assignment statement do? It computes the value of an expression and stores it in the destination. The destination for our purposes now is going to be a variable and of course a variable has a declared type and later in the course, we will see that this destination of an assignment statement can also be more advanced constructs beyond variables. Now this equal symbol in a C plus plus assignment statement is not the same as equality in mathematics and this is very important to keep in mind. For example, C assigned C plus 1 is a meaningful statement in C plus plus. Here I am saying that compute the value of C plus 1 and store it in the variable C again. However, this is not a meaningful statement in maths because of every value of C, C cannot be equal to C plus 1. Similarly, this mathematical equality a plus b equals C is a perfectly meaningful statement to make in maths. However, it is not a meaningful statement to make in C plus plus because the destination of an assignment statement in C plus plus has to be a variable for now. Now if you look at this assignment statement, the right hand side of the equal sign which is also called the right hand side of the assignment is an arithmetic expression. An arithmetic expression in general refers to values of variables. For example, here we have a as well as b. It refers to operators. Here we have the plus operator and it evaluates to a value. Now for every value that is C plus plus program computes, we must have a type. The type basically tells the compiler how much memory to allocate to store the value and it also tells the computer how to interpret the bits stored in those memory locations. So, since an expression evaluates to a value and every value must have a type, so an expression must have a type. Now normally the destination of an assignment statement for example C here and the type of the expression on the right hand side of the equal sign will match. For example, here we have the type of C as int and the type of the expression a plus b as we will shortly see will also be int. So, normally these two types are going to match the type of the assignment and the type of the expression. Now how do we write arithmetic expressions in C plus plus? This is the usual way we write expressions in algebra. For example, I could have variables a, b, c. I could have different kinds of operators, addition, subtraction. This is the multiplication operator denoted by a star. Division operator denoted by slash and this is also called the remainder operator. For example, if I write 5 percent 3, it means the remainder of 5 when divided by 3 which is 2 and I could use these operators, all of these are binary operators here to form arithmetic expressions like a plus b, a minus b, a divided by c, a percent b and so on. Now what is the data type of a plus b? Basically I am asking how many bytes should be allocated to store the value of a plus b in memory and how should the stored bits in those memory locations be interpreted. Now in general, the data type of an arithmetic expression depends on the data types of the various operands in the expression. So, in this case it will depend on the data types of a and b. So, if a and b are both integer, then all of these expressions a plus b, a minus b, a star b, a divided b, a percent b, all of these expressions will have the data type int. A general rule of thumb when calculating or determining the type of an arithmetic expression is this that the type of the expression must be at least as expressive as the types of various operands and no more. What do we mean by this? Suppose I have a variable whose type is float and another variable b whose type is int. Now float is considered more expressive than int because I can express all kinds of integer values as floating point numbers but not necessarily the other way round. So, now if I write expressions like a plus b, a minus b, a star b, a divided by b, one of the operands is float, the other is int. So, the type of the expression must be at least as expressive as the types of operands. So, in this case it must be at least as expressive as float and it need not be any more expressive than what is required by this rule that the type must be at least as expressive as the types of operands. So, in this case the type of the expression is all going to be float. On the other hand if I look at this expression 2 star b, here b is an integer, 2 is an integer constant. So, the type of this expression must be at least as expressive as the types of operands that is both of them are int. So, this type of, so this expression can have the type int whereas if I have this expression 2.0 star b, b is int 2.0 is a floating point constant, then the type of this expression must be as expressive as the types of its operands. So, here it has to be float. Here is another example, if I have a which is double, b which is float and c which is int, then double can be thought of as more expressive than float, double can represent all that float can and perhaps something more. Float is similarly more expressive than int. So, if I write this expression which involves all of a, b and c, then the type of this expression is going to be double at least as expressive as the types of each of the operands and no more. So, now if I look at this simple c++ code fragment, here I have 3 integer variables a, b and c. I have assigned the integer constant 1 to a, assigned the integer constant 2 to b and I have assigned the value of a divided by b to c. Now, the type of a divided by b will be find the type which is at least as expressive as the types of operands, here both the operands are int. So, the type of a divided by b is going to be int and therefore, the value of a divided by b that is computed here is going to be an integer value. Since a is 1 and b is 2, 1 divided by 2 has an integral part which is 0. So, therefore, the value of a divided by b will just be the integer part of 1 over 2 that is 0. On the other hand, if you look at this program, here a is integer b and c are float, a is assigned the integer constant 1, b is assigned the floating point constant 2.0. So, when I do a divided by b, the type of this expression has to be float because here I have an operand which is float and therefore, the value of a divided by b which is the value of c can be 0.5 because float can represent fractions. Now, let us look at how we read expressions when they are written out in c++. For example, consider this expression a plus b star c plus d. Now, should I read it like this, that b star c should be evaluated first and then a and d should be added to it or should a and b be added, c and d be added and then they should be multiplied or should it be the case that you add a and b then multiply it with c and then finally, add d to it. So, which of these is represented when I write this arithmetic expression in c++. Now, this depends on operator precedence and in c++ star or multiplication is higher precedence than plus. So, when I write this expression, this is what I mean that the star operator must be evaluated first, it should be applied on whatever operands appear for it and then I must add a and d to it. Now, similarly, let us look at this a plus b minus c plus d. So, the question is, does this represent this expression or does this represent this expression? Note that these two expressions may or may not evaluate to the same value. In particular, this expression is not the same as this expression in arithmetic, but we want to know what should this expression be interpreted as, should it be this or should it be that? Now, in c++, the addition and subtraction operators, they have the same precedence and when we have operators with the same precedence, what do we do? We go from left to right and we evaluate the operators as we encountered them going from left to right. So, in this case, I will first evaluate a plus b, then I will evaluate a plus b minus c and then I will add d to it. So, this is also called left associative that is we are associating operands to the operators as we go from left to right among operators with the same precedence and we will see that there are exceptions to left associative operators, but these we will encounter later in the course. Similarly, just like plus and minus had same precedence, multiplication, division and the remainder operator, they have the same precedence and their precedence is actually higher than that of plus and minus. These three operators, multiplication, division and remainder are also left associative. So, for example, if I write an expression like this, what we really mean is that all the three operators of the same precedence, they are left associative. So, first evaluate a percent b, then divide by c and finally, multiply by d. So, this might appear to be different from the usual laws of algebra that you follow, but this is how expressions are interpreted in c++. So, the best practice is really to use parenthesis to specify the exact order in which you want different operators to be evaluated unambiguously. So, parenthesis can be used to override default operator preferences. For example, a plus b star c plus d using parenthesis, I can specify that first add a and b, then multiply c with it and finally add d or I could say first multiply b and c, then add a and d to it. Paranthesis can also be used to form complex expressions. For example, in this case, I have written down a complex expression with nested parenthesis. We are going to evaluate it from the innermost parenthesized expression outwards. So, first we have to evaluate 4 plus x, let us say x is a variable, then we have to evaluate 3 divided by 4 plus x, then we have to evaluate 2 plus 3 divided by 4 plus x, then 1 divided by that quantity and finally add 1 to it. An important point to remember is that parenthesis are not to be confused with braces or square brackets. For example, if you write an expression like this, where you use braces instead of parenthesis, you will get a compilation error. So, in summary what we looked at in this lecture is the assignment statement in c plus plus. We looked at arithmetic expressions, how to obtain their types, values and we also looked at the use of parenthesis in arithmetic expressions. Thank you.