Sitemap
Kavisha Nethmini

Kavisha Nethmini

Python Control Flow

If Statements and Loops

1 min readJun 18, 2026

--

As I continue learning Python, I recently explored some of the most important concepts that control how a program behaves using if statements, for loops, and while loops.

Making Decisions with If Statements

If statement helps to decide what action should do based on the condition. There are three types of if condition, normal if condition using one condition, more than one conditions using elif and condition inside another condition using nested if statement.

Repeating Tasks with For Loops

Loop is used when we want to perform a task repeatedly. Most commonly used loop is for loop.

Using range()

Get Kavisha Nethmini’s stories in your inbox

Join Medium for free to get updates from this writer.

This is helpful when we know how many times the task should repeat.

for i in range(1, 11):
print(i)


Output - 1
2
.
.
10

Some interesting thing is conditions can be used within the loop and loop can also exist inside another loop.

Controlling Loops with Break

The break statement immediately stop the loop when a condition is met.

customers = ["Kamal", "Nimal", "Saman", "Amara"]

for customer in customers:
if customer == "Saman":
print("Customer Found")
break

print("Search")
Search
Search
Customer Found

Skipping Condition with Continue

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

transactions = ["Success", "Failed", "Success", "Success"]

for transaction in transactions:
if transaction == "Failed":
continue

print("Transaction Processed")
Transaction Processed
Transaction Processed
Transaction Processed

Working with While Loop

A while loop keeps running until a condition remains true. Unlike a for loop, a while loop is useful when the number of iterations is not known in advance.

--

--