Programs rarely execute in a straight line from the first statement to the last. In real-world problem solving, a program must make decisions, repeat actions, and choose between different paths depending on the data it receives. The tools that allow this are called control structures. They determine the flow of execution of a program and are broadly classified as sequential, selection (conditional), and iterative (loop) structures.
Python provides the if, if-else, and if-elif-else statements for decision making, and the while and for loops for repetition. Every conditional statement and loop in Python depends on a condition, which is an expression that evaluates to True or False. The concept of truthiness also plays an important role: in Python, any non-zero number, non-empty string, or non-empty collection is considered true, while zero, empty strings, and empty collections are considered false.
This chapter explains these control structures in detail with appropriate examples. We will learn how to write compound conditions using logical operators, how to control loops with break, continue, and pass, and how the range() function works with for loops. Mastering control structures is the key step that transforms a beginner who can write linear programs into a programmer who can write algorithms that actually solve problems.
By default, Python executes statements in the order in which they appear, from top to bottom. This is known as sequential flow.
print("First")
print("Second")
print("Third")
Output:
First
Second
Third
Every program has a sequential part, but real problems require decisions and repetition, which are handled by selection and iteration structures.
The if statement executes a block of code only when the condition is true. The block must be indented.
age = 18
if age >= 18:
print("You can vote")
Output:
You can vote
The else block executes when the condition is false.
age = 16
if age >= 18:
print("You can vote")
else:
print("You cannot vote yet")
Output:
You cannot vote yet
When there are multiple conditions to check, elif (short for else if) is used. Conditions are checked from top to bottom and the first true one is executed.
marks = 85
if marks >= 90:
grade = "A"
elif marks >= 75:
grade = "B"
elif marks >= 60:
grade = "C"
else:
grade = "D"
print("Grade:", grade)
Output:
Grade: B
Python does not have a switch statement like C or Java; if-elif-else is the standard way to handle multiple branches.
Conditions can be combined using the logical operators and, or, and not.
age = 20
has_id = True
if age >= 18 and has_id:
print("Eligible for the exam")
The condition above is true only when both sub-conditions are true. Nested if statements can also be used, but logical operators often make the code cleaner.
Loops allow a block of statements to be executed repeatedly. Python provides two loops: while and for.
The while loop repeats a block as long as the condition remains true. It is most useful when the number of iterations is not known in advance.
i = 1
while i <= 5:
print(i)
i = i + 1
Output:
1
2
3
4
5
The loop condition must eventually become false, otherwise an infinite loop occurs.
The for loop iterates over a sequence such as a list, string, tuple, or a range() of numbers. It is used when the number of iterations is known or when we want to process each element of a sequence.
for i in range(1, 6):
print(i)
Output:
1
2
3
4
5
The range() function generates a sequence of numbers. range(start, stop, step) includes start, excludes stop, and advances by step. By default step is 1 and start is 0.
print(list(range(5)))
print(list(range(2, 10, 2)))
Output:
[0, 1, 2, 3, 4]
[2, 4, 6, 8]
A for loop can directly process the elements of a string, list, or tuple:
name = "Riya"
for ch in name:
print(ch)
Output:
R
i
y
a
for i in range(1, 10):
if i == 5:
break
print(i)
Output:
1
2
3
4
Here the loop stops completely when i becomes 5.
for i in range(1, 6):
if i == 3:
continue
print(i)
Output:
1
2
4
5
Here the iteration with i == 3 is skipped, but the loop continues.
Python allows an else block after a loop. The else block executes only when the loop terminates normally (without a break).
for i in range(3):
print(i)
else:
print("Loop completed normally")
| Structure | Keyword(s) | Use |
|---|---|---|
| Sequential | none | Statements run top to bottom |
| Selection | if, else, elif |
Choose one path among many |
| Iteration | while, for |
Repeat a block of statements |
| Jump | break, continue, pass |
Alter normal loop flow |
| Expression | Result | Meaning |
|---|---|---|
range(5) |
0, 1, 2, 3, 4 | Start 0, stop 5, step 1 |
range(1, 6) |
1, 2, 3, 4, 5 | Start 1, stop 6 |
range(2, 10, 2) |
2, 4, 6, 8 | Start 2, stop 10, step 2 |
range(10, 0, -2) |
10, 8, 6, 4, 2 | Negative step for reverse |
: at the end of if, while, for, else, and elif lines, which causes a syntax error.else if instead of elif, which is invalid in Python.while loop by forgetting to update the loop variable.range() by confusing the stop value, which is always excluded.continue when a break was intended, causing only part of the loop to be skipped.switch works in Python; it does not, and if-elif-else is the correct approach.else: on the same line or using wrong indentation for else.range(1, 6) generates 1 to 5, since the stop value is always excluded.break and continue programs; these are favourite exam questions.if-elif-else as the alternative.else after a loop runs only when the loop ends without a break.Control structures give a program the ability to make decisions and repeat actions, turning simple linear code into true algorithms. The if-elif-else ladder handles selection, while and for loops handle repetition, and break, continue, and pass provide finer control over the flow. Since Python relies on indentation and the colon to define blocks, careful attention to syntax is essential. With these tools, we can now write programs that respond to user input, process collections of data, and solve problems of real complexity. The next chapters on lists and dictionaries build on exactly these ideas.