Beep! Tap the happy computer!
Decisions & loops

How does a program choose a path?

# If / else decisions Programs choose. ```python marks = 73 if marks >= 40: print("Pass") else: print("Need more practice") ``` ## Indentation The spaces before `print` matter in Python. They show “this line belongs to the if”. ## elif ```python if marks >= 90: print("Excellent") elif marks >= 40: print("Pass") else: print("Keep going") ``` This is the diamond in your flowchart, written in words. > Compare with `==` (equal). A single `=` means “store”, not “is this equal?”. ## One run, one path ``` if raining: take_umbrella() else: wear_cap() ``` You do **not** take the umbrella *and* the cap unless you wrote it that way. The condition picks. ### Comparisons you already know | Algebra | Python | | --- | --- | | > | > | | < | < | | ≥ | >= | | ≤ | <= | | = (is equal) | == | | ≠ | != | ### Indent Lines pushed in under `if` belong to the if. The first line that goes back to the margin is *after* the if. That is why we are picky about spaces in Python. They are not decoration — they are structure.

By the end of this lesson

Trace a simple if/else on a marks rule (pass if ≥ 40) and connect it to algebra inequalities.

Remember

  • if / else lets a program choose a path.
  • The condition is a yes/no question: marks >= 40.
  • True → the if block runs. False → the else block runs (if there is one).
  • This is the diamond in a flowchart, and the same idea as x ≥ 40 in algebra.
  • Indentation (the extra spaces) shows which lines belong to the if.

Worked examples

Cover the answer. Try the steps on paper, then open them.

Example 1 — Pass line

marks = 42 if marks >= 40: print("Pass") else: print("Try again")

  1. 42 >= 40 is True
  2. The if branch runs
  3. Screen: Pass
  4. else is skipped

Answer: Pass

Check: Change to 38 and it prints Try again.

Example 2 — Umbrella

if raining: take umbrella else: leave it.

  1. Condition: is it raining?
  2. Two paths, one run
  3. The unused path is not a bug — it is the other day

Answer: one path per run

Check: Programs choose; they do not take both.

Example 3 — Connect to algebra

if score >= 40 is the inequality score ≥ 40.

  1. Same comparison symbols: > < >= <= ==
  2. Algebra draws it on a line; code jumps to a block

Answer: same comparison, different costume

Check: You already learned the symbols in Algebra starters.

Common mistakes

Slip Using = instead of == in a condition (Python).

Fix = assigns. == asks “equal?”.

if score = 10 is the wrong symbol.

Slip Forgetting else and wondering why nothing printed when the if was false.

Fix A bare if does nothing on the false path.

Add else, or accept silence.

Slip Not indenting the print under the if.

Fix Python uses indent to know the block.

The extra spaces are part of the language.

Try on paper first

Write the answer, then tap Reveal. These are not the quiz — they are warm-ups.

1. What type of question does if ask?

Answer: Yes/no (true/false)

2. If marks = 40 and the test is marks >= 40, do you pass?

Answer: Yes

3. If marks = 39 and the test is marks >= 40, do you pass?

Answer: No

4. The flowchart shape for if is a…

Answer: Diamond

Touch practice

9 tap / type

Tap or type. Use the hint if you need it. Retry any miss — the why is the lesson.

Optional picture lab

After practice

Real world

Umbrella if it rains.

Connect

Algebra inequalities: if marks ≥ 40.

⭐ Star quiz

9 questions

Do the worked examples and touch practice first. Then take the quiz without peeking.