Operator Precedence and Associativity Worksheet Solution

CS 116

Oct. 23, 2000

Assuming the following values:

int x = 11;
int y = 6;
int z = 1;
char c = 'k';   /* 'k' = 107 */
char d = 'y';   /* 'y' = 121 */
fully parenthesize and evaluate the following expressions. Assume that the ASCII character representation is used.

  1. x > 9 && y != 3
    ((x > 9) && (y != 3))
    (true && true)
    true
    

  2. x == 5 || y != 3
    ((x == 5) || (y != 3))
    (false || true)
    true
    

  3. !(x > 14)
    (!(x > 14))
    (!false)
    true
    

  4. !(x > 9 && y != 23)
    !(((x > 9) && (y != 23)))
    !((true && true))
    !(true)
    false
    

  5. x <= 1 && y == 6 || z < 4
    (((x <= 1) && (y == 6)) || (z < 4))
    ((false && true) || true)
    (false || true)
    true
    

  6. c >= 'a' && c <= 'z'

    'a' = 97, 'z' = 122

    ((c >= 'a') && (c <= 'z'))
    (true && true)
    true
    

  7. c >= 'A' || c <= 'Z'

    'A' = 65, 'Z' = 90

    ((c >= 'A') || (c <= 'Z'))
    (true || false)
    true
    

  8. c != d && c != '\n'

    '\n' = 10

    ((c != d) && (c != '\n'))
    (true && true)
    true
    

  9. 5 && y != 8 || false
    ((5 && (y != 8)) || false)
    
    Invalid expression: can't convert 5 to boolean.

  10. x >= y >= z
    ((x >= y) >= z)
    
    Invalid expression: can't convert boolean ((x >= y)) to int.



Thomas P. Kelliher
Wed Oct 25 13:51:49 EDT 2000
Tom Kelliher