Module III — Flow Control

Topic (a): Decision Making in Python · Detailed Teaching Notes with Example Programs

📑 Contents

1. Introduction to Decision Making

Decision-making statements in Python allow a program to choose different paths of execution based on whether a condition is True or False.

In real life, we make decisions like:

Python provides the following decision-making statements:

StatementPurpose
ifExecutes a block only if the condition is True
if-elseExecutes one block if True, another if False
if-elif-elseChecks multiple conditions in sequence
Nested ifAn if inside another if
🔑 Key Points:

2. The if Statement

Syntax

if condition:
    statement(s)

How it works

  1. The condition is evaluated.
  2. If it is True, the indented block is executed.
  3. If it is False, the block is skipped.

Conceptual Flowchart

[condition?] / \ True False | | Execute Skip block block | \ / Continue
Example 1

Check if a number is positive

# Program to check if a number is positive

num = int(input("Enter a number: "))

if num > 0:
    print("The number is positive.")

print("Program ended.")
▶ Sample Output:
Enter a number: 7
The number is positive.
Program ended.

Explanation: If the user enters 7, the condition num > 0 is True, so the message is printed. The last line print("Program ended.") is outside the if block (no indentation), so it always executes.

Example 2

Check if a person is eligible to vote

# Program to check voting eligibility

age = int(input("Enter your age: "))

if age >= 18:
    print("You are eligible to vote.")
▶ Sample Output:
Enter your age: 20
You are eligible to vote.

Note: If age is less than 18, nothing is printed because there is no else block.

3. The if-else Statement

Syntax

if condition:
    statement(s)   # executes if condition is True
else:
    statement(s)   # executes if condition is False

How it works: Exactly one of the two blocks will execute — never both, never neither.

Conceptual Flowchart

[condition?] / \ True False | | Execute Execute if-block else-block \ / Continue
Example 3

Check whether a number is even or odd

# Program to check even or odd

num = int(input("Enter a number: "))

if num % 2 == 0:
    print(num, "is even.")
else:
    print(num, "is odd.")
▶ Sample Output 1:
Enter a number: 8
8 is even.

▶ Sample Output 2:
Enter a number: 5
5 is odd.

Explanation: num % 2 == 0 is True for even numbers → if block runs. Otherwise → else block runs.

Example 4

Find the larger of two numbers

# Program to find the larger of two numbers

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

if a > b:
    print(a, "is larger.")
else:
    print(b, "is larger.")
▶ Sample Output:
Enter first number: 12
Enter second number: 25
25 is larger.

4. The if-elif-else Statement

Syntax

if condition1:
    statement(s)
elif condition2:
    statement(s)
elif condition3:
    statement(s)
else:
    statement(s)   # optional

How it works:

⚠️ Important:

Conceptual Flowchart

[condition1?] / \ True False | | Block 1 [condition2?] / \ True False | | Block 2 [condition3?] / \ True False | | Block 3 else Block
Example 5

Grade calculator

# Program to assign grade based on marks

marks = int(input("Enter marks (0-100): "))

if marks >= 90:
    grade = "A+"
elif marks >= 80:
    grade = "A"
elif marks >= 70:
    grade = "B"
elif marks >= 60:
    grade = "C"
elif marks >= 50:
    grade = "D"
else:
    grade = "F"

print("Your grade is:", grade)
▶ Sample Output 1:
Enter marks (0-100): 85
Your grade is: A

▶ Sample Output 2:
Enter marks (0-100): 45
Your grade is: F

Explanation: If marks = 85, marks >= 90 is False, marks >= 80 is True → grade = "A". The remaining elif and else are skipped.

Example 6

Check whether a character is a vowel, consonant, or not a letter

# Program to classify a character

ch = input("Enter a character: ")

if len(ch) != 1:
    print("Please enter exactly one character.")
elif ch.isalpha():
    if ch.lower() in "aeiou":
        print(ch, "is a vowel.")
    else:
        print(ch, "is a consonant.")
else:
    print(ch, "is not a letter.")
▶ Sample Output 1:
Enter a character: a
a is a vowel.

▶ Sample Output 2:
Enter a character: k
k is a consonant.

▶ Sample Output 3:
Enter a character: 5
5 is not a letter.

Note: This example uses nested if inside an elif — a preview of the next topic.

