What are control flow statements in Python?

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 piece of code multiple times.

1. Sequential

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

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

2. Selection/Decision control statements

In Python, the 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:

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

if

It help us to run a particular code, but 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, we can see first if condition checks a is greater than b. If yes, then we've another if condition that checks a is also greater than c. If yes, then if body will be executed.

a = 20
b = 10
c = 15
if a > b:
if a > c:
print("a value is big")
else:
print("c value is big")
elif b > c:
print("b value is big")
else:
print("c is big")

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

Free Resources