Control Flow in Python – if, elif, else, and Loops

August 02, 2025   6 Min Read 30

What You'll Learn in This Blog:

  • What is control flow and why it matters
  • if, elif, else statements
  • Logical and comparison operators
  • for and while loops
  • break, continue, pass statements
  • Nested conditions and loops
  • Best practices
  • Practice task to apply what you've learned

What is Control Flow in Python?

Control flow refers to the order in which your code is executed. By default, Python runs code line by line from top to bottom. Control flow allows you to make decisions, repeat tasks, and react to data, making your programs dynamic.

Conditional Statements: if, elif, else

Basic if Statement


age = 18
if age >= 19:
	print("You are eligible to vote.")

If the condition evaluates to True, the block inside the if runs.

if...else Statement


age = 16
if age >= 18:
	print("You are eligible to vote.")
else:
	print("Sorry, you must be at least 18.")

Executes the else block if the conditions is False.

if...elif...else Chain


marks = 85
if marks >= 90:
	print("Grade: A")
elif marks >= 75:
	print("Grade: B")
else:
	print("Grade: C or lower")

checks multiple conditions in sequence.

Comparison & Logical Operators

OperatorDescriptionExample
==Equal toa == b
!=Not equal toa != b
>Greater thana > b
<Less thana < b
>=Greater or equala >= b
<=Less or equala <= b

Logical Operators

OperatorDescriptionExample
andAll conditions must be Truea > 5 and b < 10
orAt least one must be Truea > 5 or b < 10
notReverses condition resultnot a == b

Loops in Python

for Loop

Used to iterate over sequence (lists, strings, ranges, etc.)


for i in range(5):
	print(i)

Output: 0 1 2 3 4

while Loop

Repeats a block of code as long as a condition is True.


i = 0
while i < 5:
	print(i)
	i += 1

Same Output: 0 1 2 3 4

Loop Control Statements

StatementPurposeExample Usage
breakExit the loop earlyStop loop when a condition is met
continueSkip the current iterationSkip even numbers in a loop
passPlaceholder for future codeUsed where code is syntactically required but not yet implemented

Examples:


# break example
for i in range(10):
	if i == 5:
		break
	print(i)  # Prints 0 to 4

# continue exmaple
for i in range(5):
	if i == 2:
		continue
	print(i)  # Skips printing 2

# pass example
for i in range(3):
	pass  # Does nothing, placeholder

Nested Control Flow

Example: Nested if inside a for loop


for num in range(1, 6):
	if num % 2 == 0:
		print(f"{num} is even")
	else:
		print(f"{num} is odd")

Output:


1 is odd
2 is even
3 is odd
4 is even
5 is odd

Best Practices

  • Use clear and descriptive conditions
  • Avoid deeply nested control flow
  • Prefer for loops for finite sequences
  • Always use meaningful indentation (Python enforces it)

Task

🔸 Problem: Ask the user to enter a number and check the following:

  1. Is it divisible by 3 and 5?
  2. Is it even or odd?
  3. Print all even numbers from 0 to that number.

Example:


# Sample Output:
# Enter a number: 15
# Divisible by both 3 and 5
# The number is odd
# Even numbers: 0 2 4 6 8 10 12 14

Practice Tasks

  1. Write a program that checks whether a number is positive, negative, or zero.
  2. Write a program to classify age groups: child (0-12), teen (13–19), adult (20–59), senior (60+).
  3. Accept a username and password and check for a match.
  4. Write a program that asks for a year and checks if it's a leap year.
  5. Check whether a number is even and greater than 50.

Summary

TopicExample
if, elseif x > 0: print("Positive")
elifelif x == 0: print("Zero")
Loopsfor i in range(5):
break / continuebreak to stop loop, continue to skip
Comparisonx >= y
Logicalx > 0 and y < 10

Leave a Reply