# Conditionals and Logical Branching

Computer Science I · Programming I · https://tryals.app/learn/computer-science-i/conditionals-and-logical-branching

## Making Decisions

A program that always does the same thing is not very useful. **Conditionals** let code choose a path based on whether a condition is true, using `if` and optional `else` blocks.

The condition must be a **boolean expression** evaluating to `true` or `false`. **Comparison operators** build these:

| Operator | Meaning | Example |
| --- | --- | --- |
| `==` | Equal | `x == 5` |
| `!=` | Not equal | `x != 5` |
| `<`, `>` | Less / Greater | `x > 0` |
| `<=`, `>=` | Less/Greater-equal | `x <= 10` |

**Common pitfall:** Confusing `==` (comparison) with `=` (assignment). Writing `if x = 5` tries to assign a value instead of comparing it, causing a syntax or logic error.

## Combining Conditions

**Boolean operators** combine multiple expressions. **Short-circuiting** means evaluation stops early if the outcome is already known.

| Operator | Rule | Short-Circuit Behavior |
| --- | --- | --- |
| **`and`** | True if **both** sides are true | If left is false, stops and returns false |
| **`or`** | True if **at least one** side is true | If left is true, stops and returns true |
| **`not`** | Flips the boolean value | N/A |

Chain cases using `else if` (or `elif`). The **first** matching branch runs, and all subsequent branches are skipped entirely.

## Practice questions

5 of this lesson's 11 practice questions, with answers. The full set is in the app.

### 1. A grading program runs: if score >= 90 give A; elif score >= 80 give B; elif score >= 70 give C; else give F. For a score of 85, order what the program does.

**Answer:**

1. Test score >= 90: false, so skip the A branch
2. Test score >= 80: true
3. Run the B branch
4. Skip every remaining branch, including the else

**Why:** Conditions are checked top to bottom; the first true one (score >= 80) runs and every later branch, else included, is skipped. This is why order matters in an elif chain, putting a looser condition first would catch cases meant for a later branch.

Page: https://tryals.app/practice/computer-science-i/conditionals-and-logical-branching/a-grading-program-runs-if-score-90-give-a-elif-score-80-give

### 2. With short-circuit evaluation, in the expression A or B, if A is true then B is still always evaluated.

**Answer:** False

**Why:** False. If A is true, A or B is already true, so B is skipped, the mirror image of 'and' short-circuiting on a false left side. This is exactly why a guard like 'x is not zero or safe_to_divide(x)' can protect the right-hand call.

Page: https://tryals.app/practice/computer-science-i/conditionals-and-logical-branching/with-short-circuit-evaluation-in-the-expression-a-or-b-if-a-is-true

### 3. Sort each operator by its role.

**Answer:**

- Comparison operator: ==, !=, >=
- Boolean operator: and, or
- Assignment operator: =

**Why:** == , != and >= compare and yield true or false; 'and' and 'or' combine booleans; the single = assigns. Filing = under comparison is exactly the bug the lesson warns about: writing = where == is meant tries to assign inside a condition.

Page: https://tryals.app/practice/computer-science-i/conditionals-and-logical-branching/sort-each-operator-by-its-role

### 4. Python makes `if x = 5` a syntax error while C compiles it happily. What is C's version actually doing?

A. Raising a runtime error that is caught silently
B. Nothing, the statement is optimised away
C. Comparing x with 5, since C treats a single equals as comparison in conditionals
D. Assigning 5 to x and taking the branch, since 5 is truthy

**Answer:** D. Assigning 5 to x and taking the branch, since 5 is truthy

**Why:** Assignment is an expression in C, so it yields 5, the branch always runs, and x has been overwritten as a side effect of a test. No error is reported anywhere, which is why Python made assignment a statement, Java requires a boolean condition, and C programmers learned to write `if (5 == x)`.

Page: https://tryals.app/practice/computer-science-i/conditionals-and-logical-branching/python-makes-if-x-5-a-syntax-error-while-c-compiles-it-happily

### 5. `if x = 5` is a common bug in languages where it is legal, while `if x == 5` is what was meant. Why is the mistake so easy to make and so hard to spot?

A. Because the two operators mean the same thing and the difference only matters in some languages.
B. Because assignment is an expression, so the mistaken version compiles and runs.
C. Because = is slower than ==, so the program runs correctly but performs poorly.
D. Because the compiler cannot distinguish the two operators without type annotations.

**Answer:** B. Because assignment is an expression, so the mistaken version compiles and runs.

**Why:** In C and its relatives assignment is an expression whose value is the assigned value, so `if (x = 5)` assigns, evaluates to 5, and takes the branch, silently, with nothing to report. Language designers have responded in several ways, which shows how serious it is: Python makes assignment a statement, Java demands a boolean, and C programmers invented `if (5 == x)`.

Page: https://tryals.app/practice/computer-science-i/conditionals-and-logical-branching/if-x-5-is-a-common-bug-in-languages-where-it-is-legal-while-if-x
