Skip to main content
CodeFlow
control flow~10 minRoute 01 5 of 12

Conditions & Booleans

Decisions, two tracks, and the lever that picks one.

Story — the switchman at the fork

true ↗false ↘one question, one track

Picture a railway with a single track that splits in two. A train arrives carrying a value. Standing at the fork is a switchman with a lever. Above the lever is a question — a true-or-false question about the value the train carries.

If the answer is true, the switchman pulls the lever one way and the train takes the left track. If false, the lever goes the other way and the train takes the right track. That's an if/else: one question, two destinations, exactly one chosen.

Switchman says

I never check both tracks. I check the question once, throw the lever once, and the train goes exactly one way. That is the whole job.

See it — change the value, watch the lever

Type a value, pick an expression, then press Run. Watch the expression substitute, evaluate, and the lever throw to the chosen track. Try a value that makes the answer false — see the train go the other way.

age >= 18
true → Can votefalse → Too youngswitchman
Phase: idle

✎ Sharpen your pencil

After watching the animation a couple of times, write one sentence describing what changes between the 'true' branch and the 'false' branch — without using the words 'if' or 'else'.

Switchman says

Watch the lever — not the train. The condition is the question I ask once, before the train arrives.

Try it — assemble the if/else

The five lines below build a working if/else, but they are scrambled. Reorder them so the program is correct.

Drag the pieces — or tab to one and use the / buttons — to put the program in order.

  1. print("Too young")
  2. age = 21
  3. print("Can vote")
  4. else:
  5. if age >= 18:
Hint: Set the variable first. The expression goes on the if line; indented lines belong inside their branch.

Code it — five languages, one decision

Most languages spell the same idea three ways: the keyword (if), a true/false expression, and a body. Python uses indentation; the others use braces.

age = 21
if age >= 18:
    print("Can vote")
else:
    print("Too young")

Build the snippet by clicking tokens from the pool, in order. Click a slot to send a token back.

PoolClick to place
SlotsClick to send back
  1. empty
  2. empty
  3. empty
  4. empty
  5. empty
  6. empty
Hint: Read it left-to-right: keyword, value, comparison, target, colon, body.

Walk through it line by line

One step shows substitution; the next shows evaluation. The else block is highlighted but never runs — that is the whole point of an else.

Step 1 / 5python
1age = 21
2if age >= 18:
3 print("Can vote")
4else:
5 print("Too young")

Codey says

Python uses indentation; the others use braces. The decision itself is identical.

Quiz it — make it stick

  1. Question 1

    In Python, what does `x == 5` mean compared to `x = 5`?

  2. Predict — Question 2

    What does this print?

    score = 75
    if score >= 90:
        print("A")
    elif score >= 70:
        print("B")
    elif score >= 50:
        print("C")
    else:
        print("F")
  3. True or false — Question 3

    In Python, an empty list `[]` evaluates as truthy in an if statement.

No Dumb Questions

Real questions other learners asked on this page.

  • Why do I need `==` instead of `=` for comparison?
    Because the language has to tell the two jobs apart. `x = 5` says "make x equal to 5" (a command). `x == 5` asks "is x equal to 5?" (a question). They look similar but do completely different things, so they get different operators.
  • What does "truthy" mean? Is true the only true thing?
    Most languages let many values stand in for true or false in an if statement. In Python, anything non-empty and non-zero is "truthy" — the number 7, the string "hi", a list with anything in it. Anything empty or zero is "falsy".
  • Why does `if/elif/elif/else` exist instead of just stacking ifs?
    Because `elif` says "only check this if every if/elif above already failed." Stacking separate ifs runs every check, so two could match and run two branches. With `elif`, exactly one branch runs.