Unit 3: Expressions and Operators
The rules for reading a compound expression the same way the computer does, from arithmetic to boolean logic.
5 topics · 18 questions
Each topic is a path, not a menu: work down it in order and every question builds on the one above. Click any of them to watch the answer draw itself.
Arithmetic Operators and Precedence
- How it worksHow does the computer decide to multiply before it adds inside one expression?
- Worked exampleFind the value of 2 + 3·4 - 6 / 2.
- Common mistakeWhy isn't an expression evaluated strictly left to right the way it's read aloud?
Your turn1 of 2
Find the value of 14 + 7·9 - 32 / 3.Comparison and Boolean Operators
- How it worksHow do and, or, and not combine several true-or-false comparisons into one final answer?
- Worked exampleFind the truth value of 5 > 3 and 2 > 4.
- Common mistakeWhy doesn't or require both of its sides to be true the way and does?
- Going furtherWhat happens to a boolean expression once a not flips only the comparison right after it?
- Worked exampleFind the truth value of 10 ≠ 10 or 3 < 5.
Your turn1 of 2
Out of 363 people, 35 are age-eligible (18 or older), and of those, 8 also have identification, satisfying both required conditions. How many total people satisfy the AND check requiring both age eligibility and identification?Short-Circuit Evaluation
- How it worksHow does an and expression skip evaluating its right side the moment the left side is false?
- Worked exampleTrace count > 0 and total / count when count is 0.
- Common mistakeWhy isn't the right side of an and expression always evaluated no matter what the left side says?
Your turn1 of 2
A loop checks count > 0 and total / count across 142 separate calls, and short-circuit evaluation skips the division whenever count is 0, which happens on 6 of those n calls. On how many of the n calls does the division actually run?Operator Precedence in a Compound Expression
- How it worksHow does not bind tighter than and, which in turn binds tighter than or, inside one long expression?
- Worked exampleFind the truth value of not False and True or False.
- Going furtherWhat changes in a compound expression once parentheses override the default precedence?
Your turn1 of 2
In a compound expression, not binds tighter than and, which binds tighter than or. Given 15 total operators of these three kinds chained together, and 2 of them are not operators applied individually before any and or or runs, how many of the n operators are and or or operators?Augmented Assignment and Increment
- How it worksHow does count += 1 read the current value, add to it, and write the result back in one step?
- Worked exampleTrace total starting at 0 running total += 5 three times in a row.
- Common mistakeWhy doesn't a standalone ++ operator exist in every programming language?
- Going furtherWhat happens when += is used on a string instead of a number?
Your turn1 of 2
total starts at 0. The statement total += 35 runs 13 times in a row. What value does total hold afterward?