Presentation is loading. Please wait.

Presentation is loading. Please wait.

Precedence of all operators: AssociativelyoperatorPrecedence Innermost first()1 Right to left++ -- !2 Left to right* / %3 Left to right+ -4 Left to right.

Similar presentations


Presentation on theme: "Precedence of all operators: AssociativelyoperatorPrecedence Innermost first()1 Right to left++ -- !2 Left to right* / %3 Left to right+ -4 Left to right."— Presentation transcript:

1 Precedence of all operators: AssociativelyoperatorPrecedence Innermost first()1 Right to left++ -- !2 Left to right* / %3 Left to right+ -4 Left to right >=5 Left to right== !=6 Left to right&&7 Left to right||8 Right to left= += -= *= /= %=9

2 Consider the following C++ declarations and assignments. Int i,k;float x,y;char c;bool test; i=4;k=10;x=6.7;y=4.4;c=‘8’;test=true; For each of the following expressions, state whether it is valid or not. If an expression is valid, evaluate it. a)x * c / y + k – c * c / 3 b)c == ‘7’ || i + x != y && x – y > k c)k / i % k + x – y d)test && !test || !test e)! (y+3) > x && (x – 1 > y + k) f)(k + 6 ) / i / i % k g)i * i > k * 3 && c != ‘7’ && ! test 1 Revision د/محمد ناجى

3 h) floor(x + k) – ceil(y + k / 2) + y / i/ 2 i)++i / k + x – 3 * y + abs(k – 2 * i) j)i / k * y / i + x – 8 k)fabs(y – x + 1) + floor(sqrt(3 * i + k + 3) l)x * x / y – z * z + k – 7 m) k % 10 + y > x * i / (k + 6) n)c < i + k && x + i < y + k o)sqrt(floor(y – k) + ceil(i– x)) p)k / i + k / float(i) – int(x) / int(y) 2 q) ++i / k++ % j + x – i * k + 7 r) i / y / k % j <= x * i % k s) ( x + j = i – k ) && ( y + k < i – x )

4 Solution: (hint: ascii(‘8’)=56) a)x * c / y + k – c * c / 3 1 2 3 4 5 6 1) 375.2 2) 85.2727 3) 3136 4) 1045.33 5) 95.2727 6) -949.727 3

5 b) c == ‘7’ || i + x != y && x – y > k 12 3 4 5 6 7 1)10.72)2.33)false4)false5)true6)false7)false 4

6 c) k / i % k + x – y 1 2 3 4 1) 2 2) 2 3) 8.7 4) 4.3 5

7 d) test && !test || !test 12 3 4 1) false 2) false 3) false 4) false 6

8 e) ! (y+3) > x && (x – 1 > y + k) 123 4 5 6 7 1) 7.4 2) 5.7 3) 14.4 4) false 5) false 6) false 7) false 7

9 f) (k + 6 ) / i / i % k 1 2 3 4 1) 16 2) 4 3) 1 4) 1 8

10 g) i * i > k * 3 && c != ‘7’ && ! test 23 1 4 5 6 7 2)16 3)30 1)false 4)false 5)true 6)false 7)false 9

11 h) floor(x + k) – ceil(y + k / 2) + y / i/ 2 1 2 3 4 6 7 8 9 1) 16.7 2) 16.0 3) 5 4) 9.4 5) 10.0 6) 1.1 7) 0.55 8) 6.0 5 9) 6.55 10

12 i) ++i / k + x – 3 * y + abs(k – 2 * i) 2 3 4 1 5 6 7 8 1) 5 2) 10 3) 0 4) 0 5) 0 6)13.2 7) 6.7 8)-6.5 9 9) -6.5 11

13 j) i / k * y / i + x – 8 1 2 3 4 5 1) 0 2) 0.0 3) 0.0 4) 6.7 5) -1.3 12

14 k) fabs(y – x + 1) + floor(sqrt(3 * i + k + 3) Invalid. Missed bracket. l) x * x / y – z * z + k – 7 Invalid. z is undeclared. 13

15 m) k % 10 + y > x * i / (k + 6) 1 2 3 45 6 1) 16 2) 0 3) 26.8 4) 1.675 5) 4.4 6) true 14

16 n) c < i + k && x + i < y + k 123 4 5 6 1) 14 2) 10.7 3) 14.4 4) false 5) true 6) false 15

17 o) sqrt(floor(y – k) + ceil(i – x)) 1 2 3 4 5 6 1) -5.6 2) -6.0 3) -2.7 4) -2.0 5) – 8.0 6) error 16

18 p) k / i + k / float(i) – int(x) / int(y) 1234 5 6 7 8 1) 4.0 2) 6 3) 4 4) 2 5) 2.5 6) 1 7)4.5 8)3.5 17

19 q) ++i / k++ % j + x – i * k + 7 1 2 4 3 5 67 1) 46 2) 23 3) 3 4)92 (46*2) 5) 11.8 6) -80.2 7) -73.2 18 i = 45 ; j = 4 ; k = 2 ; x = 8.8 ; y = 2.0 ; c = ‘w’; test = true ;

20 (r) i / y / k % j <= x * i % k Invalid. x * i is float. The two operands of the mod operator must be integer. (s) ( x + j = i – k ) && ( y + k < i – x ) Invalid. (= sign is not allowed)


Download ppt "Precedence of all operators: AssociativelyoperatorPrecedence Innermost first()1 Right to left++ -- !2 Left to right* / %3 Left to right+ -4 Left to right."

Similar presentations


Ads by Google