 In the previous segment we discussed how to define variables, initialize them, read into them from the keyboard and print. Now we are going to discuss the assignment statement which is useful for storing a value into a variable defined earlier. So you might have initialized it but you can change the value and that is what the assignment statement does. The statement has the form variable equals expression. So here is an example, I might have variables s, u, t, a, t, t defined earlier I should have such variables and if I have these variables I can write a statement s is equal to u times t plus 0.5 times a times t point t and what this does is what you might expect. So the text on the right hand side of the assignment is what is called an expression and it is a formula involving constants or variables essentially like you write formula in mathematics and in the execution the value of expression is calculated and that value is stored into the variable which appears on the left hand side of the equal to symbol. Now if expression contains variables then they must have been assigned values earlier, if they are not then it is wrong to write such a statement and if for example a has been assigned a value then the value used in place of a in this expression will be whatever that value is. So for example suppose u, a and t have values 1, 2, 3 respectively assigned before this statement is executed then when the statement executes then this expression will be calculated. So the value 1 of u will be used, the value 3 of t will be used and the value 2 of a will be used. So we will have the evaluation 1 times 3 plus 0.5 times 2 times 3 times 3 and the result of this 12 will get stored into the variable s. Now when you store a value into a variable then whatever value was there before, before you did the store goes away because after all the variable contains some capacitors and now you are saying look I want a different value to be stored in these capacitors. So basically whatever value was present is lost or it is also said that the new value overrides the old value. When you evaluate an expression of which contains references to variables then the values of those variables are taken from those variables but they continue to remain in those variables as well. So for example in this case while we will need to use the values stored in u, a and t just because we are using the values does not mean that they are going to be destroyed. So even after this statement is executed the variables u, a, t will continue to have those values that were given earlier. In fact this statement only changes the value of the variable s. Whatever other variables we might have defined those variables will remain as they are. Now when we write expressions there are several rules, some of which are slightly unusual but you will see that they are quite natural. The first rule is that multiplication must be written out explicitly. So you cannot write s equals ut plus 0.5 a, t, t while in mathematics if we want to write x times y we just write x, y this is not allowed on a computer. Simply because if we write u and t together then there is a question does it mean u multiplied by t or does it mean a single name ut of some variable. So because of such considerations the designers of C++ and many languages decided that if you want multiplication to happen you have to say multiplication. So you have to explicitly indicate the multiplication operator. So between u and t and between the other things that you want multiplied you must put a star operator. Now multiplication and division have higher precedence than addition. So this is like mathematics. So for example in this case ut will be computed then 0.5 a times t times t will get computed provided you had put in the multiplication operators and only after that will the addition be computed. So between multiplication and division the precedence is the same and between addition and subtraction also the precedence is the same. So operators of same precedence if they appear and you have to make a choice between them then the evaluation happens left to right. So the operator that is on the left side will get evaluated first. So we will see examples of all this. And inside expressions you can use parenthesis with the usual meaning. Now spaces can be put between operators and values if you like but of course if you have an identifier which consists of several letters you cannot split it with a space. So this for example is okay. So the first example there are no spaces but in the second example there are spaces between on either side of the equality symbol as well as the plus. So whatever you think is easier to read is fine. So that is the choice left to you. Some examples. So suppose we create these variables and initialize some of them. So now what happens if I write r equals x times y plus p times q. So in this expression we want to pick x and y first and perform their product. Then the next multiplication has higher precedence. So that product will be computed and finally the two products will be added. So since x and y have value 2 and 3 and pq have value 4, 5 we are going to multiply 2 times 3 plus 4 times 5 and get 26. So r will get the value 26 if you execute these two statements. If you execute s equals x times y plus p times q then notice that here the parenthesis are evaluated first. So y plus p will be calculated first. So the result will be 2 times 3 plus 4 times p or in other words s will get the value 70. If you write t equals x minus y plus p minus q the evaluation is left to write. So 2 minus 3 is evaluated first which will give you a minus 1 plus p or 4 will get added. So you will get 3 and then you will do a minus 5 so you will get minus 2. So minus 2 will go into t. If you write u equal to x plus w in this context this statement is a little questionable because there is it is not clear whether w has been defined before or not. So if w has not been defined before then this statement is wrong otherwise whatever the value of w is and it had better be better have been given a value that value will be added to x and the result will be stored in u. Now division is a little tricky and suppose we have these four variables and if I write u equals x upon y plus z upon y. So notice that x and y are both integers and z and y are also integers. So in this case c plus plus has somewhat unusual rule. So if the dividend and divisor are both integers then c plus plus will somehow produce an answer which is also integral. So the most natural answer to produce is to use the quotient. So basically the division will be performed and the remainder will be ignored. So in this case 2 by 3 will produce the quotient 0, 4 by 3 will produce the quotient 1 and so the answer will be 0 plus 1 or 1. So u will become 1 after this. And if you divide an integer by 0 then you will get error because within integers division by 0 is not defined. Now c plus plus allows numbers of one type to be stored into variables of another type and in such cases c plus plus tries to do the best possible it can under the circumstances. So suppose I have index float y and suppose I say x equals 2.5, well x is integer. I cannot really store 2.5 into it. So what is the best possible? So here c plus plus says that I am going to be simplistic and I am just going to take the integer part of it. You could have said why not round it but c plus plus does not do that. c plus plus just rounds it down or takes the integer part. So I am storing something into a floating point number. Again the value that gets stored will be in that scientific notation and furthermore if I have float y then that only has about 7 digits of precision. So 1, 2, 3, 4, 5, 6, 7, 8, 9 has 9 digits which cannot be represented. So probably something like 1.234567 or maybe even 1.23456 will get stored but the magnitude will be roughly right and the exponent will be 9. So what is going to be stored here is 1, 2, 3, 4, 5, 6, 7, 0, 0. So instead of 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 0, 0 will get stored. So this is sort of an inevitable consequence of using the scientific notation or using the float data type. So these remarks will apply also to the double data type but the only thing is that the double data type can store far larger number of digits something like 17, 18 digits can be stored. So in this case the representation will still be exact. If you had used a double. Now C++ also allows you to mix different kinds of numbers when you operate on them. So I could write an expression A some operator B where A and B have different types. So here are the rules that C++ uses to evaluate such expressions. So if A and B have different data types then they will be converted into the more expressive of those types. So what does that mean? Well basically integers are less expressive than floating point. So floating point can potentially represent integers exactly but integers can very rarely represent floating point numbers exactly. So that is kind of a reason to say that float or double are more expressive. And furthermore shorter types are less expressive than longer types. So something which uses 64 bits is certainly more expressive. So A and B you use then both will get converted to the more expressive data type, you perform the operation and the result will have the type of the more expressive type, some examples. So int n sides equal to 100 but anyway so suppose we have that and after that we write int i angle 1 equals i angle 2. And now if I write i angle 1 equal to 360 divided by n sides, so let us see what happens here. So 360 is int, n sides is int. So the result of division will be an int, integer division will be used and the result will be 3 because it is only going to take the question. So 3 will get stored into i angle 1 which is an integer. Then i angle 2 suppose it is 360.0 divided by n sides. So then what happens over here is a little tricky. So 360 upon n sides the numerator is double. So if you write any constant by default it is double. So the numerator here is double. So denominator is also converted to double. So 100 is converted to double then we do the division. So the result in this case will be 3.6. So the result of the expression on the right hand side 360.0 divided by n sides is actually going to be 3.6 now. However, but notice that i angle 2 is integer. So while storing only the integer part will get stored. So we are going to do the same thing when the destination, the left hand side variables are going to be floats. So suppose you write f angle 360 upon n sides. So what happens? So 360 and n sides are both integer. So the result for the expression is 3 and 3 will get stored into f angle 1. If on the other hand we write 360.0 divided by n sides then the result will be 3.6 and that will be stored in f angle 2. So if you are expecting 3.6 to get stored then that will get stored only in f angle 2. So this is something that you have to be careful about especially when you mix integers and floating point values. One solution to this which is kind of a simple solution but which often works is really just to work with the data type double. So it has 16, 17 bits of precision and it will not do this kind of truncation. So that is something that you can think about. But anyway very likely you will use doubles and ends mixed together and in that case you do need to know these rules. Alright, now when you use either double or float or the equivalent of scientific notation then there are some other things that you have to watch out for. Now suppose for example I define the variables w, y and Avogadro and I initialize y to 1.5 and Avogadro to 6.022 e23. Now what happens if I write w equal to y plus Avogadro? Well ideally what might happen is that I would have to write that entire Avogadro out in whatever 23 digits or so and then add 1.5 to it and the actual sum will be something like this. However, this actual sum is going to be stored in w which is a floating point number. So w can only accommodate 23 bits which is about 7 digits. So really only the first 7 digits will be considered. So everything after the 7th will get truncated. So to 7 digits of precision Avogadro is really the same as y plus Avogadro. So w will receive the truncated value that is Avogadro itself. So basically if you are adding a very small number to a very large number then the addition may not have any effect at all. So let us put together whatever you have learnt or sum of what we have learnt to write a simple program. So in this program we are going to declare, define two variables centigrade and Fahrenheit and you can guess what I am going to put into them. So first I am going to print out a message saying give the temperature in centigrade. So after that I will wait for the user to type in some value and the C in statement will cause that value typed by the user to be stored in centigrade. Now I can calculate Fahrenheit. So the formula is the Fahrenheit value is centigrade value times 9 upon 5 plus 32. So that is what I have written down over here. So as a result of this the variable Fahrenheit will actually get the equivalent value of temperature in Fahrenheit equivalent to whatever was typed by the user which we interpreted as a centigrade temperature value. And finally this statement is going to print out a message saying that in Fahrenheit the temperature is so and so. And we are putting an endel which forces the value to go out and of course an endel to a line to also appear. Now let me just ask you, do I need to write 9.0 or will this work? Well think about it for a second before you hear my answer perhaps. So here centigrade has been defined as a double variable. So when we do the first multiplication itself the result will be a double. So again when we do the division it will be a double divided by 5. But when you divide a double then the result is going to be a double and therefore we will not have any truncation and will indeed get the result calculated correctly. So I am going to stop the slides for a second and I am going to show you a demo of this program. So here I have the program that I showed you earlier all typed out. So I am going to now compile it. So I go back to my shell again and now I am going to compile it. So it has compiled and now I can execute it. So as soon as I execute it, as soon as I run it I get the message give temperature in centigrade. So we can give some temperature. So I guess if you want to check whether the program is behaving properly maybe we should type in some value whose Fahrenheit equivalent we know. So say we type in 40 whose Fahrenheit equivalent is 104. So indeed it does print 104 and so our program is in fact running correctly. So now I want to make some comments about expressions specifically where can expressions appear in the program. So basically expressions can appear on the right hand side of an assignment that we have already seen but in general you could say that expressions can appear wherever a plain number can. So for example in this code I can put numbers to initialize but I can also put expressions. So instead of putting in a number I have put in an expression. Instead of putting in a number over here I have put in an expression. So now the initialization happens left to right. So you will get initialized, T will get initialized, A will get initialized and then V and S will get initialized. So this is important because when you initialize V you are using the values of U, A and T. So because the initialization is happening left to right the correct values will be available over here and similarly for this. So for example if you define variables in this manner where you are referencing a variable which has not yet been defined in the left to right order then this is incorrect. To have this correct you should really write y equal to 2 first and then x equal to 5 times y. Now you could have printed just a number if you wanted or you could have printed a variable but you can also print out an expression. So this will cause this expression to be evaluated and that value will get printed. Similarly when you are passing an argument to a command you could place a value, a numerical value or you could place the name of a variable or you can put an expression. So before that value is actually sent off to that command the expression is evaluated and only the resulting value will get sent off. Now very often we might want to find the remainder and in C++ the remainder is found by using the operator percent. The percent is actually the character percent is actually the remainder operator. So x percent y evaluates to the remainder when x is divided by y and xy must be integer expressions. So for example I might write something like this int n equals 1, 2, 3, 4, 5, 6, 7, 8 d0 d1. So if I write d0 equals n mod 10, d0 will get the remainder of n modulo 10 so it will get 8. d1 I am writing as n by 10 divided by 10. So what will n by 10 be? n by 10 will just be the quotient when divided by 10 so that will be 1, 2, 3, 4, 5, 6, 7. But when taken modulo 10 again will get b7. So basically what is going on over here is that by using the remainder operator we have a way of extracting the digits of a number by taking remainder and dividing by 10 as many times as we want. So here are some exercises these are going to test whatever you have learnt in this segment. So what have we learnt? So we have learnt about assignment statement, we have learnt about how arithmetic happens when expression contains numbers of different types and we should also know that real numbers are represented only to a fixed number of digits of precision. And so adding very small values to very large values may not have, may have no effect. What we did not discuss but what you should read from book are things like overflow and representation of infinity. So we will stop this segment, we will end this segment here.