Computer Science · Ch 6 — Flow of Control
Introduction
Introduction
Every program has an order in which its statements run, and controlling that order is what this chapter is about. The textbook opens with a simple picture: a school bus taking children to school along a road that offers exactly one route (Figure 6.1). The driver cannot choose an alternative — the bus passes one milestone after another until it reaches the school. This is precisely how the programs written so far behave: Python executes one statement after another, from the first line of the program to the last. This straight-line behaviour is called sequence, a concept introduced in Chapter 5.
Flow of control
The order in which the statements of a program are executed is called the flow of control. Up to now, that order has always been "top to bottom, one statement at a time". A purely sequential program looks like this:
# Print the difference of two input numbers
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
result = a - b
print("The difference of", a, "and", b, "is", result)
Enter first number: 5
Enter second number: 7
The difference of 5 and 7 is -2
Each line runs exactly once, in the order written: read the first number, read the second, compute the difference, print it. Notice that when the second number is larger, the program happily prints a negative difference — it has no way to "decide" to do anything different. That limitation motivates the rest of the chapter.
Control structures
The flow of control of a program is shaped using control structures. Python supports two types:
- Selection — the program chooses between alternative paths based on a condition (decision making, covered in Section 6.2).
- Repetition — the program executes a set of statements again and again as long as a condition holds (looping, covered in Section 6.4).
The chapter also examines indentation (how Python marks which statements belong together in a block), the break and continue statements (which alter the normal behaviour of loops), and nested loops (a loop placed inside another loop).
A remark by Guido van Rossum, the creator of Python, is quoted in the book: making indentation part of the language's syntax guarantees that all code is properly indented — a hint of why Python treats leading whitespace so seriously, as Section 6.3 explains.
Drawn by us to help you understand the concept clearly, and verified to make sure it's accurate. For exams, practice from your NCERT textbook's own diagram.
Figure 6.1 is a colour illustration — not a flowchart — used to make the idea of sequential execution concrete before any code appears. It shows a yellow school bus, labelled "SCHOOL BUS", travelling along a single winding grey road with a dashed centre line, through a green landscape dotted with trees, lamp posts, a park bench, children and parents walking, and cyclists. Along the road, five yellow location-pin markers carrying a bus icon mark the stops the bus passes. The road ends at a pink school building with a red roof, labelled "School".
The teaching point lies in the geography of the picture: there is only one way from the starting point to the school. The driver has no choice of route — the bus must follow the one road, passing each stop (each milestone) one after another, in order, until it arrives.
This is exactly the concept of sequence from Chapter 5: Python executes the statements of a program one after another, from the beginning to the end, just as the bus visits each marker in turn with no possibility of skipping ahead, turning back, or taking a different path. Every program written before this chapter behaves like this bus. The figure sets up the chapter's central question — what if the program should be able to choose a path (selection) or travel a stretch repeatedly (repetition)? Those are the control structures the rest of the chapter introduces.