TryBugHunt

Infinite loops: why your program hangs and never finishes

The tab freezes, nothing prints, and the loop condition never becomes false.

12 free challenges6 Python6 JavaScript

What is an infinite loop?

An infinite loop is a loop whose exit condition never becomes true. The program does not crash and does not print an error — it simply stops making progress, which makes it one of the more disorienting bugs to hit for the first time.

Why it happens

Every loop needs its body to move something toward the exit condition. The failure is almost always that the variable in the condition is not the variable the body changes: the increment sits inside an if, a continue jumps over it, a string method's return value is discarded so the text never actually changes, or a condition is tested against a value nothing updates.

How to recognise it

  • It hangs on some inputs and finishes on others — the working ones avoid the branch that fails to make progress.
  • A while loop whose body contains continue.
  • A condition on one variable while the body updates a different one.
  • In Python, calling text.replace(...) or list.sort() without assigning the result.

Error guides caused by this pattern

All error guides →

In-depth guides for common exceptions and runtime errors triggered by infinite loops, with working fixes and real-world examples:

How to fix it

Print the condition variable at the top of every iteration. If it does not change, you have found it. Ask what happens when the branch is false: if the answer is 'nothing', the loop can stall. Put the increment where it always runs — a for loop's header is safer than a while loop's body.

Breaks vs Works Example

Breaks

// JavaScript: loop condition variable never advances
while (i < items.length) {
  if (items[i] === target) break;
  // forgetting i++ when target not matched hangs the tab
}

Works

// JavaScript: increment is placed where it runs on every iteration
while (i < items.length) {
  if (items[i] === target) break;
  i++;
}

💡 Always verify that every code path inside the loop moves state toward the exit condition.

Practise infinite loops

Working code with one bug in it. Find it, fix it in the browser, and see the explanation. No account needed.

javascriptEasy
Doubling until past a limit never stops

stepsToExceed(limit) counts how many times you double 1 before it is bigger than limit. Example: stepsToExceed(8) → 4 (1 → 2 → 4 → 8 → 16).

Infinite loop

pythonEasy
Retry loop never stops on success

attempts_until_success(n) keeps trying until try number n succeeds, and returns how many tries it took. Example: attempts_until_success(3) → 3.

Infinite loop

javascriptHard
Removing duplicates never finishes

dedupe(arr) returns the array without duplicates, keeping the first copy of each value. Example: dedupe([1, 2, 2, 3]) → [1, 2, 3].

Infinite loop

pythonHard
Counting pairs freezes when a pass finds none

count_pairs(nums, target) counts the pairs of numbers that add up to target. Example: count_pairs([1, 2, 3], 5) → 1 (2 + 3).

Infinite loop

javascriptHard
Search freezes when the value is missing

indexOfValue(sorted, target) returns the index of target in a sorted array, or -1 if it is not there. Example: indexOfValue([1, 3, 5, 7], 4) → -1.

Infinite loop

javascriptMedium
Reversing a word never finishes

reverseString(text) returns the text backwards. Example: reverseString("abc") → "cba".

Infinite loop

javascriptMedium
Counting odd numbers freezes on an even one

countOdds(nums) counts the odd numbers. Example: countOdds([1, 2, 3]) → 2.

Infinite loop

pythonMedium
Adding up digits never finishes

sum_digits(n) adds up the digits of a number. Example: sum_digits(123) → 6 (1 + 2 + 3).

Infinite loop

pythonMedium
Collatz steps freeze on odd numbers

shrink_to_one(n) counts the steps to reach 1. Each step: halve n if it is even, or make it 3n + 1 if it is odd. Example: shrink_to_one(6) → 8.

Infinite loop

javascriptMedium
Counting capitals freezes on a lowercase letter

countUppercase(text) counts the capital letters. Example: countUppercase("aBc") → 1.

Infinite loop

pythonMedium
Removing double spaces freezes

squash_spaces(text) turns any run of spaces into one space. Example: squash_spaces("a b") → "a b".

Infinite loop

pythonMedium
Countdown never stops

countdown(n) returns a list counting down from n to 1. Example: countdown(3) → [3, 2, 1].

Infinite loop

Stuck on your own code?

Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.

Other bug patterns