So you look at the different arithmetic operators there are not a few rolls that may come into play 2
when using arithmetic operations. 3
Knowing these rules is very important. 4
And let's look at what these rules are. 5
We can have instances where we come across expressions such as this which have more than one otic Medek 6
operation. 7
So it's a compound expression. 8
But the question is in what order are the subexpressions evaluated. 9
That is what the first ad 5:01 line or would do for us multiply 2 and 5 and then add the result to 3 10
dollar has a couple of set rules called operator precedence rules for this kind of compound expression 11
. 12
First rule is that multiplicative operators have higher precedence over any two operators multiplicative 13
Operators Group multiplication division and Margulis whereas any two operators include addition and 14
subtraction with just one example expression but looks something like this. 15
That is to an Freiler multiplied first as multiplication has higher presidents once the first rule is 16
applied. 17
Then the second rule is that operators within the same group will be evaluated from left to right and 18
by applying this rule on our example expression we would have something like this. 19
That is all left to what operators belong to the same group which is additive two operators group and 20
so they are evaluated from left to right. 21
So 9 or are that first then three is subtracted from the desert and a new reserve is finally added to 22
do in to fight. 23
Finally note that you can use pattern sources to change the order of evaluation. 24
So in our example we can force the expression to be evaluated this way. 25
He had 3 and 2 and 5 or 9 will be added separately but the sum of three and two will be multiplied with 26
5 due to rule 1 and the result will be proctored from five plus nine. 27
Next is open and promotion rule. 28
That is it has something to do with promoting an operator. 29
Let's see what it is. 30
It states that operate in smaller than and that is byte shot or car are promoted to and hit Here's an 31
example. 32
Here is the Ajume that the two operands of by date type. 33
Then they will be first promoted to end before any operation is applied. 34
And here is another example. 35
The carburetor. 36
I recall that card Streater I said 16 bit unsigned integer whose range is zero to the power of 16 minus 37
one. 38
So if God is used an IT operation it will also be promoted to an end. 39
So in this example. 40
Characters IA and B are promoted to the respective decimal values. 41
97 98 and you of 16. 42
So the final result will be 195. 43
Shortly we will also be a quick demo where we will see examples of current use getting promoted to and 44
next let's look at another rule which we can refer to as same type operations rule and this rule states 45
that if both our parents are in long float our bubble then the arithmetic operation is carried out in 46
that type and also evaluated the value of that same day. 47
Here are a couple of examples where the operands are integers in the second example. 48
Even though the characters are at this point fine the value return will be zero as spawn 5 is not an 49
integer. 50
So point five was truncated to zero. 51
Previous rule was called same type operations stored on the operations but of SAME TIME NEXT rule is 52
about operands being of different types and so is referred to as mixed type operations. 53
Let's see what it states. 54
It states that if both operands belong to different types then the value of the smaller type is automatically 55
promoted to larger type. 56
And here is the order of promotion. 57
Note that if you have long and flautas operands longest promoted to float even though long 64 bit untrue 58
disturbed little bit. 59
Here is a similar example as in the previous light did BORTOLI draws 1 and 2 are integers and so the 60
result was also an integer which was 0. 61
Here in both examples one of the operands is a double and so due to this particular rule the interior 62
operand is promoted to double and so the final result is a double value point five. 63
Here is how an expression involving a car and float are evaluated got is first promoted to and the operand 64
promotion rule. 65
Then it is promotor to float DUPERE this rule. 66
And finally both the floats will be added on. 67
Here is a very nice example in this expression. 68
First division operation is applied as both operators are from the same group that is belong to multiplicative 69
Operators Group and hence are evaluated from left to right. 70
So that's the operator president's rule in action. 71
Next since 9 and 5 are in the first same type operations rule is applied and the result of division 72
will be an integer value 1. 73
No we are left with integer under double N since integers are smaller they are tight due to mixed type 74
operations. 75
When it gets promoted to double and the final result will be double. 76
In general the largest data type in an expression determines the type of the final result. 77
Now as mentioned earlier let's do a quick them off Oper in promotion rule a new method called Cartaya 78
promotion has been added to basics demo class which can be downloaded from the resources section. 79
Let's look at the program now. 80
OK here is the numerator cark by promotion and it's invoked from the main method and there are a bunch 81
of print statements here. 82
So let's just go ahead and run this program and we will look at the Outlook and then we will discuss 83
the print statements. 84
Ok ok so I'm already in the basics directory so let me just run it. 85
It's already compiled. 86
So here is the output. 87
Let's just look at that last statement because it was already covered in the in the slight. 88
So it's just printing it just adding two characters a and b and the output is 195. 89
And that's because off the operand promotional. 90
So that's because here we have two characters on both the characters as we discussed will be promoted 91
to integers so the corresponding integer values and UAD of 16 is 97 98 and so you'll see the final value 92
as the same as 1 95. 93
OK so that's what is happening even even though you're adding two characters here. 94
Now the first one is very simple it's simply printing the carbon carbon is basically is assigned a decimal 95
50 so decimal 50 is equal to you'd be up in uk of 16 its value. 96
So if you see here it's printing do. 97
We already know that from our characters demo. 98
So it's just printing too. 99
So carbon is basically to write on the second print statement here. 100
It's subtracting carbon from 73 so it's 73 minus carbon carbon is basically two. 101
So it gets promoted to án And so the equal in decimal value is once again 50 so it's going back to 50 102
. 103
So it would be 73 minus 50. 104
And so we are seeing 23 as the final 73 minus 50. 105
So in the next print statement we are doing we are subtracting character 3 from carbon. 106
So carbon has two and this is three. 107
So. 108
So both of them are characters essentially and both of them get promoted to two. 109
And so carbon will get promoted to end and the value will be 50. 110
And since this is three it will get promoted to 51. 111
So 51 us 51 us minus 1. 112
And that's what we see here. 113
OK. 114
So that's the operant promotion run. 115
And so basically we look at that four kinds of rules. 116
One is to operate or presidents run then operate in promotion rule then same type operations rule and 117
Mixter type operations. 118
OK. 119
So these are very important. 120
So please do understand them very well and you can also expect some questions in the quest for him from 121
this particular lesson. 122
OK. 123
And you can also download this program and play around really familiarize yourself with with the with 124
this lesson. 125
OK. 126
And thank you. 127
And happy coding.