ChristCoded

the course / the basics

If / else: choose you this day

Joshua gave Israel a fork in the road, and every program has the same thing: a question that's either true or false. Learn how if and else decide which code runs, why indentation matters, and why only one branch ever runs.

And if it seem evil unto you to serve the LORD, choose you this day whom ye will serve; whether the gods which your fathers served that were on the other side of the flood, or the gods of the Amorites, in whose land ye dwell: but as for me and my house, we will serve the LORD. — Joshua 24:15 (KJV)

At the end of his life, Joshua gathered the tribes of Israel at Shechem and laid a decision in front of them. Not “maybe”, not “later” — choose you this day. And then he told them his own answer before they gave theirs: as for me and my house, we will serve the LORD.

A program makes decisions in exactly that shape: if something is true, do this; else, do that.

The if statement

answer = input("Whom will ye serve? ")

if answer == "the LORD":
    print("As for me and my house")
else:
    print("Choose you this day")

Read it top to bottom:

  1. input(...) shows the question and waits for you to type an answer. Whatever you type is stored in answer (that’s a variable).
  2. if answer == "the LORD": asks a question. The answer is either True or False.
  3. If it’s True, Python runs the indented line under the if.
  4. If it’s False, Python skips that line and runs the indented line under else.

Run it twice:

Whom will ye serve? the LORD
As for me and my house

Whom will ye serve? tomorrow
Choose you this day

The condition: a question with a yes-or-no answer

The part between if and the colon is the condition. It must come out as True or False — values called booleans.

Condition Means Example that’s True
a == b is equal to "the LORD" == "the LORD"
a != b is not equal to "tomorrow" != "today"
a < b, a > b less / greater than 3 < 7
a <= b, a >= b less / greater or equal 20 <= 20

Notice == has two equals signs. A single = assigns a value (point this name here); == asks whether two things are equal. Mixing them up is the most common beginner bug — Python will refuse if answer = "the LORD": with a SyntaxError.

Indentation decides what belongs where

Python knows which lines belong to the if by their indentation — the spaces at the start of the line. Everything indented under if runs only when the condition is True; everything indented under else runs only when it’s False. Lines that aren’t indented run either way.

if answer == "the LORD":
    print("As for me and my house")
    print("we will serve the LORD.")   # still inside the if
print("Amen.")                          # runs every time

Use 4 spaces per level (your editor will usually do it when you press Tab).

Only one branch runs

An if/else is a fork in the road: each run of the program takes exactly one path. There’s no way for both blocks to run, and no way for neither to run.

More than two roads: elif

Joshua’s verse actually names three options: the gods beyond the river, the gods of the Amorites, or the LORD. When you have more than two paths, add elif (“else if”) branches. Python checks them in order and runs the first one that’s True:

hour = 14

if hour < 12:
    print("Good morning")
elif hour < 18:
    print("Good afternoon")      # this one runs: 14 is not < 12, but it is < 18
else:
    print("Good evening")

Still only one branch runs — the first match wins, and the rest are skipped.

The same idea in JavaScript

const answer = prompt("Whom will ye serve?");

if (answer === "the LORD") {
  console.log("As for me and my house");
} else {
  console.log("Choose you this day");
}

JavaScript puts the condition in parentheses and the blocks in { } braces instead of using indentation, and uses === for “is equal to”.

It’s everywhere

App The fork
Your phone if password == saved: unlock it
A game if health <= 0: game over
Your battery if battery < 20: low power mode

Common mistakes

  • = instead of == in the condition (see above).
  • Forgetting the colon at the end of if ...: and else: — Python needs it.
  • Wrong indentation. A line that should be inside the if but isn’t indented will run every time.
  • Comparing different cases. "The LORD" == "the LORD" is False: capital letters matter. answer.lower() == "the lord" is more forgiving.

Try it

  1. Change the program so that typing the lord in any capitalisation counts.

  2. Write a program that asks for an age and prints "adult" if it’s 18 or more, otherwise "minor". (Hint: int(input(...)) turns typed text into a number.)

  3. Add an elif branch to Joshua’s program for the answer "tomorrow" that prints "Choose you this day, not tomorrow."

    One way to do it
    answer = input("Whom will ye serve? ")
    if answer.lower() == "the lord":
        print("As for me and my house")
    elif answer == "tomorrow":
        print("Choose you this day, not tomorrow.")
    else:
        print("Choose you this day")

Reflect

Joshua didn’t wait for the crowd before he answered for his own household. Some decisions in life have only two roads, and putting them off is a choice too. Choose you this day.