Control Flow in Python: if-else and match-case with Examples
#Control Flow in Python: if-else 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 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)
The if-else statement allows you to execute certain code blocks when a condition is met.
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
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).
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.
match variable:
case value1:
# code block if variable == value1
case value2:
# code block if variable == value2
case _:
# default case if no matches found
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.
Feature | if-else | match-case |
---|---|---|
Flexibility | Can handle complex conditions | Best for fixed values/patterns |
Code Readability | Can become lengthy with multiple elifs | Cleaner and concise |
Availability | Available in all Python versions | Only available from Python 3.10 onwards |
# 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
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.
Python control flow
Python if else example
Python match case example
Python decision making statements
Python switch case alternative