Flow of Control in Python – Sequence, Selection & Iteration


Understand Flow of Control in Python in very simple language, step by step. Learn sequence, selection (conditions), and iteration (loops) with real-life examples.

In any program, instructions are normally executed one after another, from the beginning to the end. But real-life programs don’t always follow this simple sequence.

Sometimes a program needs to change its execution based on a condition. For example, if a condition is true, one statement should execute, and if it is false, a different statement should execute. In other situations, a program needs to repeat a single statement or a group of statements again and again.

To handle such situations, Python provides program control statements. With their help, we can decide the order in which a program’s execution will take place.

In this chapter, we will learn about the following flow of control statements in Python:

  • Selection Statements
  • Iteration Statements
  • Jump Statements

After learning all these statements, you will be able to easily control the execution flow of any program.

What is Flow of Control – Python Programming

Flow of control means the order in which a program’s statements will execute.

Normally, in a Python program, statements execute from top to bottom, one after another. But while solving real-life problems, we often need to make a decision or repeat a task multiple time. To handle such situations, Python provides various control structures that change the normal execution flow of a program.

Python mainly has three types of control structures:

  1. Sequence
  2. Selection (Decision Making)
  3. Iteration (Looping)

Sequence Construct

The sequence construct is the default flow of program execution in Python. Here, all statements execute in the same order in which they are written in the program.

How it Works

The program first executes the first statement. Then it moves to the next statement, and in this way, executing statements one by one, it reaches the last statement.

Real-Life Example

Suppose you are making a sandwich.

  1. Take two slices of bread.
  2. Spread butter on both slices.
  3. Place the filling (vegetables/cheese) between the slices.
  4. Cut the sandwich and serve it.

Here, all the steps are completed in a fixed order. If you skip a step or change their order, you won’t get the correct result. In the same way, in a sequence construct too, all statements execute in a fixed order.

Python Example

length = 12
width = 5
area = length * width
print(“The area of the rectangle is:”, area)

Output

The area of the rectangle is: 60

Selection Construct (Decision Making)

With the help of the selection construct, a program can choose one path out of several possible paths (branches) based on a condition. In other words, the program decides which statement or block should execute.

How it Works

The program first checks a condition.

  • If the condition is True, a certain block of statements executes.
  • If the condition is False, another block executes (if available).

In Python, this type of control flow is implemented with the help of if, if-else, and if-elif-else statements.

Real-Life Example

Think about traffic signal.

  • If signal is red, then stop.
  • If signal is green, then move.

Here, the action taken depends on the condition, that is, the weight. In the same way, a program also makes decisions based on a condition.

Python Example

temperature = 40
if temperature >= 38:
    print(“It’s a hot day, stay hydrated.”)
else:
    print(“The weather is pleasant today.”)

Output

It’s a hot day, stay hydrated.

Iteration Construct (Looping)

The iteration construct is used to execute a statement or block of statements repeatedly. This process continues until the given condition becomes False.

How it Works

Before or during each cycle (iteration) of the loop, the condition is checked.

  • If the condition is True, the loop executes again.
  • When the condition becomes False, the loop ends, and the program moves on to execute the next statement.

In Python, for and while loops are mainly used for iteration.

Real-Life Example

Suppose you are watering a row of plants.

As long as there are plants left in the row, the following steps will be repeated:

  1. Walk up to the next plant.
  2. Pour water into its pot.
  3. Check if the soil looks moist enough.
  4. Move on to the next plant.

All these steps are repeated again and again until every plant in the row has been watered. In the same way, in an iteration construct too, statements are executed repeatedly.

Python Example

plant = 1
while plant <= 4:
    print(“Watering plant number”, plant)
    plant = plant + 1

Output

Watering plant number 1
Watering plant number 2
Watering plant number 3
Watering plant number 4

Summary of Control Structures

Control StructureHow it WorksWhat it is Used For
SequenceStatements execute in the same order in which they are written in the program.To execute a task step by step.
SelectionChooses one out of several different paths based on a condition.For decision making, with the help of if, if-else, and if-elif-else.
IterationExecutes a block of statements repeatedly until the condition becomes False.To repeat a task again and again, with the help of for and while loops.

Leave a Comment

Your email address will not be published. Required fields are marked *

error: Content is protected !!
Scroll to Top