Sitemap

Python Control Flow : If/Elif/Else Statements and Loops Explained

3 min readApr 15, 2023

--

Press enter or click to view image in full size

Python control flow refers to how the execution of code is controlled based on certain conditions. Control flow allows you to create logic in your code, so that certain parts of the code are executed only if specific conditions are met. There are several control flow statements available in Python, including: on specific conditions or repeatedly execute a block of code until a condition is satisfied. In this article, we’ll explore two essential control flow statements in Python — if/else statements and loops.

  1. If/elif/Else Statements

a. If Statements

If statements are used to execute code if a certain condition is true. Here’s an example :

age = 34
if age >= 18:
print("You are an adult")

In this example, the code inside the if block will only be executed if the age variable is greater than or equal to 18.

b. Else Statements

Else statements are used to execute code if the condition in the if statement is false. Here’s an example :

age = 13
if age >= 18:
print("You are an adult")
else:
print("You are not an adult")

In this example, the code inside the else block will be executed if the age variable is less than 18.

c. Elif Statements

elif statements are used to check multiple conditions. Here’s an example :

age = 34
if age < 18:
print("You are a minor")
elif age < 65:
print("You are an adult")
else:
print("You are a senior citizen")

In this example, the code inside the first elif block will be executed if the age variable is less than 18, and the code inside the second elif block will be executed if the age variable is greater than or equal to 18 but less than 65. If neither of these conditions are met, the code inside the else block will be executed.

2. Loops

Get Heronima Petra’s stories in your inbox

Join Medium for free to get updates from this writer.

Loops are control flow statements that allow you to execute a block of code repeatedly based on a specific condition. In Python, there are two types of loops: for loops and while loops.

Loops

A for loop is used to iterate over a sequence of values, such as a list or a string. The general syntax of a for loop in Python is as follows :

fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)

In this example, the code inside the for block will be executed for each item in the fruits list.

Here’s an example of a for loop in action :

my_list = [1, 2, 3]

for item in my_list:
print(item)

In this example, the for loop iterates over the values in my_list and prints each value to the console.

While Loops

A while loop is used to execute a block of code repeatedly until a specific condition is met. The general syntax of a while loop in Python is as follows :

while condition:
# code to execute while the condition is True

Here’s an example of a while loop in action :

i = 0

while i < 5:
print(i)
i += 1

In this example, the loop will continue to execute as long as the value of i is less than 5. The code inside the loop will print the current value of i and then increment i by 1. The output of this code will be :

0
1
2
3
4

Note that it’s important to be careful when using while loops, as an incorrect condition or an infinite loop can cause your program to hang or crash. To avoid infinite loops, make sure that the condition will eventually be false, or use a break statement to exit the loop. Here’s an example of using a break statement to exit a loop :

while True:
response = input("Enter a value (or 'q' to quit): ")
if response == 'q':
break
else:
print("You entered:", response)

In this example, the loop will continue to ask the user for input until they enter the value ‘q’. When the ‘q’ is entered, the break statement will be executed, causing the loop to exit.

--

--

Heronima Petra
Heronima Petra

Written by Heronima Petra

◼️ Data Science, and Python enthusiast who loves to share ideas