Control flow statements decide which statements run and how often. By default statements run in sequence, top to bottom. This section covers decision statements, which choose between blocks; the next covers loops.
Three kinds of control flow
Kind
Meaning
Python statements
Sequential
Statements run one after another in order
(default)
Decision (selection)
A condition decides which block runs
if, if…else, if…elif…else, match
Loop (iteration, repetition)
A block runs repeatedly
while, for
The if family
Syntax
if condition: block_1elif another_condition: block_2else: block_3
Each header ends with a colon :; the block under it is indented (4 spaces). The block ends at the first line that is not indented as deeply.
elif and else are optional; any number of elif parts may appear; else comes last.
Only the first true condition’s block runs; the rest are skipped.
The condition can be any value: non-zero numbers and non-empty collections are true, so if items: means “if the list is not empty”.
A block cannot be empty; write pass as a placeholder.
The conditional expression puts a simple if/else on one line: status = "even" if n % 2 == 0 else "odd".
Nested if: the leap-year test
An if inside another if (or else) is a nested if. The leap-year rule is a classic example: a year is a leap year if it is divisible by 4, except century years, which must be divisible by 400.
Key takeaways
Control flow is sequential, decision or loop.
if, elif, else choose one block; only the first true branch runs.
Indentation defines blocks; pass fills an empty block.
x if cond else y is a one-line conditional expression.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Write a program that reads three numbers and prints the largest, using if…elif…else.
Show worked answer
a = float(input("a: ")), b = float(input("b: ")), c = float(input("c: ")), then if a >= b and a >= c: largest = a, elif b >= c: largest = b, else: largest = c, and print("Largest:", largest). The first test checks whether a is at least as large as both others; if not, the largest is either b or c, which the elif decides. (The built-in max(a, b, c) gives the same result.)
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-201/data-structures-and-control-statements-in-python/decision-control-statements/ to read this page.