LambdaLabTM
Computer Science · Class 11 · Selection Statements
Types of StatementsSelection⏱️ 10 min read

What is a Selection Statement?

Every program you have written so far runs every line it contains. A selection statement is the first one that does not: based on a condition, some statements are carried out and others are skipped completely. It is also called a conditional statement, and it is how a program stops being a fixed list of instructions and starts making a decision.

1A program that decides

Think about a shop bill. If the total is over ₹500, delivery is free. If it is not, delivery is charged. The same shop, the same rule, two different bills — and the shopkeeper does something different in each case.

You could not write that with what you know so far. A sequential program does the same thing every time it runs. To write the rule you need a way to say: run these lines only when this is true.

Key Takeaway
A selection (or conditional) statement chooses which statements to run. Based on a condition, some statements get executed and some get skipped. Which ones is decided while the program is running — not when you write it.

2It all hangs on the condition

The condition is the question being asked. It is nothing new: it is an expression whose value is True or False, exactly like the ones you built with the relational and logical operators.

conditions.py
# a condition is just an expression whose value is True or False
age = 19

print(age >= 16)
print(age == 16)
print(age < 16 or age > 60)
Output
True
False
False

Those three lines print their answers because we asked them to. Inside a selection statement nothing is printed — Python works the condition out for itself, and uses the True or False to decide what to run next.

Tip
If you can say your rule as a question with a yes-or-no answer, you can write it as a condition. Is the age at least 16? age >= 16. Is the number even? num % 2 == 0. Is the day Sunday? day == 'Sunday'.

3The shape of a selection statement

Every selection statement is built from the same parts. Tap them below — including the empty space at the start of the third line, which is a part in its own right.

🔎 The parts of an if statement

Tap any part of the program — including the empty space — to see what it is called and what it does.

age = 19

Nothing selected. Tap a coloured part above — the four spaces at the start of the third line count as a part too.

Two of those parts are new punctuation and both are compulsory. The colon ends the header. The indentation marks the block that belongs to it.

4Indentation is not decoration

In many languages, curly brackets say where a block starts and ends, and the spacing is only for looks. Python has no brackets here. The spacing is the language: a line indented under the header is inside the block, and a line back at the margin is outside it.

Which means one stray press of the space bar changes what your program does. Both programs below have a bill of 200, and only one of them prints anything at all:

right.py
bill = 200.0

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

print('Amount to pay:', bill)
Output
Amount to pay: 200.0
indent_trap.py
# the last line has been indented by mistake
bill = 200.0

if bill > 500:
    print('You get free delivery!')
    print('Amount to pay:', bill)
Output

The second program prints nothing. Its last line is now indented, so it is part of the block — and the block was skipped, because 200 is not more than 500. Same statements, same order, one extra level of indentation, and the output disappears. No error is reported, because nothing is wrong with the program; it is simply not the program you meant to write.

Watch Out
Use 4 spaces, and use them everywhere. Python accepts any consistent amount, but 4 spaces is the standard every book and every exam uses. Lines in the same block must all be indented the same; mixing 4 spaces on one line and 6 on the next is an error. Press Tab in IDLE and you get 4 spaces — that is what it is set up to do.

A block is not limited to one statement. Every line indented under the header belongs to it, however many there are:

block_of_two.py
# a block can hold as many statements as you like
marks = 91

if marks >= 90:
    print('Excellent!')
    print('You are in the top grade.')

print('Marks =', marks)
Output
Excellent!
You are in the top grade.
Marks = 91

5Selection comes in three shapes

Now the part that decides which one you write. All three start with if and a condition, so they look alike on the page. What separates them is how many different things your program can end up doing.

🔀 The three shapes

They all start with if, so the syntax will not tell you which to write. Count the outcomes instead.

if

1 outcome

Do this — or do nothing extra.

the shape it makes
if the bill is over 500:
say delivery is free
print the amount to pay

There is one block, and it either runs or it does not. The bill can be any amount at all — that is the many possibilities — but there is only one extra thing that can happen, and when the condition is False the program simply carries on with the line below.

reach for it when

Many possibilities, but only one thing the program might do about them.

ShapeOutcomesUse it when
if1there is one thing that might happen — otherwise the program just carries on
if…elseexactly 2there are two things it might do, and one of them is the default
if…elif…elsemanythere are more than two, and each one has its own test
Note
Counting the outcomes, not the conditions. All three can test as many possibilities as you like — any age, any bill, any day of the week. The question that picks the shape is what the program does about them: one thing, one of two things, or one of many.

6Recap

Key Takeaway
A selection statement runs some statements and skips others, decided by a condition — an expression that is True or False. The header ends in a colon, and the indented lines under it are the block that may run. There are three shapes — if, if…else and if…elif…else — and you pick between them by counting the outcomes.
Note
The next three lessons are one shape each, in that order. Everything on this page is the part they share.
Quick Check

What decides whether the block under an if header runs?

Quick Check

A line under an if header is indented by 4 spaces. What does that indentation mean?

Quick Check

A program must print 'Pass' or 'Fail' and nothing else. Which shape fits?