Ctrl-C and other ways to stop the runaway
Okay so here's the thing. Last lesson I told you about the night my while loop got stuck and my old laptop fan sounded like it was trying to take off. I mentioned Ctrl-C in passing. Today we're actually going to use it, on purpose, so it's not a panic move the first time your loop runs away from you. Because it will. Everybody's does eventually.
What Ctrl-C actually does
Ctrl-C tells whatever's running in your terminal to stop, right now, mid-thing. Doesn't matter if it's a loop, a slow calculation, or a program that's just hanging for no reason you can see. You hit Ctrl-C, it stops.
On a Mac it's sometimes Cmd-period instead, depending on what you're running it in. If Ctrl-C doesn't work, try that. Different setups, same idea.
This is not an elegant way to end a program. It's the "pull the fire alarm" option. But at the end of the day, sometimes you need the fire alarm, and you should not feel bad about using it.
Let's make one run away on purpose
Type this in. Type it yourself, don't copy-paste it, your fingers need to feel this one:
``python count = 0 while count < 10: print("counting...") ``
Run it.
It's not going to stop. You just wrote an infinite loop. See the bug? count never changes. The condition count < 10 is true forever because count is stuck at 0 forever. This is the single most common mistake with while loops and I have made it more times than I'll admit.
Your terminal is going to fill up with "counting..." forever, scrolling faster than you can read it. This is your moment. Hit Ctrl-C.
Nice. You just stopped a runaway program. That's a real skill, not a party trick. You'll use it constantly.
Now let's fix the actual bug
The fix is one line:
``python count = 0 while count < 10: print("counting...") count = count + 1 ``
Now count goes up every time through the loop, and eventually count < 10 becomes false, and the loop ends on its own like a civilized program. No Ctrl-C needed. That's the goal, always, the loop should know when to stop by itself. Ctrl-C is the seatbelt, not the plan.
Building the habit: stupid-small steps
This is where I'll say the thing I always say: break your problem into stupid-small steps. Don't try to write the whole loop in your head first and then type it all at once. Write the loop skeleton, run it, watch it misbehave, fix one thing, run it again.
I actually taught myself loops this way, sort of by accident. I was obsessed with a board game years back and I wanted to see every possible starting move written out. So I wrote a loop to spit them all out, one at a time, and I filled about half a moleskin notebook with the printed output just watching it go. Nobody asked me to do this. There was no practical reason. But writing that loop, watching it run too long, watching it run wrong, fixing it, running it again, that's actually how the concept clicked for me. Not a tutorial. Just messing around with something I cared about and being willing to let it break a bunch of times.
So if the counting example feels too dry, try something dumb you actually care about. Loop through your favorite five songs and print them out. Loop through numbers 1 to 100 and print only the ones divisible by 7. Doesn't matter what it is. The stupid-small version where you can see every step is the whole point.
When to actually reach for Ctrl-C
Real talk on when this comes up:
- Your terminal is scrolling text faster than you can read and it's clearly not stopping on its own.
- Your computer fan kicks on loud, like it's working way harder than a text program should need to. That's usually an infinite loop chewing through your CPU.
- You just plain don't know what your program is doing anymore and want to check your code before running it again.
None of these are emergencies. Nothing's going to break. Ctrl-C just stops it clean. Worst case you lose whatever the program hadn't saved yet, which for the stuff we're writing in this class is nothing, so don't sweat it.
One thing I will say plainly: don't force-quit your whole terminal window or restart your computer to escape a loop. Ctrl-C is built for exactly this and it's gentler. Save the nuclear option for when Ctrl-C genuinely doesn't work, which is rare.
Before next time
Write an infinite loop on purpose, let it run for a few seconds so you can watch it happen, then Ctrl-C your way out of it. Then fix it so it stops on its own. Do this twice if you can, once with counting numbers, once with something you picked yourself. I want the panic to wear off before we do anything harder with loops.