---
title: "Selection with if, elif and else"
description: "Making a program take one path or another according to a condition, including several branches tested in order until one fits."
canonical: https://lightmysky.com/learn/computing/selection-with-if-elif-and-else-mt_zylJ-p9g1g
source: https://lightmysky.com/learn/computing/selection-with-if-elif-and-else-mt_zylJ-p9g1g.md
retrieved: 2026-09-12
---

> **Agent view.** This is the Markdown twin of the page, for tools and assistants.
> When to use this site, and the call that answers each job: https://lightmysky.com/agent-instructions.md
> API description (OpenAPI 3.1): https://lightmysky.com/openapi.json · Authentication: https://lightmysky.com/auth.md
> Pricing: https://lightmysky.com/pricing.md · Catalog: https://lightmysky.com/llms.txt · Full catalog: https://lightmysky.com/llms-full.txt
> Every machine-readable file on this domain: https://lightmysky.com/.well-known/ai-catalog.json
> Ask for Markdown with `Accept: text/markdown`, a `.md` address, or `?mode=agent`.

# Selection with if, elif and else

Making a program take one path or another according to a condition, including several branches tested in order until one fits.

Subject: Computing · Area: Programming · Ages 14 to 16
Page: https://lightmysky.com/learn/computing/selection-with-if-elif-and-else-mt_zylJ-p9g1g

## Ready when they can

- Write an if and else that react to a value the user typed
- Use a chain of branches so exactly one of several messages appears
- Explain why reordering the branches changes the result

## Lesson: One road per run

Selection picks one road for the program to walk. If the test is true its block runs, elif tries the next test, and else sweeps up everything left over. In Python the blocks are marked by indentation, so lining up wrongly means thinking wrongly.

**Example.** A grading program tests from the top: if mark >= 90 print A, elif mark >= 70 print B, else print Fail. A mark of 82 skips A, matches B, and stops there. Only the first true branch ever runs.

Order changes the result, so always test the highest boundary first. A mark like 95 would match a lower test too, and the chain stops at the first match. Tests can also sit inside other tests, and a whole if and else that only sets one value can shrink to a single line.

**Tip.** Try every road before you trust the chain: the true case, the false case, and each elif in turn.

**Recap.** First true branch wins, so order your tests from strictest to loosest.

## Practice

8 questions on this page, each with its working shown.

## Needs first

- [Boolean Expressions and Comparison Operators](https://lightmysky.com/learn/computing/boolean-expressions-and-comparison-operators-mt_0aIh-DPCZ4)

## Opens up

- [Count-Controlled Loops](https://lightmysky.com/learn/computing/count-controlled-loops-mt_r1eXTWI_6j)
