LambdaLabTM
Computer Science · Class 11 · Selection Statements
SelectionOne outcome⏱️ 10 min read

The if Statement

The simplest of the three, and the one to reach for when there are many possibilities but only one outcome. The value being tested can be anything at all — any bill, any age, any mark — but there is only one extra thing your program might do about it. It happens, or the program carries on as though the if were not there.

1How it is written

shape.py
if condition:
    statement
    statement
next statement

The header names the condition and ends in a colon. The indented lines under it are the block. The line at the margin below is not part of the block, and that line is the one worth watching in everything that follows.

2A program that decides

A shop gives free delivery on bills over ₹500. Every customer is told their total; only some are told about the delivery.

Input
what we ask the user for
  • the bill amount
Process
what we work out
  • is the bill more than 500?
Output
what we show
  • the free-delivery message, but only when it is
  • the amount to pay, always
delivery.py
# a big order earns free delivery
bill = float(input('Bill amount: '))

if bill > 500:
    print('You get free delivery!')

print('Amount to pay:', bill)
Output
Bill amount: 750
You get free delivery!
Amount to pay: 750.0

Now the same program, run again by a customer who spent less:

delivery.py
# the same program, a different customer
bill = float(input('Bill amount: '))

if bill > 500:
    print('You get free delivery!')

print('Amount to pay:', bill)
Output
Bill amount: 200
Amount to pay: 200.0

One program, two runs, two different outputs. Nothing was rewritten between them. That is the whole point of selection — and notice what did not happen on the second run: no error, no warning, no blank line where the message would have been. The block was skipped, and the program went straight on to the line below it.

3Watch the condition decide

Drag the bill through the boundary and watch two things: the block, which appears and disappears, and the last line, which never does.

✅ Run it, or skip it

Drag the bill the customer typed. Watch the block below the if — and watch the last line, which never moves.

750.0
delivery.py
headerbody — the indented blockoutside
bill = float(input('Bill amount: '))
if bill > 500:
print('You get free delivery!')
print('Amount to pay:', bill)
the condition
bill > 500750.0 > 500
True

True → the block runs, then the program carries on with the line below it.

output
Bill amount: 750
You get free delivery!
Amount to pay: 750.0

The last line prints either way. That is the whole difference an if makes: one line more, or one line less.

Key Takeaway
False is not an error. When the condition is False, Python skips the block and carries on with the next statement outside it. The program does not stop, nothing is reported, and the rest of the program runs exactly as usual.

4Many possibilities, one outcome

The bill could be any amount — that is thousands of possibilities. But the program only ever does one extra thing about them: it prints the free-delivery message. There is no second message for small bills, and none is wanted. That is what makes a plain if the right shape here.

The condition can be as complicated as the rule needs. Here two questions are joined with and, so several scenarios are being tested at once — and there is still only one outcome:

club.py
# one outcome: you are told about the club, or nothing is said
age = int(input('Your age: '))
marks = float(input('Your marks: '))

if age >= 16 and marks >= 60:
    print('You can join the coding club')

print('Thank you for applying')
Output
Your age: 17
Your marks: 72
You can join the coding club
Thank you for applying
club.py
# the same program, an applicant who is too young
age = int(input('Your age: '))
marks = float(input('Your marks: '))

if age >= 16 and marks >= 60:
    print('You can join the coding club')

print('Thank you for applying')
Output
Your age: 15
Your marks: 90
Thank you for applying

Excellent marks, and still nothing — because and needs both halves to be True. Everybody gets the thank-you either way.

When one outcome is not enough
If you find yourself wanting to say something to the applicants who did not qualify, you have discovered a second outcome — and a plain if is no longer the right shape. That is the next lesson.

5The two mistakes everybody makes once

Both are punctuation, both stop the program before it runs a single line, and both name themselves clearly once you have seen them.

The missing colon.

no_colon.py
age = 19

if age >= 16
    print('You are eligible to drive!')
Output
  File "no_colon.py", line 3
    if age >= 16
                ^
SyntaxError: expected ':'

The missing indentation.

no_indent.py
age = 19

if age >= 16:
print('You are eligible to drive!')
Output
  File "no_indent.py", line 4
    print('You are eligible to drive!')
    ^
IndentationError: expected an indented block after 'if' statement on line 3

Both are syntax errors, so nothing runs at all — not even the correct line above. Python does not need to know the age to spot them; it cannot read the file. And the second message is unusually helpful: it names the statement, the line number and what it wanted.

Watch Out
Do not use = in a condition. = is an order (put 16 in age), == is a question (is age 16?). Writing if age = 16: gives SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='? — a rare case of Python guessing your mistake correctly.

6Try it

Run it with a bill over 500, then with one under. Then try moving the last line four spaces to the right and run it again — you should get nothing at all from a small bill.

delivery.py

7Recap

Key Takeaway
if gives your program one outcome: the block runs when the condition is True, and is skipped when it is False. Skipped means nothing happens — no error, no message — and the program carries on with the first line back at the margin. Header ends in a colon; block is indented by 4 spaces.
Quick Check

bill = 200. What does 'if bill > 500:' followed by a print, then a print at the margin, produce?

Quick Check

Which of these is the right shape for: 'warn the student if attendance is below 75%, and say nothing otherwise'?

Quick Check

What is wrong with: if marks >= 40 print('Pass')