Introduction to Control Flow
Control flow refers to the order in which statements in a program are executed. By default, Python executes code sequentially, meaning line by line from top to bottom. However, real-world problems require decision-making, and this is where control flow statements come into play.
Control flow allows the program to:
- Make decisions based on conditions
- Execute specific code blocks only when required
- Skip unnecessary operations
- Handle multiple scenarios efficiently
Example (Without Control Flow)
print("Welcome")
print("Checking age")
print("Access granted")
Example (With Control Flow)
age = 16if age >= 18:
print("Access granted")
Key Concepts
- Conditions must evaluate to
TrueorFalse - Indentation defines code blocks
- Control flow improves program flexibility
- if Statement
The if statement is the simplest form of control flow. It executes a block of code only when a condition is true.
Syntax
if condition:
# code block
Example
temperature = 30if temperature > 25:
print("It is a hot day")
Multiple Conditions
marks = 70if marks > 50 and marks <= 100:
print("You passed")
Using if with Strings
name = "Pooja"if name == "Pooja":
print("Welcome Pooja")
Important Points
- Indentation is mandatory in Python
- Conditions can use comparison and logical operators
- Code inside
ifruns only if the condition is true
- if-else Statement
The if-else statement is used when there are exactly two possible outcomes.
Syntax
if condition:
# executes if condition is true
else:
# executes if condition is false
Example
number = 7if number % 2 == 0:
print("Even number")
else:
print("Odd number")
Example with User Input
age = int(input("Enter your age: "))if age >= 18:
print("Eligible to vote")
else:
print("Not eligible")
Real-Life Use Case
- Checking login credentials
- Validating user input
- Determining eligibility
Common Mistakes
- Using
=instead of== - Incorrect indentation
- Forgetting colon
:
- if-elif-else Ladder
When there are multiple conditions to check, the if-elif-else ladder is used.
Syntax
if condition1:
# code block
elif condition2:
# code block
elif condition3:
# code block
else:
# default block
Example (Student Grades)
marks = 82if marks >= 90:
print("Grade A")
elif marks >= 75:
print("Grade B")
elif marks >= 50:
print("Grade C")
else:
print("Fail")
How it Works
- Conditions are checked from top to bottom
- Once a condition is true, remaining conditions are skipped
- Only one block executes
Best Practices
- Order conditions properly (highest to lowest)
- Avoid overlapping conditions
- Keep conditions simple and readable
- Nested Conditions
Nested conditions involve placing one if statement inside another. This is useful when a decision depends on multiple levels of conditions.
Syntax
if condition1:
if condition2:
# code block
Example
age = 20
has_id = Trueif age >= 18:
if has_id:
print("Entry allowed")
Real Example
num = int(input("Enter a number: "))if num >= 0:
if num == 0:
print("Number is Zero")
else:
print("Number is Positive")
else:
print("Number is Negative")
Advantages
- Helps in handling complex decision-making
- Allows step-by-step validation
Disadvantages
- Too many nested conditions reduce readability
- Makes code harder to maintain
Alternative Approach
Use logical operators instead:
if age >= 18 and has_id:
print("Entry allowed")
Conclusion
Control flow statements are fundamental to programming in Python. They allow programs to make decisions, handle different scenarios, and execute code intelligently. Concepts like if, if-else, and if-elif-else are used in almost every real-world application.
By mastering control flow, learners can write dynamic programs that respond to user input and changing conditions. Understanding when and how to use these statements effectively will significantly improve coding skills and prepare learners for advanced topics like loops, functions, and real-world problem-solving.
Leave a Reply