Skip to content
BENIBIX Learn

Infinite Loop

Development🧩 Programming ConceptsControl Flow

Also known as Endless Loop

A loop that never ends. Usually a bug, and the program seems to freeze.

Think of it like

Think of it like a dog chasing its own tail. It goes round and round and never gets anywhere.

Example

Jack writes while n > 0 but forgets n = n - 1. The program prints 3 over and over until he presses Ctrl+C.

Why it matters

A stuck program wastes power and makes people think it crashed. Knowing the cause makes it quick to fix.

Definition

An infinite loop is a loop that never stops. Usually it is a bug: the test that should end it never turns false. The program seems to freeze while the computer works as hard as it can. Sometimes a loop is meant to run forever, like a game waiting for key presses. In that case something else, like a break, is the way out.

Example code

python
n = 3
while n > 0:
    print(n)   # n never changes
# 3, 3, 3, 3 ... until Ctrl+C
Nothing changes n, so the test is true forever.

Common mistake

Thinking the computer is just slow. If nothing happens for long, check for a loop whose test never changes.

Key takeaways

  1. 1
    No way out. The end test never turns false.
  2. 2
    Looks frozen. The program works hard but shows nothing.
  3. 3
    Sometimes on purpose. A game loop runs until you quit.

Learn it properly