DEV Community

Cover image for Python Control Statements: A Beginner-Friendly Guide with Examples
Rachit Joshi
Rachit Joshi

Posted on

Python Control Statements: A Beginner-Friendly Guide with Examples

Introduction

Every program follows a particular sequence of execution.

By default, Python executes statements one after another in the order in which they are written. However, real-world applications are rarely that simple.

There are situations where a program must make decisions, repeat a set of instructions, or even skip certain operations depending on specific conditions.

In this blog, we will take a detailed look at Python control statements, explore their types, and examine practical examples illustrating how they are used in everyday programming.

What Are Control Statements in Python?

Control statements are fundamental components of Python that determine how a program executes its instructions.

In a normal situation, Python executes statements sequentially, moving from the first line of code to the last.

However, many real-world applications require a program to make decisions, repeat specific actions, or alter the normal flow of execution according to particular conditions.

Control statements provide developers with the flexibility to manage these situations efficiently.

They allow a program to choose different execution paths, perform repetitive tasks, skip certain operations, or terminate a process when necessary.

Python control statements are generally classified into three main categories:
**

  1. Conditional Statements**

Conditional statements help a program make decisions based on specific conditions.

Python provides the following conditional statements:

if
if...else
if...elif...else
The if Statement

The if statement executes a block of code only when a condition evaluates to True.

age = 20

if age >= 18:
print("You are eligible to vote.")
Output
You are eligible to vote.

In this example, Python checks whether the value of age is greater than or equal to 18.

The if...else Statement

Sometimes we need an alternative action when a condition is false.

marks = 45

if marks >= 50:
print("Pass")
else:
print("Fail")
Output
Fail
The if...elif...else Statement

This statement is useful when multiple conditions need to be checked.

temperature = 30

if temperature < 20:
print("Cold")
elif temperature < 30:
print("Pleasant")
else:
print("Hot")
Output
Hot

2. Loop Statements

Loops are used when we need to execute the same block of code repeatedly.

Python mainly provides two types of loops:

for loop
while loop
The for Loop

A for loop is commonly used to iterate through a sequence.

fruits = ["Apple", "Banana", "Orange"]

for fruit in fruits:
print(fruit)
Output
Apple
Banana
Orange

The loop automatically visits every item in the list.

The while Loop

A while loop continues running until the specified condition becomes false.

count = 1

while count <= 5:
print(count)
count += 1
Output
1
2
3
4
5

This type of loop is especially useful when the number of iterations is unknown beforehand.

3. Jump Statements

Jump statements allow us to alter the normal execution of loops.

Python provides three jump statements:

break
continue
pass
The break Statement

The break statement immediately stops the loop.

for number in range(1, 10):
if number == 6:
break

print(number)
Enter fullscreen mode Exit fullscreen mode

Output
1
2
3
4
5

As soon as the loop encounters the value 6, it stops executing.

The continue Statement

The continue statement skips the current iteration and moves to the next one.

for number in range(1, 6):
if number == 3:
continue

print(number)
Enter fullscreen mode Exit fullscreen mode

Output
1
2
4
5

Here, the number 3 is skipped.

The pass Statement

The pass statement acts as a placeholder.

for number in range(5):
pass

Although the loop doesn't perform any action, Python doesn't generate an error.

Conclusion

Control statements form the backbone of Python programming.

They help us make decisions, repeat operations, and control how our programs behave under different conditions.

Once you become comfortable with if, for, while, break, continue, and pass, you'll be able to build much more powerful applications.

Top comments (0)