๐Ÿšฆ Mastering Control Statements in Python

PYTHON

4/25/20253 min read

Control the Flow. Command the Logic.

Python is known for its simplicity, but beneath the clean syntax lies powerful tools that help you control the flow of your program. These tools are called control statements.

In this blog, weโ€™ll explore three essential control statements in Python:

  • break

  • continue

  • pass

Youโ€™ll learn what they do, when to use them, and see real-world examples in action. Let's dive in!

What Are Control Statements?

Control statements let you change the default flow of execution in loops and conditional blocks. They help in skipping iterations, exiting loops early, or inserting placeholders.

The break Statement
Purpose:

Used to exit a loop prematurely โ€” when a certain condition is met.

Syntax:

for item in sequence:

if condition:

break

Example:

# Stop the loop when 7 is found

numbers = [1, 3, 5, 7, 9]

for num in numbers:

if num == 7:

print("Found 7, stopping loop.")

break

print(f"Checked: {num}")

Output:

Checked: 1

Checked: 3

Checked: 5

Found 7, stopping loop.

When to Use:
  • Searching for an element and exiting early

  • Canceling infinite loops on a condition

  • Breaking out of nested loops (with care)

The continue Statement
Purpose:

Used to skip the current iteration of a loop and move to the next one.

Syntax:

for item in sequence:

if condition:

continue

Example:

# Skip printing even numbers

for i in range(1, 6):

if i % 2 == 0:

continue

print(i)

Output:

1

3

5

When to Use:
  • Ignoring specific conditions

  • Filtering input without exiting loop

  • Cleaning or validating data streams

The pass Statement
Purpose:

pass is a placeholder โ€” it does nothing. It's used when a statement is syntactically required but you don't want to run any code (yet).

Syntax:

if condition:

pass # do nothing for now

Example:

for i in range(3):

if i == 1:

pass # you can add logic here later

print(f"Processing {i}")

Output:

Processing 0

Processing 1

Processing 2

When to Use:
  • Creating code stubs

  • Temporarily disabling logic

  • Structuring empty class/method definitions

Password Validator:

while True:

password = input("Enter a password: ")

if len(password) < 6:

print("Too short, try again.")

continue

if " " in password:

print("No spaces allowed.")

continue

print("Password accepted!")

break

Whatโ€™s Happening:
  • continue restarts the loop if the password is invalid

  • break exits once the input is valid

Search for a Number in a List:

numbers = [2, 4, 6, 8, 10, 12]

search = int(input("Enter a number to search: "))

for num in numbers:

if num == search:

print("Number found!")

break

else:

print("Number not found.")

Output:

Enter a number to search: 10

Number found!

ATM PIN Attempts (with Max 3 Tries)

correct_pin = "4321"

for attempt in range(3):

entered_pin = input("Enter your ATM PIN: ")

if entered_pin == correct_pin:

print("Access granted.")

break

else:

print("Incorrect PIN.")

else:

print("Card blocked after 3 failed attempts.")

Finding First Negative Number in a List

numbers = [5, 3, -8, 4, -2, 7]

for num in numbers:

if num < 0:

print(f"First negative number found: {num}")

break

OUTPUT:

First negative number found: -8

Skip Empty Input

for _ in range(5):

name = input("Enter your name: ")

if name.strip() == " ":

print("Empty input! Skipping...")

continue

print(f"Hello, {name}!")

Print Only Multiples of 3

for i in range(1, 21):

if i % 3 != 0:

continue

print(i, end=" ")

Skip Words Starting with โ€˜aโ€™

words = ["apple", "banana", "avocado", "grape", "apricot", "cherry"]

for word in words:

if word.startswith("a"):

continue

print(word)

OUTPUT:

banana

grape

cherry

Unimplemented Function

def process_data():

# Logic coming soon

pass

Skipping Items That Need No Action

tasks = ["send email", "call client", "meeting", ""]

for task in tasks:

if task == "":

pass # Placeholder for handling empty task

else:

print(f"Doing task: {task}")

Combining break and continue

while True:

age = input("Enter your age (or 'q' to quit): ")

if age == 'q':

print("Goodbye!")

break

if not age.isdigit():

print("Invalid input. Try again.")

continue

age = int(age)

if age >= 18:

print("Youโ€™re eligible to vote.")

else:

print("Youโ€™re not eligible yet.")

Key Takeaways: Mastering Control Statements in Python
break
  • Used to exit a loop early when a condition is met.

  • Common in search operations, input validation, or emergency stops.

  • Often paired with else in for loops to detect if a break didnโ€™t occur.

continue
  • Used to skip the rest of the current loop iteration and move to the next one.

  • Helps in filtering data or ignoring unwanted conditions without breaking the loop.

  • Keeps your code cleaner by avoiding nested if statements.

pass
  • A do-nothing placeholder used when a statement is syntactically required but no action is needed.

  • Useful during development, in empty function/class definitions, or TODO blocks.

Kishore Babu Valluri

Senior Data Scientist | Freelance Consultant | AI/ML & GenAI Expert

With deep expertise in machine learning, artificial intelligence, and Generative AI, I work as a Senior Data Scientist, freelance consultant, and AI agent developer. I help businesses unlock value through intelligent automation, predictive modeling, and cutting-edge AI solutions.