Learn how to use conditional statements and loops in Python to control the flow of your code

Nicolai Søderberg
4 min readDec 21, 2022

--

Photo by Clément Hélardot on Unsplash

Conditional statements and loops are essential tools for controlling the flow of your code in Python. In this article, you will learn how to use if, else, and elif statements to execute code based on certain conditions, how to nest if statements to create more complex conditional logic, how to use for loops to iterate over a sequence of values, how to use while loops to repeat a block of code while a condition is true, and how to use the break and continue statements to control the flow of a loop. These concepts are fundamental to programming in Python and will enable you to write more sophisticated and efficient code.

The most basic conditional statement is the if statement, which allows you to specify a condition and a block of code to be executed if that condition is true. Here is an example of an if statement in Python:

x = 10
if x > 5:
print("x is greater than 5")

In this example, the condition is x > 5, and the block of code to be executed is print("x is greater than 5"). If the condition is true (i.e., if x is greater than 5), then the code block will be executed. If the condition is false (i.e., if x is less than or equal to 5), then the code block will not be executed.

You can also use the else statement to specify a block of code to be executed if the condition in the if statement is false. Here is an example:

x = 3
if x > 5:
print("x is greater than 5")
else:
print("x is not greater than 5")

In this example, the code block print("x is not greater than 5") will be executed because the condition x > 5 is false.

In addition to the if and else statements, you can also use the elif (short for "else if") statement to specify additional conditions to be checked. Here is an example:

x = 3
if x > 5:
print("x is greater than 5")
elif x == 5:
print("x is equal to 5")
else:
print("x is less than 5")

In this example, the code block print("x is less than 5") will be executed because the condition x > 5 is false and the condition x == 5 is also false.

You can nest if statements to create more complex conditional logic. For example:

x = 3
y = 4
if x > 5:
if y > 5:
print("x and y are both greater than 5")
else:
print("x is greater than 5 and y is not")
else:
if y > 5:
print("y is greater than 5 and x is not")
else:
print("x and y are both not greater than 5")

In this example, the code block print(“y is greater than 5 and x is not”) will be executed because the condition x > 5 is false and the condition y > 5 is true.

Loops in Python allow you to repeat a block of code multiple times. With the two most used being forloop and whileloop. There is also nested loop which are when a loop is inside another loop.

A for loop allows you to iterate over a sequence of values, such as a list or a string. Here is an example of a for loop in Python:

for i in range(5):
print(i)

This for loop will print the numbers 0 through 4, because the range function returns a sequence of values from 0 to one less than the specified number (in this case, 5).

A while loop allows you to repeat a block of code while a certain condition is true. Here is an example of a while loop in Python:

x = 0
while x < 5:
print(x)
x += 1

This while loop will print the numbers 0 through 4, because the condition x < 5 is true for each iteration of the loop. The x += 1 statement increments x by 1 at the end of each iteration, so that the condition will eventually become false and the loop will terminate.

You can use the break statement to exit a loop prematurely, and the continue statement to skip the rest of the current iteration and move on to the next one. Here is an example that uses both statements:

for i in range(10):
if i % 2 == 0:
continue
if i > 6:
break
print(i)

In this example, the continue statement will skip the iteration if i is even, and the break statement will exit the loop if i is greater than 6. The loop will therefore only print the odd numbers 1, 3, 5.

In this article, we learned how to use conditional statements and loops in Python to control the flow of our code. We discussed how to use if, else, and elif statements to execute code based on certain conditions, and how to nest if statements to create more complex conditional logic. We also learned how to use for loops to iterate over a sequence of values and while loops to repeat a block of code while a condition is true. Finally, we covered the use of the break and continue statements to control the flow of a loop. By understanding and applying these concepts, you can write more sophisticated and efficient code in Python.

--

--

Nicolai Søderberg

I am a Data Consultant/System Specialist with a degree in Quantum Physics from the University of Copenhagen