Master Iterative Loops: Simple Example Loop in Action: Iterative Code Iterate With Ease

By Carl

Ever found yourself in need of repeating a task multiple times, like filling out a long form or calculating a series? Welcome to the world of iterative loops, a crucial concept in programming that helps make tasks less tedious and more manageable. In this article, we'll dive into an iterative loop example that's as universally useful as it is simple to understand.

a flow diagram with the words for loop, while loop and start are labeled in different colors
a flow diagram with the words for loop, while loop and start are labeled in different colors

Imagine you're a grocery store owner and you need to send out a newsletter to all your customers with their personalized discounts. Without an iterative loop, you'd be stuck manually writing each customer's name and calculation their discount one by one, a monumental task. But with an iterative loop, you can automate this process, making your life significantly easier.

The Institutional Reality Loop
The Institutional Reality Loop

Loop Basics

Before we delve into our iterative loop example, let's quickly recap the basics. Loops, in general, allow us to execute a block of code multiple times. In iteration, this repetition depends on a condition; once the condition changes, the loop ends.

Explain Different Types of Iterative Looping Structures
Explain Different Types of Iterative Looping Structures

Now, let's talk about the different types of loops. For this discussion, we'll focus on the for loop. A for loop knows how many times it needs to repeat before it even begins, making it perfect for tasks where you know how much work there is upfront.

Initializing the Loop

OODA Loop | TechTello
OODA Loop | TechTello

In our scenario, we know we have 100 customers. So, our for loop will look like this: "for i in range(100)". Here, "i" is our iterator; it represents the current iteration of the loop, starting from 0 (since lists in Python are zero-indexed).

After initializing our loop, we define what happens in each iteration. This block of code is the loop body, and it's enclosed between indentation (like in the example above, the code is indented by four spaces).

The Loop Body

the three stages of learning loops
the three stages of learning loops

In our loop body, we'll write out the customer's name and their discount (we'll use a simplified calculation here). This would look something like this:

print("Dear {},\nYou have a discount of ${:,.2f}!\n".format(customers[i], discounts[i]))

Here, we're using the format() function to insert our customer's name and discount into the string. The ":,.2f" in the discount's placeholder ensures it's formatted as a dollar amount with two decimal places.

Growth Loop
Growth Loop

Nesting Loops

But what if you need to do more than one task for each customer? Like sending a newsletter and updating their purchase history? Here, you'd use nested loops. The outer loop (our customer loop) repeats first, then the inner loop (our task loop) repeats within each iteration of the outer loop.

an info sheet with the words loop engineering on it and other information about how to use it
an info sheet with the words loop engineering on it and other information about how to use it
Ooda Loop Sticker
Ooda Loop Sticker
The fastest way to teach iteration: 6 easy activities
The fastest way to teach iteration: 6 easy activities
a flow diagram showing how to stop while looping with the help of someone else
a flow diagram showing how to stop while looping with the help of someone else
How to reverse a linked list in Java using Recursion and Iteration (Loop) - Example
How to reverse a linked list in Java using Recursion and Iteration (Loop) - Example
OODA loop
OODA loop
Python while Loop: Python Tutorial on while Loop with Examples
Python while Loop: Python Tutorial on while Loop with Examples
Building a Personal AI Infrastructure (PAI) (December 2025 Version)
Building a Personal AI Infrastructure (PAI) (December 2025 Version)
Iterative
Iterative

To illustrate this, let's imagine adding a task to send each customer a birthday greeting in their birthday month. Here, our outer loop would iterate over customers, and our inner loop over months:

Outer Loop

The outer loop looks like: "for i in range(100)" - it grabs each customer in turn.

for i in range(100):
    for j in range(12):   # Our inner loop starts here
        if customer_birth_mon[i] == j+1:
            print("Dear {},\nHappy Birthday for this month!".format(names[i]))

Inner Loop

The inner loop checks if the current month matches the customer's birth month. If it does, it prints a birthday greeting. After checking all 12 months, it goes back to the outer loop and grabs the next customer.

And there you have it - a comprehensive look at an iterative loop example that's applicable in numerous scenarios. So next time you need to repeat a task, remember programming has you covered!