ЁЯТ╗
тМия╕П
ЁЯЦ▒я╕П
ЁЯЦея╕П
ЁЯТ╛
тЖР Back to Dashboard
Font Size:

1. Introduction

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.

2. Sequential Flow

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.

3. Selection Statements (Decision Making)

3.1 The if Statement

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

3.2 The if-else Statement

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

3.3 The if-elif-else Statement

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.

4. Logical Conditions

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.

5. Iteration (Loops)

Loops allow a block of statements to be executed repeatedly. Python provides two loops: while and for.

5.1 The while Loop

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.

5.2 The for Loop

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]

5.3 Iterating over Sequences

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

6. Jump Statements

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.

7. Loop with else

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")

Quick Revision Tables

Table 1: Control Structures Summary

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

Table 2: range() Function Variations

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

Mind Map

graph TD A["Python Control Structures"] --> B["Sequential"] A --> C["Selection"] A --> D["Iteration"] A --> E["Jump Statements"] C --> C1["if"] C --> C2["if-else"] C --> C3["if-elif-else"] D --> D1["while loop"] D --> D2["for loop with range()"] D --> D3["Loop with else"] E --> E1["break"] E --> E2["continue"] E --> E3["pass"]

Important Diagrams (SVG)

Diagram 1: if-elif-else Decision Flowchart

if-elif-else Decision Flow Start marks >= 90? (Condition 1) Grade = A marks >= 75? (Condition 2) Yes No Grade = B Grade = D (else) Print Grade Stop Golden Rule Conditions check top to bottom; first true block runs

Diagram 2: while Loop Flowchart

while Loop Flow Start i = 1 i <= 5 ? (Condition) Yes No Loop Body print(i); i = i + 1 Back to condition Exit Loop When condition false Stop Golden Rule The loop body must change the condition, otherwise the loop never ends

Common Mistakes

  1. Forgetting the colon : at the end of if, while, for, else, and elif lines, which causes a syntax error.
  2. Using inconsistent indentation inside blocks; Python relies on indentation to define blocks.
  3. Writing else if instead of elif, which is invalid in Python.
  4. Creating an infinite while loop by forgetting to update the loop variable.
  5. Misusing range() by confusing the stop value, which is always excluded.
  6. Using continue when a break was intended, causing only part of the loop to be skipped.
  7. Assuming switch works in Python; it does not, and if-elif-else is the correct approach.
  8. Putting a statement after else: on the same line or using wrong indentation for else.

Exam Tips

  1. Memorise that range(1, 6) generates 1 to 5, since the stop value is always excluded.
  2. Practise predicting the output of break and continue programs; these are favourite exam questions.
  3. Remember that Python has no switch statement; explain if-elif-else as the alternative.
  4. Draw the flowchart for if-else and while loops; marks are often awarded for the diagram.
  5. Trace loops on paper by writing down the value of the loop variable at each step.
  6. Remember that the else after a loop runs only when the loop ends without a break.

Conclusion

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.