Introduction to Programming (22C:16, 22C:106)

Homework 6: Due Thursday, 3/20/97

Solution:

Next to each valid expression you will find the value the expression evaluates to. Below that you will see a series of expressions that expose the order in which these operators are evaluated. For each invalid expression you will see a brief explanation of the error.

  1. !(sum - 10 < sum < sum + 10): False
    !(990 < sum < sum + 10)
    !(990 < sum < 1010)
    !(1 < 1010)
    !(1)
    0

  2. (sum % 10 == 0 && sum % 100 == 0): True
    (0 == 0 && sum % 100 == 0)
    (0 == 0 && 0 == 0)
    (1 && 0 == 0)
    (1 && 1)
    1

  3. !ch == '3': False
    0 == '3'
    0

  4. (ch = '4' && ch != 4): character with ASCII value 1
    (ch = '4' && 1)
    (ch = 1)
    1

  5. (ch != '03') || (ch == 'A'): Error, because '03' is neither a character constant, nor is it a string constant.

    However, on CodeWarrior 11 this is not an error and the expression evaluates as follows.
    (ch != '03') || (ch == 'A')
    (1) || (ch == 'A')
    (1) || (0)
    1

    More accurately, the second subexpression in the expression is never evaluated since the first subexpression is True and the expression ``short circuits'' to True.

  6. name < "Turning" && name > "True": True
    1 && name > "True"
    1 && 1
    1

    Note that the string class assumes a lexicographic ordering or alphabetical ordering on strings.

    The above expression produces an error on CodeWarrior 11 because the string being used does not contain functions that overload the operators < and >. This is unfortunate and is not true of most other implementations of the string class including Astrachan's string class.

  7. ((name > "turning") && (sum / (sum - 1000) == 0)): False
    (0) && (sum / (sum - 1000) == 0)

    Note that 'T' comes before 't' in the ASCII table and hence the subexpression ("Turing" > "turning") is False. Because of short-circuited evaluation, the expression evaluates to False as soon as the first subexpression evaluates to False. Hence, the second subexpression is not evaluated. If it were evaluated, it would cause a division by 0 error.

    The above expression produces also an error on CodeWarrior 11 because of the same reason that Expression 6 does.

  8. (log10(sum) == name.length() - 3): True
    (3 == name.length() - 3)
    (3 == 6 - 3)
    (3 == 3)
    1

    Note that log10() is a function defined in the math.h header file. Also note that length() is a member function of the string class that returns the length of the string object it is applied to.

  9. ch != ' ' || ch != 10 || ch != '3': True
    1 || ch != 10 || ch != '3'
    1 || ch != '3'
    1

    Note the use of short-circuited evaluation here.

  10. name + ch > name + "?": False
    "Turing3" > name + "?"
    "Turing3" > "Turing?"
    0

    "Turing3" comes before "Turing?" because '3' comes before '?' in the ASCII table.

    The above expression produces also an error on CodeWarrior 11 because of the same reason that Expressions 6 and 7 do.



Sriram Pemmaraju
Fri Mar 21 14:34:08 CST 1997