MCS-201 · Unit 10

Data Structures and Control Statements in Python

Section 10 of 12

Block 3 · Unit 10Data Structures and Control Statements in Python11 min · Section 10 of 12

10.10Decision Control Statements

Covers IGNOU MCS-201 §10.9, 10.9.1, 10.9.2

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

KindMeaningPython statements
SequentialStatements run one after another in order(default)
Decision (selection)A condition decides which block runsif, if…else, if…elif…else, match
Loop (iteration, repetition)A block runs repeatedlywhile, for

The if family

Syntax
if condition:
    block_1
elif another_condition:
    block_2
else:
    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 1 · multiple choice

What does this program print?

Python · quiz_if.py
x = 15
if x > 10:
    print("A")
elif x > 5:
    print("B")
else:
    print("C")
Show answer

Answer: B. A

The first true condition is x > 10; once its block runs, the elif and else are skipped even though x > 5 is also true.

Question 2 · multiple choice

What is "pass" if 45 >= 40 else "fail"?

Show answer

Answer: A. "pass"

The conditional expression returns the first value when the condition is true.

Question 3 · multiple choice

Is the condition in if -3: true or false?

Show answer

Answer: C. True, because every non-zero number is truthy

Only zero counts as false among numbers.

Question 4 · multiple choice

What does this program print when a = 7 and b = -1?

Python · quiz_nested.py
a, b = 7, -1
x = 0
if a > 0:
    if b < 0:
        x = x + 5
    elif a > 5:
        x = x + 4
    else:
        x = x + 3
else:
    x = x + 2
print(x)
Show answer

Answer: D. 5

a > 0 is true, then b < 0 is true, so 5 is added; the inner elif is not checked.

Question 5 · multiple choice

Which keyword starts an alternative condition in an if chain?

Show answer

Answer: C. elif

Python uses the single keyword elif.

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.)

↑ ↓ move↵ openTab switch type