# Conditionals and Boolean Logic

Mathematics I · Programming Elements · https://tryals.app/learn/mathematics-i/conditionals-and-boolean-logic

## Making Decisions

Programs make **decisions** using **conditional statements**. An `if` statement runs code only when a condition is true; an `if/else` chooses between two paths; `else if` chains mutually exclusive cases. The condition is a **boolean expression**, evaluating to `true` or `false`.

Conditions rely on **comparison operators** to produce booleans:

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

> **Pitfall:** Never confuse `=` (assignment: store a value) with `==` (comparison: test equality). Writing `if (x = 5)` assigns 5 instead of testing it.

## Boolean Logic

Booleans combine using **logical operators**, matching propositional logic:

| Operator | Symbol | Rule |
| --- | --- | --- |
| `and` | `&&` | True if **both** are true |
| `or` | `\|\|` | True if **at least one** is true |
| `not` | `!` | Negates the boolean |

**Operator precedence** and parentheses control evaluation order. For safety, use parentheses: `(x > 0) and (x < 10)`.

Languages use **short-circuit evaluation**: in $A \text{ and } B$, if $A$ is false, $B$ is skipped. This prevents runtime errors, allowing safe guards like checking if a list has items before inspecting its contents.

## Practice questions

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

### 1. `if (i < len && arr[i] > 0)` is safe, and swapping the two conditions can crash. Why does the order of a boolean test matter when `AND` is commutative?

A. Because short-circuit evaluation stops at the first false
B. Because array bounds are checked left to right by the compiler
C. Because the second condition is undefined, which the first prevents from being evaluated
D. Because AND is not commutative in programming languages

**Answer:** A. Because short-circuit evaluation stops at the first false

**Why:** Logically A ∧ B equals B ∧ A; operationally the language evaluates left to right and stops early, so the guard protects the access. It is a language guarantee layered on top of the logic, and it is why side effects inside boolean expressions are such a reliable source of bugs.

Page: https://tryals.app/practice/mathematics-i/conditionals-and-boolean-logic/if-i-len-arr-i-0-is-safe-and-swapping-the-two-conditions

### 2. Let x be 4 and y be 9. Sort each condition by its value.

**Answer:**

- True: x < y, x != y, (x > 5) or (y > 5)
- False: (x == 4) and (y == 4), not (x < y), (x > 5) and (y > 5)

**Why:** 4 < 9 and 4 != 9 hold; negating a true condition gives false; y == 4 fails so the conjunction in c2 fails. c5 is true because y > 5, but c6 fails because x > 5 does not, and demands every part, or demands only one.

Page: https://tryals.app/practice/mathematics-i/conditionals-and-boolean-logic/let-x-be-4-and-y-be-9-sort-each-condition-by-its-value

### 3. Let p be true and q be false. Select every expression that evaluates to TRUE.

A. p or q
B. p and (not q)
C. p and q
D. not (p or q)
E. not q

**Answer:** A. p or q; B. p and (not q); E. not q

**Why:** p or q is true because p holds; not q is true because q fails; p and (not q) is true and false is... true and true, so true. p and q fails on q, and not (p or q) negates a true value. Getting the last one wrong usually means negating the wrong level.

Page: https://tryals.app/practice/mathematics-i/conditionals-and-boolean-logic/let-p-be-true-and-q-be-false-select-every-expression-that-evaluates

### 4. Short-circuit evaluation of `a AND b` is what makes `if x != 0 and 10/x > 1` safe.

**Answer:** True

**Why:** True. A false left operand fixes the whole conjunction as false, so the right side is never evaluated and the division never happens. It also means `AND` is not commutative once either side can fault, swapping the operands here crashes on exactly the input the guard was written for.

Page: https://tryals.app/practice/mathematics-i/conditionals-and-boolean-logic/short-circuit-evaluation-of-a-and-b-is-what-makes-if-x-0-and-10-x
