What are control flow statements in Python?

Key takeaways:

  • Control flow regulates the execution order of code using sequential statements, decision-making (e.g., if, if-else, nested if, if-elif-else), and loops (for and while).

  • Selection statements like if and if-else allow programs to execute specific code blocks based on conditions, while nested and if-elif-else statements enable more complex decision-making.

  • Loops like for and while enable code blocks to execute repeatedly, making it easier to handle iterative tasks efficiently.

A program’s control flow is the order in which the program’s code executes. The control flow of a Python program is regulated by conditional statements, loops, and function calls.

Python has three types of control structures:

  • Sequential: Default mode
  • Selection: Used for decisions and branching
  • Repetition: Used for looping, i.e., repeating a code multiple times.

1. Sequential

Sequential statements are statements whose execution process happens in a sequence. The problem with sequential statements is that if the logic has broken in any of the lines, the complete source code execution will fail.

## This is a Sequential statement
a=20
b=10
c=a-b
print("Subtraction is : ",c)

2. Selection/decision control statements

In Python, selection statements are also known as Decision control statements or branching statements.

The selection statement allows a program to test several conditions and execute instructions based on which condition is true.

Some decision control statements are as follows:

  • if
  • if-else
  • nested if
  • if-elif-else

if

It helps us run a particular code only when a certain condition is met or satisfied. A if only has one condition to check.

Depiction of if statement
Depiction of if statement

In the following code example, we’re checking the if condition, e.g., if n modulo 2 equals to 0 then the print statement will execute.

n = 10
if n % 2 == 0:
print("n is an even number")

if-else

The if-else statement evaluates the condition and will execute the body of if if the test condition is True, but if the condition is False, then the body of else is executed.

Depiction of if-else statement
Depiction of if-else statement

In the following code example, the else body will execute as 5 modulo 2 is not equal to 0.

n = 5
if n % 2 == 0:
print("n is even")
else:
print("n is odd")

Nested if

Nested if statements are an if statement inside another if statement.

Depiction of nested if statement
Depiction of nested if statement

In the following code example, the first if checks if a is greater than b. The else block contains a nested if that checks if b is greater than a. If neither condition is true, it prints that a and b are equal. This is a simple use case for nested if with two variables.

# Assign values to variables a and b
a = 10
b = 20
# Compare the two numbers using nested if statements
if a > b:
print("a is greater than b")
else:
if a < b:
print("b is greater than a")
else:
print("a and b are equal")

if-elif-else

The if-elif-else statement is used to conditionally execute a statement or a block of statements.

Depiction of if-elif-else statement
Depiction of if-elif-else statement

In the code provided, the initial if statement checks whether 15 is equal to 12. If not, the elif condition will verify that 15 is greater than 12. If this condition is true, the code block inside elif will be executed. Otherwise, the code in the else block will be executed if none of the preceding conditions are met.

x = 15
y = 12
if x == y:
print("Both are Equal")
elif x > y:
print("x is greater than y")
else:
print("x is smaller than y")

If you're eager to deepen your understanding of Python and sharpen your problem-solving skills, our “Learn to Code: Become a Software Engineer” path is the perfect learning resource. With this structured path, you’ll go beyond fundamental concepts like generators and dive into advanced topics that will prepare you for a successful career in software engineering.

3. Repetition

A repetition statement is used to repeat a group (block) of programming instructions.

In Python, we generally have two loops/repetitive statements:

  • for loop
  • while loop

for loop

A for loop is used to iterate over a sequence that is either a list, tuple, dictionary, or a set. We can execute a set of statements once for each item in a list, tuple, or dictionary.

Depiction of for loop statement
Depiction of for loop statement

In the following code example, the first example demonstrates how to use a for loop to iterate over a list and access its elements, while the second example shows how to iterate over a range of numbers.

print("1st example")
lst = [1, 2, 3]
for i in range(len(lst)):
print(lst[i], end = " \n")
print("2nd example")
for j in range(0,5):
print(j, end = " \n")

while loop

In Python, while loops are used to execute a block of statements repeatedly until a given condition is satisfied. Then, the expression is checked again and, if it is still true, the body is executed again. This continues until the expression becomes false.

Depiction of while loop
Depiction of while loop

The following code iterates from 0 to 4 and prints each value using a while loop. It prints End to signify the end of the program after the loop is completed.

m = 5
i = 0
while i < m:
print(i, end = " ")
i = i + 1
print("End")

Frequently asked questions

Haven’t found what you were looking for? Contact Us


What are control flow statements in Python, and why are they important?

Control flow statements regulate the order in which a program’s code executes. They allow developers to manage sequential execution, decision-making, and repetition, enabling dynamic and efficient programming.


What are the three types of control structures in Python?

Python has three types of control structures:

  • Sequential: Executes statements in order.
  • Selection: Implements decision-making using if, if-else, nested if, and if-elif-else.
  • Repetition: Repeats code blocks using for and while loops.

How does a while loop work in Python?

As long as a given condition is true, a while loop repeatedly runs a block of statements. The loop ends when the condition turns false. It can iterate through numbers, for instance, until a particular value is reached.


What are the four types of control statements?

The four types of control statements in programming are as follows:

  1. Decision-making statements: Under specific circumstances, these statements enable the program to run distinct code blocks. The if-else statement is the most widely used decision-making statement.
  2. Iteration statements (loops): These statements allow the program to repeat a block of code multiple times. Common iteration statements include for, while, and do-while loops.
  3. Jump statements: By shifting control to a different area of the program, these statements change the way the program normally executes. Break, continue, and goto statements are a few examples.
  4. Selection statements (switch-case): These statements offer a means of choosing an execution path from a number of options depending on an expression’s value.

What is the flow of code?

The flow of code refers to the order in which the instructions in a program are executed.

  1. Sequential flow: This is the most basic flow, in which instructions are executed one after another, in the order they appear in the code.
  2. Conditional flow: This involves decision-making statements (like if, else if, else) that determine which block of code to execute based on certain conditions.
  3. Iterative flow: This involves loops (like for, while) that repeat a block of code multiple times, either a fixed number of times or until a certain condition is met.
  4. Jump flow: This involves statements like break, continue, goto (though goto is generally discouraged due to the potential for making code harder to read and maintain) that alter the normal sequential flow by jumping to a different part of the code.

Free Resources