You are on page 1of 4

When it comes to these operators, the expression should be evaluated from left to right.

It
doesn’t matter which operator comes first. Go through the explanation and try to
understand.

Example :

If you have a / b % c * d :
• The step is : a/b first, then the answer % c, then the answer * d.

If you have a / (b % c) *d :
• Of course you have to do the ones in the brackets first.
• Then arrange the expression a / answer * d
• Next do a / answer first then only * d

For + and –

The calculation is from left to right. It doesn’t matter which operator comes first.

Example 1

int a = 1, b = 2, c = 3
if given the formula y = ++a – b + c--

so the workings will be :

y = ++ a – b + c--
++1 – 2 + 3
2–2+3
y=3
For * , / and %

The calculation is also from left to right. It doesn’t matter which operator comes first.

Example 1

int a = 1, b = 2, c = 3
if given the formula y = ++a % b * c--

so the workings will be :

y = ++ a % b * c--
++1 % 2 * 3
2%2*3
0 *3
0

NOTE : don’t do * first ----- it’s wrong. Do from left to right, follow the expression, do
% then only do *)

So you will get :

y=0*3
y=0

Example 2

int a = 15, b = 4, c = 2
if given the formula y = ++a / 3 % c * 2 % 3 * b++

so the workings will be :

y = ++a / 3 % c * 2 % 3 * b++
16 / 3 % 2 * 2 % 3 * 4
5 %2*2%3*4
1 *2%3*4
2 %3*4
2 *4
8
The example below shows you a combination of all arithmetic operators

int a = 15, b = 4, c = 2, p, q, r;

p = b / 3 % --a + c % 2;
q = c-- * a / (3 + b) % a;
r = (10 - c) + a % ++b * 2 - c;

We will calculate the first expression first :

p = b / 3 % --a + c % 2
= 4 / 3 % --15 + 2 % 2 (first step is to replace the variables with the values)
= 4 / 3 % 14 + 2 % 2 (next the ++ or -- is calculated. At this point a has
become 14 because of --a)

= 1 % 14 + 2 % 2
=1+2%2
=1+0
=1

At this point, a has changed value to 14, b is still 4 and c is still 2

Next expression:

q = c-- * a / (3 + b) % a
= 2-- * 14 / (3 + 4) % 14 (replace the variables with values)
= 2 * 14 / (3+4) % 14 (++ or -- is calculated. c is still 2 because of c--)
= 28 / 7 % 14
= 4 % 14
= 4

At this point, a is still 14, b is still 4 but c has changed value to 1

Next expression:

r = (10 - c) + a % ++b * 2 - c;
= (10 – 1) + 14 % ++4 * 2 – 1
= (10 – 1) + 14 % 5 * 2 – 1 (you cannot do * first, you must do from left to
right. So do % first)
= 9 + 4 *2–1
= 9 + 8 -1
= 16

You might also like