In the last lecture, we were introduced to the precedence among the operators and we had seen that parenthesis has got the highest precedence followed by unary minus then multiplication division module and modulus have got the same priority addition and subtraction are having the next priority. And for operators of the same priority if in an expression there are more than one operators which are of the same priority then evaluation is done from left to right. Only one thing we did not mention and that is if we want to change the precedence of the evaluation then we can always do it using parenthesis ok using parenthesis because we know parenthesis is the highest priority one and its overriding scenario alright.
So let us. Look at some examples for this which will make the idea clear. Here are some example arithmetic expressions.
Here you can see a plus b times c minus d divided by e. Now, according to my precedence rule. This is equivalent to A plus as if I have if I had done it by hand computation if I had done by hand computation how would I have done there is a chance of confusion somebody could have done A plus B and the whole thing multiplied by C etc.
To you know even in school level in order to avoid such confusions we use parenthesis and this is equivalent to. this scenario that A this B and C are parenthesized and D divided by E are parenthesized. So, first this will be done then this will be done why B into B times C first. because, it is left and this is right right.
So, first this then this then addition and sub. So, let us see how will it break it down again we will have first b times c will be done. followed by d times e then we have got suppose this result is x and this result is y then my expression is a plus x minus y.
Now, out of these again these two they are of the same precedence. So, first this will be done suppose this is z minus y that is how the computation will go on. Here, what is the significance of this? The critical point to note here is this part.
Here there is an unary operator. Therefore, during computation what it means the computer will automatically assume that these parentheses are there, alright. If you had not wanted that then you better write it in.
some other way put it in some other way so that the confusion is not there. The computer will not allow the compiler will generate the code in a way that it will be treated as this A times. first minus b will be done then this one is the highest precedence next highest precedence. So, this will be done then multiplication between these two will be done.
Now, I am sorry I am sorry here normally say if I had done this what would have happened. First of all I would be doing minus b and by capital letter I am writing some constant value all right say b is right now say 5. So, first after computing this it would be a times minus 5 because b was 5 plus. Suppose, D was 100 and E was ah D was 100 and E was 9. So, what is my ah modulus 1 right. So, then I will I will be now at this point I have got D modulus. E minus F.
Now at this point which one has got the higher priority this and this. Now since I have put a parenthesis here therefore if I put a parenthesis here then this will be done first. Otherwise this would have been done first because this multiplication has got the same priority as this and we resolve left to right ok. So, similarly here a minus b plus c plus d.
Now, here these are having the same priority left to right. Now, I want it to be done in a particular order. Therefore, I have put the parenthesis in such a way that a minus b should be done first, then a minus b plus c will be done first.
then D will be added to that that would have been done anyway why because since these are of the same priority that would be done left to right. So, first this would be done then this would have been done then this would have been done. have been done ok either way here also the same I can write it in this way but that is the precedence is being shown in the form of bracket but here this is an example where I have used parenthesis to override the precedence the precedence between this multiplication and this modulus was the same but just by putting this parenthesis there.
I have forced this to be computed first. Similarly, here now normally given this, this would be done first, then this would have been done, then this would have then the addition should have been done, but I have overwritten that by saying that ok first you do a a plus b. you multiply C and D multiply by E that is there anyway and then do the addition, but this is actually overriding the normal precedence.
So, you should you will with a little bit of practice will. will make some mistakes, but then gradually with practice that will go away and all of you will be able to write it fine. Now, we are coming to a very important concept of this arithmetic expression evaluation integer arithmetic. What does it mean? It means that when the operands and in arithmetic expression are integers then the expression is called an integer expression and the operation is called the integer arithmetic.
Integer arithmetic always yields integer values it will always yield I am sorry. it will always yield integer values ok. So, it will be clearer when we take some examples. On the other hand, a real arithmetic is arithmetic operations involving only real or floating point values. For example, here You can see this expression 1.0 divided by 3.0 multiplied by 3.0.
Now all these are operands. Now here is I would like to introduce a term when I say a plus b then this plus I call it as an operator. And, these I call as operand on which the operation is being done. Now, in real arithmetic all the operands are floating point numbers right. Now, sometimes the floating point values are rounded to the number of significantdigits because it may be 7.5976 like that you can go on it is often rounded.
to the number of significant digits permissible in a particular machine, we get an approximation of the results. Now, one thing that we have to remember is that the modulus operation is not defined over real operands. So, I am not allowed to do 3.5 modulus to 0.1 that is not allowed ok.
The modulus operation is allowed only for integer arithmetic. Now, we can also have mixed mode arithmetic where some of the operands are integers and some are float ok or real. In that case we call it a mixed mode arithmetic expression.
If any of the operand is of real type then only real arithmetic is performed and the result is a real number. This example will make it clear. Now, you see here look at the first one.
25 divided by 10 both the operands this operand and this operand both these operands are integers. Therefore when I divide by divide them I get an integer quotient or integer result whereas If one of the operands was integer and one was real, if at least if even one is real then the result will be real. So, what is happening here?
This is becoming like 25.0 divided by 10.0. So, that will become 2.5. Now, here on the other hand since both are integers the result will be integer. So, let me just put in one twist on this. Suppose now here 25 divided by 10 is actually what?
is the if I divide it is also 2.5 right 2.5, but since this operator is nothing, but an integer division. So, it will find out the quotient and the quotient is an integer the quotient is 2. If I had done 25 modulus 10 what would my result be? the result would be 5 another integer all right.
So, we could understand what is meant by mixed mode, real mode and integer mode of arithmetic. We will see some more things later. Now, there are some problems for value assignment.
For example, in assignment operation we actually take the expression value and assign it to to a variable or we assign a particular variable to another variable. For example, we could have as we have done say something v is u plus f times t. This is an expression value is going first it will be computed and the value will go to v. So, the value is going there right. On the other hand I could have done v assigned u that means.
The value of u is going to this variable, here also the value is going. Here the value of an expression, here just a value of a variable. But the most important point is that the data right hand side should be compatible with that of the left hand side ok.
Now if for example I say v assigned u and u is a float and v is an int then I may have problem. What type of problem can I have? Suppose u is 25.7, I assign this to v. So, v can only take an integer value. So, in most of the cases you will get an error. Why because you know that depending on the computers, depending on the different architectures integers are often given 2 bytes and floats are given 4 bytes.
So, you are trying to. pack in a large number I mean a 4 byte number into a 2 byte space the space is not there so the errors can come in some cases ah maybe ah they will cut it out and represent it as ignore this part and you will not understand it will just assign 25 to v that is also an error that is creeping in. Now, this is this error will go unnoticed if the compiler catches it which is mostly the case in that case you will you will the compilation error will occur and we will understand that it is not.
For example, as I said the 4 byte floating point number is not allowed to be assigned to a 2 byte integer variable. So sometimes what we do this is a new concept called type casting. Look at this example.
Here. We have defined x to be integer. So, x has got 2 bytes suppose I have I show ah 2 bytes for x and r is a floating point number which has been represented through 4 bytes. So, Now, sometimes what we do we type cast this thing 2 times r will be what 2 times r will be a float because r is real and so, if one of the operators are real then this part will be a real arithmetic, but then that result I am casting back to int. So, this operation is called the type casting operation that means, whatever I have I have casting that to fit in.
in a way so that it can fit in as an integer. Here is another example say perimeter has been defined as double that means, it can have twice the floating point space pi is a float 3.14. Now, here you see this pi we had encountered pi earlier, but here this pi is not the constant which has been defined as defined, but pi is a variable which has been initialized at the time of declaration.
And r is an initialized. integer. Therefore, when I am computing perimeter. is twice pi r. So, what what am I what am I actually doing?
I am trying to compute 2 pi r right the circumference of a circle. So, twice 2 is real now pi now r perimeter is double. So, I want to have perimeter with a larger accuracy.
Therefore, I first make r to be double. I typecast this integer to a double type, I typecast this float to a double type, then I multiply and get the perimeter ok. So, this is another very powerful operation which we often need for scientific computation. So, typecasting of a floating point expression to an integer variable as is shown here and the typecasting to double has been shown here we have already explained that.
Now, we have now looked at arithmetic expressions, how to form arithmetic expressions and arithmetic expressions are formed using arithmetic operators. Besides arithmetic operators, there is another very important type of operator called the relational operator. A relational operator is used to compare two quantities ok.
Let us see for example, this symbol very familiar denotes is less than. This symbol which is also familiar shows is greater than. This symbol is less than or equal to this. Sometimes in our normal course we write it in this way. But in a computer we cannot write in this way.
We have to write it less than equal to. Alright. Similarly, greater than equal to unlike the greater than equal to that we used to write in this way, we have to write here greater than or equal to.
Now, this means greater than or equal to that is well known to us. Next, here is another operator this requires some description. discussion is equal to. Now, earlier this is very important to note and this is a very source of a very common error and common mistake.
Typically when we say in our normal arithmetic x equals y we meant that the value of x and the value of y are the same right. But we have seen that in C language this actually means the variable x is being assigned the value of y right. So, how do I compare whether this 2 variables are equal or not for that C provides this technique of using 2 consecutive equality sign to show x whether x is equal to y all right. We will come to this in a moment. There is another operator that is not equal to again that is compared to what we had in school we used to write something like this in C we have to write it with this exclamation mark followed by equality this is means it is not equal to alright.
Now, a very important thing comes into the play. Whenever I am using this relational operators, what do I get? If I write x less than y in an now we know that what are these x and y? These are two operands and what is this? This is an operator.
So, when I carry out some I will be getting some result. So, if I had done x plus y and suppose x was 5 and y was 7, then x plus y would have given me what 12 would have resulted into 12, 5 plus 7. Now if I do this x less than y that means 5 less than 7. what will this give me? It will any relational operator gives me only true or false 1 or 0. So, when I say x less than y and x is 5 y is 7 it is true.
Therefore, x less than y in this case will return 1 ok. If I write y less than x if I write y less than x it will return me false because y is 7 and x is 5 then this is be false this would return 0 ok. Similarly for greater than y greater than x will return me in this case in for this particular value set y greater than x will give me true 1 or in conceptually we can say true 1. but this true cannot be represented as T R U E in a computer it is written in C it is represented as 1 less than equal to in this case what would be have what would have happened Y less than equal to X is it true or false is it true or false. than or equal to.
In this case is y less than x? No. Is y equal to x? No. Therefore, it will be false.
Y greater than or equal to x? What would have happened? Y is not greater than is greater than x, not equal to x.
Therefore, it immediately becomes true. Y is Y equal to X? In this case, no. So, it will return false.
Y is not equal to X. Is it true or false? It is true.
Therefore, it will be 1. So, what we could observe is we can form expressions using relational operators, but these expressions only return the values true and false. We will see that these relational operators if you recall in our flow chart discussions we often had some decision boxes shown as diamonds and in that decision box we used to say x greater than 10 or say we used to write something like n equal to 3. Yes or no, right. We had taken one path for yes, one path for no and that can be captured very easily using such relational operators as we will see. Now here are some other examples. I have already given you enough examples.
10 greater than 20, true or false? It is false. 25 less than 35.5 yes it is true, 12 greater than 7 plus 5. Now, here if one of the operands is an expression that will first be evaluated.
So, the arithmetic operators and the expression have higher priority over relational operators all right. So, here what will happen 7 plus 5 will be first computed. So, that is 12 is 12 greater than 12 no.
So, it is false ok. So, when arithmetic expressions are used on either side of a relational operator the arithmetic expressions will be evaluated first and the arithmetic expressions will be evaluated. in accordance with the precedence that we have already defined.
So, if there will be something like this a plus b greater than c minus d that is equivalent to first I will compute a plus b then I will compute c plus d and then suppose a plus b is 10 and c plus d is 17 then this will lead to false ok. First these will be computed then the relation operational operator will be computed. So, here is a quick look at a at an application of this.
Suppose I want to implement the flowchart something like this, I have read x and y and I want to do x greater than y, if so yes, I will be say I will print x is larger otherwise I will print Y is sorry otherwise I will print Y is larger right. So, that sort of situation how will that translate into C language here you see this part we have not discussed, but it is very intuitive you can understand if x is greater than y I print. what is larger?
Dash is larger then what will come here? The value of x. So, I will print say x is 10 and y is 15 this is x and this is y then what will be printed here? 10 is not greater than 15. So, this part will not be executed will come to here because x is not greater than y.
So, we are taking this no path and we will print dash is larger and in dash what will come the value of y that is 15. So, what will be printed is 15 is larger. So, that is how we can implement this sort of decision box. and when I have got a choice between paths based on the decision that is being that condition is being computed using relational operators which this conditions are telling me which path I will take is is the situation true if so I will take this path otherwise I will take this path this is known as the this is the purpose of the relational operators ok.
So, next class we will start with logical operators. Today we have discussed about arithmetic expressions, how arithmetic expressions are formed using arithmetic operators and what are the very important thing that we have discussed today is. or in the earlier lecture also lecture that what is the precedence till now what we have discussed is that what is the precedence of the operators in an arithmetic expression and what is a relational operator. And we have seen that relational operators have got lower priority than arithmetic expressions.
So, if on either side of the relational operator there are arithmetic expressions those will be evaluated first in accordance to the arithmetic operation priority ok. Next we will come to another very important concept that is logical operators. Thank you.