CognitoCoding All posts
19 June 2026 4 min read by Eris Taylor

What Is a Loop in Coding? (The One Concept That Saves You Writing the Same Thing Over and Over)

loopspythonlearn-to-codebeginnerscoding-concepts

This post expands on our One Concept, One Board Short: Loops. Watch it first (60 seconds), then come back for the full story.


The problem loops solve

Imagine you've got 30 students in a class and the teacher wants to say "Good morning!" to each one individually.

If the teacher had to write a note for every single student, that would be 30 separate notes.

Now imagine they had a machine that just ran through the list and said "Good morning!" to each person automatically — stopping when it reached the end.

That machine is a loop.

A loop tells your code: "Do this thing. Keep doing it. Stop when I say."


What a loop actually looks like

Here's the simplest loop in Python:

fruits = ["apple", "banana", "cherry"]

for fruit in fruits:
    print(fruit)

Run this and you get:

apple
banana
cherry

Three lines of output. One loop. No copying and pasting print() three times.

If you add 10 more fruits to the list, the loop still works — you don't change anything else.


The two types of loop you'll use most

The for loop — when you know the list

Use for when you have a specific collection of things to go through: a list of names, a range of numbers, a set of files.

for number in range(1, 6):
    print(number)

Output:

1
2
3
4
5

range(1, 6) means: start at 1, stop before 6. Handy for counting.

The while loop — when you're waiting for something

Use while when you don't know in advance how many times you need to repeat — you just keep going until a condition becomes false.

lives = 3

while lives > 0:
    print(f"You have {lives} lives left")
    lives = lives - 1

print("Game over!")

Output:

You have 3 lives left
You have 2 lives left
You have 1 lives left
Game over!

The loop keeps going as long as lives > 0. The moment lives hits zero, it stops.


Where loops show up in real builds

Once you learn loops, you'll spot them everywhere:

In games — a game loop runs constantly: check for input → update the screen → check again. Every game you've ever played is one giant loop.

In data — if you've got a spreadsheet with 500 rows and you want to process each one, a loop does it in three lines instead of 500.

In the Farmer Was Replaced (our Wednesday series) — in Episode 1, we wrote a loop that moved the drone across every square in a 3×3 grid. Without a loop, we'd have written the same move command 9 times.

In web scraping / bots — loop through a list of websites, grab a piece of info from each one, move on to the next.

In the OCCOB Shorts themselves — the script that generates each Short has a loop that processes the audio, the frames, and the upload in sequence.


The mistake beginners make with loops

Forgetting the indentation.

In Python, everything inside the loop must be indented. If it's not, Python thinks it's outside the loop and only runs it once.

# This is WRONG — the print only runs once, after the loop
for name in ["Alice", "Bob", "Charlie"]:
    pass
print(name)  # Only prints "Charlie"

# This is RIGHT — print runs for every name
for name in ["Alice", "Bob", "Charlie"]:
    print(name)  # Inside the loop

One tab makes a big difference.


What comes next after loops

Loops are even more useful once you combine them with:

  • if statements — do different things for different items in the loop ("if the score is above 50, print 'pass'")
  • Lists — build up a collection of results as the loop runs
  • Functions — wrap a loop inside a function and call it whenever you need it

Those are the next three concepts in the One Concept, One Board series — each one a single Short, one idea at a time.


Try it yourself

Open Python (or repl.it if you don't have it installed) and run this:

my_list = ["coding", "is", "just", "giving", "instructions"]

for word in my_list:
    print(word.upper())

Change the list. Add your own words. See what happens.

That's how you learn loops — not by reading about them, but by breaking them and fixing them.


Watch the One Concept, One Board series

Every Monday and Thursday we drop a new 60-second Short: one coding concept, one whiteboard, zero jargon.

Subscribe to Cognito Coding on YouTube so you don't miss the next one.

Cognito Coding — Learning To Code By Solving Real Problems.