20
Precedence of all operators: Precedence operator Associativ ely 1 () Innermost first 2 ++ -- ! Right to left 3 * / % Left to right 4 + - Left to right 5 < <= > >= Left to right 6 == != Left to

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

Embed Size (px)

Citation preview

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

Precedence of all operators:

PrecedenceoperatorAssociatively

1()Innermost first

2++ -- !Right to left

3* / %Left to right

4+ -Left to right

5< <= > >=Left to right

6== !=Left to right

7&&Left to right

8||Left to right

9= += -= *= /= %=Right to left

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

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

1Revision /ناجى محمد د

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

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 + 7r) i / y / k % j <= x * i % k

s) ( x + j = i – k ) && ( y + k < i – x )

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

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

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

b) c == ‘7’ || i + x != y && x – y > k

1 2

3

4

5

6

7

1)10.7 2)2.3 3)false 4)false 5)true 6)false 7)false

4

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

c) k / i % k + x – y

1

2

3

4

1) 2

2) 2

3) 8.7

4) 4.3

5

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

d) test && !test || !test

1 2

3

4

1) false

2) false

3) false

4) false

6

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

e) ! (y+3) > x && (x – 1 > y + k)

1 2 3

45

6

7

1) 7.4

2) 5.7

3) 14.4

4) false

5) false

6) false

7) false

7

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

f) (k + 6 ) / i / i % k

1

2

3

4

1) 16

2) 4

3) 1

4) 1

8

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

g) i * i > k * 3 && c != ‘7’ && ! test

2 3 1

4

5

6

7

2)16

3)30

1)false

4)false

5)true

6)false

7)false

9

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

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

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

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

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

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

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

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

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

m) k % 10 + y > x * i / (k + 6)

12 3

45

6

1) 16

2) 0

3) 26.8

4) 1.675

5) 4.4

6) true

14

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

n) c < i + k && x + i < y + k

1 2 3

45

6

1) 14

2) 10.7

3) 14.4

4) false

5) true

6) false

15

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

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

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

p) k / i + k / float(i) – int(x) / int(y)

1 2 34

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

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

q) ++i / k++ % j + x – i * k + 7

1

2

4

3

5

6 7

1) 46

2) 23

3) 3

4)92 (46*2)

5) 11.8

6) -80.2

7) -73.2

18i = 45 ; j = 4 ; k = 2 ; x = 8.8 ; y = 2.0 ; c = ‘w’; test = true ;

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

(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)