Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › C++ FUNDAMENTALS
A comparison asks about one relationship between two values and produces one truth value: true or false. With score = 12, lower = 5, and upper = 10, each operator answers a separate question. A comparison does not return the distance between values, and it does not remember a comparison that came before it.
| EXPRESSION | RESULT | REASON |
|---|---|---|
| score == lower | false | 12 is not equal to 5 |
| score != lower | true | 12 is different from 5 |
| score < upper | false | 12 is not less than 10 |
| score <= upper | false | 12 is not less than or equal to 10 |
| score > lower | true | 12 is greater than 5 |
| score >= lower | true | 12 is greater than or equal to 5 |
The expressions score >= lower and score <= upper each produce their own truth value. The first says that 12 has reached the lower boundary, while the second says that 12 has not passed the upper boundary. To test both boundaries at once, you must combine those complete comparisons with a logical operator.
The mathematical-looking expression lower <= score <= upper does not mean what it appears to mean in C++. C++ evaluates operators in steps, so it first evaluates lower <= score. With the fixed values, 5 <= 12 is true. In the next comparison, that truth value is used numerically as 1, producing 1 <= 10, which is also true. The complete C++ expression therefore reports true even though 12 is outside 5-10.
int score = 12;
int lower = 5;
int upper = 10;
bool chained = lower <= score <= upper; // true in C++, but wrong
bool joined = score >= lower && score <= upper; // false, the correct resultJava does not silently use a truth value as the number for the next comparison. After Java evaluates lower <= score, it has a truth value, and Java cannot compare that truth value numerically with upper. The chained expression is rejected instead of producing a misleading result. In both languages, write the lower-bound and upper-bound comparisons separately.
Which expression correctly tests whether score = 12 is inside the range from lower = 5 to upper = 10?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The operator && means both complete comparisons must be true. The inside-range test is (score >= lower) && (score <= upper). For the fixed values, this becomes true && false, so the result is false. The parentheses make each operand visible as a complete comparison involving score, lower, or upper.
The operator || means at least one complete comparison must be true. A value is outside the range when it is below the lower boundary or above the upper boundary: (score < lower) || (score > upper). Here, score < lower is 12 < 5, or false, and score > upper is 12 > 10, or true. false || true produces true.
bool inside = (score >= lower) && (score <= upper);
bool outside = (score < lower) || (score > upper);
bool alsoOutside = !((score >= lower) && (score <= upper));The operator ! reverses one truth value. Since the inside expression is false for score = 12, !((score >= lower) && (score <= upper)) is true. The direct || form and the negated && form agree here because each describes the same two possibilities: score is below lower or score is above upper.
Type an expression using score, lower, upper, and || that is true because score = 12 is outside 5-10.
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The && operator evaluates from left to right and stops as soon as one operand is false. In score <= upper && 100 / (score - score) > 0, the left comparison is 12 <= 10, which is false. Because both operands cannot be true anymore, the right comparison is skipped completely, including its division.
bool safe = (score <= upper) && (100 / (score - score) > 0);If the right operand were evaluated, score - score would be 0 and the program would attempt integer division by zero. In C++, integer division by zero is undefined behaviour. In Java, it throws an ArithmeticException. Short-circuiting avoids that consequence here because the fixed left comparison is false, but changing the left comparison or the values can allow the unsafe operand to run.