Do you remember Donkey annoyingly asking “are we there yet?” from the back of the couch in Shrek 2? I remember it pretty well—mostly because when I saw it for the first time as a kid, I liked it so much that I rewatched the whole movie three times in a row. Back then, having it on a cassette, I didn’t even consider just rewinding to that part. Instead, I watched it all. Three times. Those were the times.
But don’t worry, this is not a post about my childhood. It’s still about computers—and I’ll tell you what Donkey from Shrek has to do with it a bit later. Today, I want to describe one core concept in computer science. It’s quite simple, extremely powerful, and pretty ubiquitous. However, the way it’s used isn’t always obvious. Still, it’s so essential to the programming world that it’s hard to imagine even the smallest program without one. It’s called a loop.
First, I wanted to write this post about video games, but then I realized that so much of it would be about loops that I should probably describe them first.
Let’s scratch the surface. Imagine you’re a barista in a coffee shop. Your job is to prepare coffee. If your manager were to describe it to you in the dumbest way possible, it’d be something like:
“when the first client arrives, you take their order, prepare it, and give it back to them. When the second client arrives, you take their order, prepare it, and give it back to them.”
Sounds like fun, doesn’t it?“
As humans, we’d probably be surprised to hear a job described this way. But for computers, it’s the only way to work. They’re extremely fast, highly customizable, and super helpful—but in order to function this way, they need instructions that are precise. So precise that putting the wrong symbol in the wrong place can crash a program that consists of thousands of lines of code.
I’ll tell you about this at some point too. It’s pretty hilarious that developers sometimes spend more time looking for small typos than actually solving big problems.
So you shouldn’t be surprised to find a program that contains instructions written in this overly literal way. However, this would normally be part of a joke or a demonstration of how not to write code. That’s because early programmers realized pretty quickly that repetition is something we’d be dealing with a lot—and they found a way to describe it precisely, without repeating everything. These structures are called loops, and there are several types.
The basic one is called a for
loop. Its code can look a bit cryptic if you’ve never touched a programming language, but the interpretation is straightforward:
Start from here
Do this
Move forward (or backward)
Go back to step 2 (until you reach the end)
An improvement on this technique is called for each
, and it’s even easier to explain: for each something, do something.
Going back to the barista example, the working routine could be described like this:
for each client, make them coffee.
These are loops from the for
group. But there’s another kind: the while
loop. It also executes instructions repeatedly, but the key difference is that instead of relying on something you can count (like each client), it uses a condition to decide when to stop.
It’s like saying:
“while it’s not 5 PM, keep serving clients.”
Now, if you think about it, we’re on shaky ground here. What if you miss 5 PM?
“No way,” you probably thought. And you’re right—your life is your life. But again, computers are much more precise, and it’s so much easier for them to fail. If, say, the computer lagged and missed the exact moment of 5:00 PM—and now it’s 5:00:02—it might think it’s still not 5 PM, and just keep going.
This is what’s known as an infinite loop.
Even if you’ve never programmed before, you’ve probably heard of this. The idea is self-explanatory: it’s an operation that will never stop (unless we make it stop). Sometimes, that’s not too hard—you can just terminate the program. But the reason this happens, again, is because computers are extremely precise. If you were told to keep serving clients until your boss comes and tells you to go home, and then they never show up, you—being a sensible person—would just head home anyway. A computer won’t. It’ll keep doing (or trying to do) whatever it was told to do, until it’s either explicitly stopped or hits a condition where it physically can’t continue—like running out of memory.
Now, let’s try to go deeper and look at something a bit more technical.
The simplest example of a loop in a computer program is doing something with a collection of data. Say you want to calculate the number of letters in multiple sentences. The program responsible for this task could be interpreted like this:
“for each sentence, calculate its length.”
But that doesn’t sound too exciting. Where things get interesting is in places you wouldn’t expect loops to exist.
For example: clicking buttons on your computer. It might seem like your computer is in some kind of frozen state when you’re not interacting with it—and only “wakes up” when you start clicking, typing, or moving your mouse. But that’s not what’s happening. The computer is always listening.
Keep listening until... what do you think?
Until it’s told to stop :) That simple.
The computer listens to everything you do so it can react. Has anything happened? Has this button been pressed? Has this key been typed?
You can think of it like a barista watching the door, ready to greet the next customer the moment they walk in. Even though this scanning might seem passive, if you strip away the autopilot part, it’s more or less:
“Is someone there? … Is someone there?”
And once they answer “yes,” they approach you.
Now, this might sound inefficient. If the computer is constantly scanning everything I do, how does it have the capacity to do anything else?
The answer is: power. Computers—even the cheapest ones—are insanely powerful. Think: hundreds of millions of operations per second kind of powerful. I won’t go much deeper into this today—just keep in mind, speed isn’t the problem here.
So we’ve looked at a basic infinite loop: constantly asking “has something happened yet?” If the answer is no, it asks again, hoping something’s changed. If yes, it moves on.
Now let’s look at how the same concept is applied in other contexts.
For example, when you open a website, there’s a server (a special kind of computer that provides the website’s content). And the way this server works is—it runs an infinite loop. At every iteration (which happens very quickly), it asks:
“Has anybody requested the site?”
If the answer is no, it “takes a breath” and asks again.
And again.
And again.
Thousands of times per second.
Video games use loops too—a lot of them. One loop constantly checks what the player is doing. Another updates the screen so the player can actually see the results of their actions. I’ll go into more detail about how video games work in a separate piece, so stay tuned.
And do you know what all this constant looping reminds me of? Donkey from Shrek.
Except computers aren’t annoying — they’re fast, quiet, and tireless. But in a nutshell, this is how they operate: constantly asking questions. Has anything happened yet? How about now? Still nothing? Over and over, until something changes.
Loops are such an essential part of programming that they’re one of the first things computer scientists and software engineers learn. But while their most basic use is working with collections—collections of anything—the reality is that even developers don’t always realize how many counterintuitive places they show up: websites, operating systems, video games, and much, much more.
And that brings me to a familiar conclusion I seem to keep reaching:
Computers are, at their core, very powerful. That’s what allows us to do all sorts of things with them. But that power would go unnoticed if it were used too obviously. Instead, what we experience is a kind of magic. And that magic is an illusion—an illusion made possible by simple concepts, used in surprising ways.
This is just the beginning of our journey. I want to show you more of these illusions—and how they make computers what they are.