LambdaLabTM
Computer Science · Class 11 · Selection Statements
SelectionTwo outcomes⏱️ 11 min read

The if...else Statement

Reach for this one when there are many possibilities but exactly two outcomes. Any age, any mark, any number can go in — and whatever goes in, the program does one of two things. The else block is the default: it carries no condition of its own and catches every case the if did not.

1Where a plain if runs out

Here is the driving-age program written with what you knew yesterday. A 16-year-old is told they may drive. A 12-year-old is told nothing at all:

only_if.py
age = int(input('What is your age? '))

if age >= 16:
    print('You are eligible to drive!')
Output
What is your age? 12

The program is not wrong; it is incomplete. A person who asks a question and is answered with silence has not been answered. This case — the one the condition left out — needs an outcome of its own, and that is what else is for.

2How it is written

shape.py
if condition:
    statement      # runs when the condition is True
else:
    statement      # runs when it is False
next statement

Three things to notice. else has no condition — it is not a question, it is everything left over. It still ends in a colon. And it lines up exactly under the if, at the same indentation, because the two belong to one statement.

The indentation is doing the same job it did with a plain if, only now there are two blocks to keep apart. The lines indented under if are the if block — the statements that run when the condition is True. The lines indented under else are the else block — the ones that run when it is False. A line back at the margin below them belongs to neither, so it runs whichever way the decision went.

3The program, finished

Input
what we ask the user for
  • the person's age
Process
what we work out
  • is the age 16 or more?
Output
what we show
  • “eligible” when it is
  • “not eligible” when it is not
age_drive.py
# ask for the age and say whether the person may drive
age = int(input('What is your age? '))

if age >= 16:
    print('You are eligible to drive!')
else:
    print('You are not eligible to drive!')
Output
What is your age? 19
You are eligible to drive!
age_drive.py
# the same program, run by somebody younger
age = int(input('What is your age? '))

if age >= 16:
    print('You are eligible to drive!')
else:
    print('You are not eligible to drive!')
Output
What is your age? 12
You are not eligible to drive!

Nobody is left in silence now. Every possible age produces an answer, and it is always exactly one answer.

4One road or the other — never both

Drag the age through the boundary. Try to find a value where both messages print, or where neither does. There is not one.

⚖️ One road or the other

Drag the age. Try to find a value where both messages print, or where neither does.

blocks that ran
1
19
age >= 1619 >= 16True
the if block▶ runs
if age >= 16: print('You are eligible to drive!')

runs when the condition is True

the else blockskipped
else: print('You are not eligible to drive!')

the default — runs for everything else

output
What is your age? 19
You are eligible to drive!
Key Takeaway
With if…else, exactly one of the two blocks runs — always one, never both, never neither. If the condition is True, the if block runs and the else block is skipped. If it is False, the else block runs and the if block is skipped.

That is a stronger promise than a plain if makes, and it is the reason to use this shape. A plain if allows the case where nothing happens. if…else does not.

5else is the default case

The else block does not test anything. It is where the program says in every other case, do this. That makes it useful far beyond “the opposite of the condition”:

  • the message for everyone who did not qualify;
  • the price for everyone without a discount card;
  • “Invalid input” for everything that was not one of the answers you asked for.

Because it takes no condition, it can never disagree with the if above it. That matters more than it sounds. Here is the same rule written as two separate if statements — which works:

two_ifs.py
age = int(input('What is your age? '))

if age >= 16:
    print('You are eligible to drive!')

if age < 16:
    print('You are not eligible to drive!')
Output
What is your age? 12
You are not eligible to drive!

It works, but the rule is now written twice, and the two copies have to stay opposites for ever. Change the driving age in one of them and forget the other:

two_ifs_drift.py
# the first test was changed to 18; the second was forgotten
age = int(input('What is your age? '))

if age >= 18:
    print('You are eligible to drive!')

if age < 16:
    print('You are not eligible to drive!')
Output
What is your age? 17

A 17-year-old is now told nothing at all. Both conditions are False, and the gap between 16 and 18 has quietly fallen through the program. With else that gap cannot exist, because there is only one condition to change.

Tip
Two ifs testing opposite conditions is a sign you wanted if…else. It is also slower: Python works out both conditions, where if…else works out one.

6A second example: even or odd

A whole number is even when dividing it by 2 leaves no remainder — which is what % tells you. Two outcomes, so if…else:

even_odd.py
# check whether the number is even or odd
num = int(input('Enter a natural number: '))

if num % 2 == 0:
    print(num, 'is an even number')
else:
    print(num, 'is an odd number')
Output
Enter a natural number: 24
24 is an even number

Run it with an odd number and the other road is taken:

even_odd.py
# the same program, an odd number this time
num = int(input('Enter a natural number: '))

if num % 2 == 0:
    print(num, 'is an even number')
else:
    print(num, 'is an odd number')
Output
Enter a natural number: 7
7 is an odd number
Watch Out
num % 2 == 0 has two operators and the order matters. % runs first, then == compares its result with 0 — which is what you want, and it is why no brackets are needed. Add them in the wrong place and the program does not merely answer wrongly: num % (2 == 0) works out 2 == 0 as False, which counts as 0, and crashes with ZeroDivisionError: integer modulo by zero.

7What goes wrong

Giving else a condition. It never takes one. If you need a second test, you need elif — the next lesson.

else_condition.py
age = 19

if age >= 16:
    print('You are eligible to drive!')
else age < 16:
    print('You are not eligible to drive!')
Output
  File "else_condition.py", line 5
    else age < 16:
         ^^^
SyntaxError: expected ':'

Indenting else. It must start in the same column as its if. Indented, it looks to Python like a statement inside the block, and no statement may begin with else:

else_indented.py
age = 19

if age >= 16:
    print('You may drive')
    else:
        print('You may not drive')
Output
  File "else_indented.py", line 5
    else:
    ^^^^
SyntaxError: invalid syntax

8Try it

Run it with a few ages, including 15 and 16. Then change >= to > and find the one age whose answer changes.

age_drive.py

9Recap

Key Takeaway
if…else gives your program exactly two outcomes, and exactly one of them happens on every run. else takes no condition, ends in a colon, and lines up under its if. It is the default case — everything the condition did not catch.
Quick Check

In an if…else, how many of the two blocks run?

Quick Check

Why does else not need a condition of its own?

Quick Check

num = 7. What does 'if num % 2 == 0:' … 'else:' print?