Control Flow in Python: if-else and match-case with Examples

8/16/2025

#Control Flow in Python: if-else match-case with Examples

Go Back

Control Flow in Python: if-else and match-case with Examples

Control flow is the backbone of any programming language, and in Python, it allows developers to control how code is executed based on certain conditions. The most commonly used control flow statements are if-else and the newer match-case statement introduced in Python 3.10.

In this guide, we’ll cover both control structures in detail, along with practical examples to help you write clean and efficient Python programs.


#Control Flow in Python: if-else  match-case with Examples

What is Control Flow in Python?

Control flow refers to the order in which individual statements, instructions, or function calls are executed in a program. By using decision-making statements, Python allows developers to define how the program should respond under different conditions.

The two most important decision-making statements are:

  • if-else (used for conditional branching)

  • match-case (used for pattern matching)


if-else Statement in Python

The if-else statement allows you to execute certain code blocks when a condition is met.

Syntax:

if condition:
    # code block if condition is True
elif another_condition:
    # code block if another condition is True
else:
    # code block if all conditions are False

Example:

age = 18

if age < 18:
    print("You are a minor.")
elif age == 18:
    print("You just became an adult!")
else:
    print("You are an adult.")

Output:

You just became an adult!

Use Case: Useful when multiple conditions need to be checked (e.g., validating user input, decision-making in applications).


match-case Statement in Python

The match-case statement (introduced in Python 3.10) is similar to switch-case in other languages like C, C++, or Java. It simplifies checking against multiple possible values.

Syntax:

match variable:
    case value1:
        # code block if variable == value1
    case value2:
        # code block if variable == value2
    case _:
        # default case if no matches found

Example:

day = "Monday"

match day:
    case "Monday":
        print("Start of the work week.")
    case "Friday":
        print("End of the work week!")
    case "Saturday" | "Sunday":
        print("Weekend!")
    case _:
        print("Midweek day.")

Output:

Start of the work week.

Use Case: Best when you want to compare one variable against multiple patterns without writing long chains of if-elif statements.


Key Differences Between if-else and match-case

Featureif-elsematch-case
FlexibilityCan handle complex conditionsBest for fixed values/patterns
Code ReadabilityCan become lengthy with multiple elifsCleaner and concise
AvailabilityAvailable in all Python versionsOnly available from Python 3.10 onwards

Real-World Example:

# Using if-else
score = 85
if score >= 90:
    grade = "A"
elif score >= 75:
    grade = "B"
elif score >= 60:
    grade = "C"
else:
    grade = "F"

print("Grade using if-else:", grade)

# Using match-case
match score:
    case s if s >= 90:
        grade = "A"
    case s if s >= 75:
        grade = "B"
    case s if s >= 60:
        grade = "C"
    case _:
        grade = "F"

print("Grade using match-case:", grade)

Output:

Grade using if-else: B
Grade using match-case: B

Conclusion

Control flow in Python is essential for writing programs that can make decisions. The if-else statement is ideal for conditional logic and has been widely used since Python’s early days. The match-case statement, however, makes code cleaner and easier to read when dealing with multiple patterns.

👉 Whether you’re writing simple scripts or large-scale applications, mastering Python control flow will make your code more efficient and maintainable.


🔑 SEO Keywords to Target:

  • Python control flow

  • Python if else example

  • Python match case example

  • Python decision making statements

  • Python switch case alternative

Table of content