Skip to content
Utah Community Learning

while loops and how I got one stuck running forever

About 20 minutes

while loops and how I got one stuck running forever

Okay so here's the thing. for loops are great when you know exactly how many times you want to do something. Ten photos, five grocery items, four hundred files. You count it out ahead of time and the loop does that many laps.

But sometimes you don't know the number ahead of time. Sometimes you just want the computer to keep doing something until a condition changes. That's a while loop. It's less "do this ten times" and more "keep doing this as long as this thing is true."

The basic shape

```python count = 0

while count < 5: print("Still going") count = count + 1

print("Done") ```

Type that yourself. Don't paste it in. Your fingers need to feel the shape of it, that's not me being precious about it, I just genuinely think you learn different stuff typing than you do reading.

Walk through it with me before you run it:

  • count starts at 0.
  • Python checks: is count less than 5? Yes. So it runs the print, then adds 1 to count.
  • Back to the top. Checks again. Still less than 5? Yes. Does it again.
  • This keeps happening until count hits 5, and then the check fails, and Python skips the loop and moves on to "Done."

Five laps, then it stops. That "count = count + 1" line is doing the whole job. It's the thing that eventually makes the condition false.

The way this goes wrong

Now delete that middle line. The count = count + 1 one. Run it again.

Don't actually run it unless you're ready to hit Ctrl-C in about two seconds, I'll explain why in a second.

If count never changes, the condition count < 5 is true forever. Python has no idea it's supposed to stop. It just keeps looping, print, print, print, print, faster than your screen can even show you, forever, until you make it stop.

This is called an infinite loop and it is a super common mistake. Everybody who's ever learned while loops has done this at least once. I want to be really clear that this isn't a sign you're bad at this. It's basically a rite of passage.

Here's my actual story. Early on I was writing something to loop through, and I got the counter logic wrong, forgot to update it at all, actually. Ran the program and just sat there while my old laptop's fan spun up like it was trying to take off. That fan on that machine sounded like a leaf blower when it got going. I remember just watching the screen, nothing printing yet somehow, computer working its butt off, and thinking "why does the computer hate me today."

Ctrl-C is your friend. That's the keyboard shortcut that tells Python "stop whatever you're doing right now." On a lot of setups it's Ctrl-C, on a Mac terminal it's the same thing usually. Learn it now. You will use it. I still use it regularly, it's not a beginner-only tool, it's just a tool.

Why this matters more than it looks like it does

A for loop physically cannot run forever, it's got a set number of laps built in. A while loop has no such guardrail. You are the guardrail. You have to build in the thing that changes, every single time, or the loop never ends.

This is also where I'll bring up something that happened to me later on, once I'd been at this a while and thought I knew what I was doing. I wrote this really slick one-line version of a loop, real clever, real compact, and I was proud of it. Showed it to my son Jared. He nodded, whatever, moved on. I came back to that same code about a week later needing to fix something small in it, and I could not for the life of me figure out what past-me had written or why. Couldn't read my own code. Had to basically rebuild my understanding of it from scratch.

That's the whole lesson right there, honestly. Clever is not the goal. Clear is the goal. Write your while loops boring and obvious, with a plain counter you update in a plain way, not some slick trick you'll be proud of for a week and then hate yourself for later.

Try this at home

Write a while loop that counts down from 10 to 1 and then prints "Liftoff." Steps:

  1. Set a variable, something like count = 10.
  2. Loop while count > 0.
  3. Print count.
  4. Subtract 1 from count each time through.
  5. After the loop ends, print "Liftoff."

Then, on purpose, comment out the line that subtracts 1, and watch it start looping without end. Then hit Ctrl-C. I want you to feel that moment on purpose, once, in a safe low-stakes way, so it's not scary the first time it happens by accident. Because it will happen by accident. That's kind of the point of this whole lesson.

Before next time

Get comfortable with Ctrl-C before we meet again, seriously, practice killing a stuck loop at least once so your hand knows where to go. It'll save you real frustration later.