Control Statements in Python
2.7 if Statement
Core Concept
if is used for decision making based on conditions.
Basic Syntax
if condition:
statement
Flow
• Condition → True → block runs
• Condition → False → skip
Example
age = 20
if age >= 18:
print("Adult")
if-else
if age >= 18:
print("Adult")
else:
print("Minor")
if-elif-else (Multiple Conditions)
marks = 75
if marks >= 80:
print("A")
elif marks >= 60:
print("B")
else:
print("Fail")
Nested if
age = 20
citizen = True
if age >= 18:
if citizen:
print("Eligible to vote")
Example
Like real-life decision:
• If raining → take umbrella
• Else → go normally
Common Mistakes
• Missing colon :
• Wrong indentation
• Using = instead of ==
2.8 match Statement
Core Concept
match is used for pattern matching (like switch-case in other languages)
Basic Syntax
match value:
case pattern1:
statement
case pattern2:
statement
Example
day = 2
match day:
case 1:
print("Sunday")
case 2:
print("Monday")
case _:
print("Invalid")
Important Points
• _ = default case
• Cleaner than multiple if-elif
Advanced Matching
num = 10
match num:
case 0:
print("Zero")
case _ if num > 0:
print("Positive")
case _:
print("Negative")
Example
Like menu selection:
• Press 1 → English
• Press 2 → Nepali
• Else → Invalid
When to Use
• Many conditions on same variable
• Cleaner than long if-elif
2.9 break Statement
Core Concept
break is used to exit a loop immediately
Example with loop
for i in range(5):
if i == 3:
break
print(i)
Output:
012
Concept
• Break stops loop completely
• Used in:
• Searching
• Menu systems
• Early exit conditions
Example
Like:
• You are searching a book
• Once found → stop searching
Important
• Works only inside loops
• Break exits entire loop, not just condition
Summary
Statement Purpose
if Decision making
match Multi-case decision
break Exit loop early
Example
while True:
choice = int(input("Enter option (1-3): "))
match choice:
case 1:
print("Option 1 selected")
case 2:
print("Option 2 selected")
case 3:
print("Exiting...")
break
case _:
print("Invalid choice")
Summary
• if → decide what to do
• match → choose from many options
• break → stop immediately
Exam Tips
• Always use correct indentation
• Use match instead of long if-elif (modern Python)
• Use break carefully inside loops
2.10 continue Statement
Core Concept
continue is used to skip current iteration and move to the next loop cycle
Syntax
for/while:
if condition:
continue
Example
for i in range(5):
if i == 2:
continue
print(i)
Output:
0134
2 is skipped
Example
Teacher calling roll numbers
• Skip absent student → move to next
Di erence: break vs continue
break continue
Stops loop completely Skips one iteration
Exits loop Goes to next loop
2.11 Loop Statement
Core Concept
Loops are used to repeat a block of code multiple times
Why loops?
Avoid writing same code again and again
Example
Like:
• Eating 5 biscuits → repeat same action
• Loop automates repetition
2.11.1 while Loop
Concept
Runs as long as condition is True
Syntax
while condition:
statement
Example
i=1
while i <= 5:
print(i)
i += 1
Important Points
• Condition checked before execution
• Can become in nite if not controlled
Example
While you have money → keep shopping
ff
fi
2.11.2 for Loop
Concept
Used to iterate over sequence (list, range, string)
Syntax
for variable in sequence:
statement
Example
for i in range(5):
print(i)
Advanced Use
for char in "Python":
print(char)
Example
Reading each letter in a word
while vs for
while for
Condition-based Sequence-based
Unknown iterations Known iterations
2.12 Nested Loop
Concept
Loop inside another loop
Example
for i in range(3):
for j in range(2):
print(i, j)
Execution Flow
Outer loop runs → inner loop completes fully → repeat
Example
School:
• For each class → check each student
Pattern Example
for i in range(3):
for j in range(3):
print("*", end=" ")
print()
2.13 In nite Loop
Concept
Loop that never stops
Example
while True:
print("Hello")
Why dangerous?
• Program never ends
• System may hang
How to control
Use:
• break
• Proper condition
Example
Like a fan running forever unless switched o
Combined Example
while True:
num = int(input("Enter number (0 to exit): "))
if num == 0:
break
elif num % 2 == 0:
continue
print(f"Odd number: {num}")
• continue → skip iteration
• while → condition loop
• for → sequence loop
• nested loop → loop inside loop
• in nite loop → runs forever
fi
fi
ff