No history yet

Introduction to Loops

Doing More with Less Code

Imagine you need to send a personalized "Happy Birthday!" email to one hundred friends. You could write the same email, change the name, and click send one hundred times. That's a lot of repetitive work. In programming, we face similar situations where we need to perform the same action multiple times. Instead of writing the same line of code over and over, we use a loop.

loop

noun

A control flow structure that allows a block of code to be executed repeatedly as long as a certain condition is met.

A loop is a fundamental concept that tells a computer to repeat a set of instructions until a specific condition is no longer true. It's a way to automate repetitive tasks, making your code more efficient, shorter, and easier to read. Whether it's processing every item in a shopping cart, calculating the grade for each student in a class, or drawing a series of shapes on a screen, loops are the tool for the job.

Loops help you repeat actions without writing the same code over and over.

The Anatomy of a Loop

While different programming languages have slightly different ways of writing loops, they all share the same basic structure. Think of it like a recipe with three key parts that work together to control how many times the loop runs.

PartQuestion It AnswersExample
InitializationWhere do I start?Start a counter at 0.
ConditionHow long do I continue?Keep going as long as the counter is less than 10.
IterationWhat happens after each repetition?Add 1 to the counter.

First, you initialize a variable, which sets the starting point. Next, you set a condition, which is a test that's checked before each repetition. If the condition is true, the code inside the loop runs. If it's false, the loop stops. Finally, you have the iteration (or update), which modifies the variable after each cycle, bringing it closer to the point where the condition becomes false.

Without all three parts working correctly, you might create an infinite loop, where the condition never becomes false and the program gets stuck running the same code forever. That's why understanding this structure is so important.

Loops in Action

Let's see how this structure looks in practice. Below are some generic examples that show the logic of a loop that prints numbers from 1 to 5. Notice how each one has a clear starting point, a condition for stopping, and a step to take after each cycle, even if the keywords are different.

/* JavaScript-style example */
// Initialization: start a counter at 1
// Condition: loop as long as counter is 5 or less
// Iteration: add 1 to counter after each loop
for (let counter = 1; counter <= 5; counter++) {
  print(counter);
}

This structure is a common pattern you'll see across many languages.

# Python-style example
# Initialization and Condition are handled by 'range(1, 6)'
# It creates a sequence from 1 up to (but not including) 6.
# Iteration happens automatically, moving to the next number.
for counter in range(1, 6):
  print(counter)

In both cases, the result is the same: the numbers 1, 2, 3, 4, and 5 are printed. By using a loop, we achieve this with just a few lines of code instead of five separate print commands. This efficiency is why loops are one of the most powerful tools in a programmer's toolkit.

Ready to check your understanding? Let's see what you've learned about loops.

Quiz Questions 1/5

What is the primary purpose of a loop in programming?

Quiz Questions 2/5

What is the term for the part of a loop that sets the starting point for the counter variable?

Loops are a core building block in programming. They automate repetition, keep your code clean, and empower you to solve complex problems efficiently.