♾️ Infinite Loop

5th Grade 💻 Coding

An infinite loop is a loop that never ends, because its condition always stays true. Sometimes it’s a bug — and sometimes it’s on purpose.

Free · US Letter · no sign-up
Google Classroom Save to Pinterest Email me

When it’s a bug, the program “freezes”: it stops responding, or shows the same thing over and over without end. Usually the reason is that we forgot to change something inside the loop — for example, we never made the counter smaller. In Python you stop a program like that with Ctrl+C, and in Scratch with the red stop button.

But sometimes you want an infinite loop: a game keeps checking whether a key was pressed, and a digital clock keeps ticking without a break. Scratch has a “forever” block for this, and in Python you write while True:. The difference is that here it’s planned, and there’s a way out — closing the game or a quit command.

For example

  • Lucas wrote a loop that adds 1 to x as long as x is greater than 0, and started x at 1 — x only grew, and the loop never ended.
  • A Scratch “forever” loop that checks “if the space key is pressed, jump” is infinite on purpose.

📌 Remember: Infinite by mistake = a bug. Infinite on purpose = the heart of a game.

Quick quiz

  1. What is an infinite loop?

  2. How do you stop a stuck Python program?

  3. Where is an infinite loop useful?

🔎 Ask Thinkapedia another question