Example 7

Simple calculator using if-elif-else

# Simple calculator

a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
op = input("Enter operator (+, -, *, /): ")

if op == "+":
    result = a + b
elif op == "-":
    result = a - b
elif op == "*":
    result = a * b
elif op == "/":
    if b != 0:
        result = a / b
    else:
        result = "Error: Division by zero"
else:
    result = "Invalid operator"

print("Result:", result)
▶ Sample Output:
Enter first number: 10
Enter second number: 4
Enter operator (+, -, *, /): /
Result: 2.5

5. Nested if Statements

Definition: A nested if is an if (or if-else, or if-elif-else) statement placed inside another if, elif, or else block.

Syntax

if condition1:
    if condition2:
        statement(s)
    else:
        statement(s)
else:
    statement(s)

When to use:

Conceptual Flowchart

[condition1?] / \ True False | | [condition2?] else block / \ True False | | Block A Block B
Example 8

Find the largest of three numbers using nested if

# Program to find the largest of three numbers

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))

if a >= b:
    if a >= c:
        largest = a
    else:
        largest = c
else:
    if b >= c:
        largest = b
    else:
        largest = c

print("The largest number is:", largest)
▶ Sample Output:
Enter first number: 15
Enter second number: 42
Enter third number: 27
The largest number is: 42

Explanation: First, compare a and b. If a >= b, then compare a with c. Otherwise, compare b with c.

Example 9

Check whether a year is a leap year

# Program to check leap year

year = int(input("Enter a year: "))

if year % 4 == 0:
    if year % 100 == 0:
        if year % 400 == 0:
            print(year, "is a leap year.")
        else:
            print(year, "is not a leap year.")
    else:
        print(year, "is a leap year.")
else:
    print(year, "is not a leap year.")
▶ Sample Output 1:
Enter a year: 2024
2024 is a leap year.

▶ Sample Output 2:
Enter a year: 1900
1900 is not a leap year.

▶ Sample Output 3:
Enter a year: 2000
2000 is a leap year.

Logic: A year is a leap year if:

Example 10

Check whether a number is positive, negative, or zero — and even/odd

# Nested if: sign and parity

num = int(input("Enter a number: "))

if num > 0:
    print("Positive number")
    if num % 2 == 0:
        print("Even")
    else:
        print("Odd")
elif num < 0:
    print("Negative number")
    if num % 2 == 0:
        print("Even")
    else:
        print("Odd")
else:
    print("Zero")
▶ Sample Output:
Enter a number: -6
Negative number
Even

6. Comparison of Decision-Making Statements

StatementUse CaseBlocks Executed
ifSingle condition0 or 1
if-elseTwo-way decisionExactly 1
if-elif-elseMultiple mutually exclusive conditionsExactly 1 (or 0 if no else)
Nested ifHierarchical / dependent conditionsDepends on conditions

7. Common Mistakes to Avoid

MistakeCorrection
Forgetting the colon :Always end the condition with :
Incorrect indentationUse consistent 4 spaces for each block
Using = instead of === is assignment; == is comparison
Writing elif as else ifPython uses elif
Overlapping conditions in elifOrder matters — put the most specific first
Empty blockUse pass if a block must be empty

Example of pass

if x > 0:
    pass   # to be implemented later
else:
    print("Non-positive")

8. Summary Table

ConceptSyntaxExample
ifif cond:if x > 0: print("+")
if-elseif cond: ... else: ...if x%2==0: ... else: ...
if-elif-elseif c1: ... elif c2: ... else: ...Grade calculator
Nested ifif c1: if c2: ...Largest of 3 numbers

9. Practice Exercises

  1. Write a program to check whether a number is divisible by both 3 and 5.
  2. Write a program to input a character and check whether it is uppercase, lowercase, a digit, or a special character.
  3. Write a program to find the smallest of four numbers using nested if.
  4. Write a program that takes a student's marks in 5 subjects and prints the grade based on average.
  5. Write a program to check whether a triangle is equilateral, isosceles, or scalene based on its three sides.
  6. Write a program to determine the roots of a quadratic equation ax² + bx + c = 0 (real or complex).
  7. Write a program to simulate a simple ATM menu using if-elif-else.

10. Key Takeaways


Module III — Flow Control · Topic (a): Decision Making · Detailed Teaching Notes