Control structures in Python determine the flow of execution of a program. They include conditional statements (if, if-else, if-elif-else, nested if) and loops (for, while, nested loops, break, continue).
1. Conditional Statements
Conditional statements allow a program to make decisions based on conditions.
a. if Conditional Statement
The if statement executes a block of code only if the condition is True.
Syntax:
if condition:
# Code block to execute if the condition is True
Example:
x = 10
if x > 5:
print(“x is greater than 5”)
Output:
x is greater than 5
b. if-else Condition
The if-else statement executes one block of code if the condition is True and another block if it is False.
Syntax:
if condition:
# Code block if condition is True
else:
# Code block if condition is False
Example:
age = 18
if age >= 18:
print(“You are eligible to vote.”)
else:
print(“You are not eligible to vote.”)
Output:
You are eligible to vote.
c. if-elif-else Condition
The if-elif-else statement checks multiple conditions one by one.
Syntax:
if condition1:
# Code if condition1 is True
elif condition2:
# Code if condition2 is True
else:
# Code if no conditions are True
Example:
marks = 85
if marks >= 90:
print(“Grade: A”)
elif marks >= 80:
print(“Grade: B”)
elif marks >= 70:
print(“Grade: C”)
else:
print(“Grade: D”)
Output:
Grade: B
d. Nested if-elif-else Condition
A nested if statement is an if statement inside another if, elif, or else block.
Syntax:
if condition1:
if condition2:
# Code block if both conditions are True
else:
# Code block if condition1 is True, but condition2 is False
else:
# Code block if condition1 is False
Example:
x = 10
y = 5
if x > 5:
if y > 2:
print(“Both conditions are True”)
else:
print(“Inner condition is False”)
else:
print(“Outer condition is False”)
Output:
Both conditions are True
2. Iteration (Loops)
Loops allow repeating a block of code multiple times.
a. for Loop
A for loop iterates over a sequence (list, tuple, string, or range).
Syntax:
for variable in sequence:
# Code block to execute
Example:
for i in range(1, 6):
print(i)
Output:
1
2
3
4
5
Looping through a List:
fruits = [“apple”, “banana”, “cherry”]
for fruit in fruits:
print(fruit)
Output:
apple
banana
cherry
b. while Loop
A while loop executes a block of code as long as the condition is True.
Syntax:
while condition:
# Code block to execute
Example:
i = 1
while i <= 5:
print(i)
i += 1
Output:
1
2
3
4
5
3. Nested Loops
A loop inside another loop is called a nested loop.
Syntax:
for outer in range(x):
for inner in range(y):
# Code block to execute
Example:
for i in range(1, 4):
for j in range(1, 4):
print(i, j)
Output:
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
4. Control Statements: Break and Continue
Python provides break and continue statements to control loop execution.
a. break Statement
The break statement exits the loop immediately.
Example:
for i in range(1, 6):
if i == 4:
break
print(i)
Output:
1
2
3
Loop stops when i becomes 4)
b. continue Statement
The continue statement skips the current iteration and moves to the next.
Example:
for i in range(1, 6):
if i == 3:
continue
print(i)
Output:
1
2
4
5
(i = 3 is skipped)