← Introduction to Computer Science

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

  1. How it worksHow does the computer decide to multiply before it adds inside one expression?
  2. Worked exampleFind the value of 2 + 3·4 - 6 / 2.
  3. 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

  1. How it worksHow do and, or, and not combine several true-or-false comparisons into one final answer?
  2. Worked exampleFind the truth value of 5 > 3 and 2 > 4.
  3. Common mistakeWhy doesn't or require both of its sides to be true the way and does?
  4. Going furtherWhat happens to a boolean expression once a not flips only the comparison right after it?
  5. 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

  1. How it worksHow does an and expression skip evaluating its right side the moment the left side is false?
  2. Worked exampleTrace count > 0 and total / count when count is 0.
  3. 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

  1. How it worksHow does not bind tighter than and, which in turn binds tighter than or, inside one long expression?
  2. Worked exampleFind the truth value of not False and True or False.
  3. 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

  1. How it worksHow does count += 1 read the current value, add to it, and write the result back in one step?
  2. Worked exampleTrace total starting at 0 running total += 5 three times in a row.
  3. Common mistakeWhy doesn't a standalone ++ operator exist in every programming language?
  4. 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?