BugHunt

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 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.

Errors and symptoms this causes

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.

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
Multiplier running forever

stepsToExceed(limit) counts how many times you must double 1 before passing limit, so stepsToExceed(8) returns 4.

Infinite loop

pythonEasy
Retry mechanism failing instantly

attempts_until_success(n) retries until the nth attempt succeeds and returns the number of tries, so attempts_until_success(3) returns 3.

Infinite loop

javascriptHard
Set cleaner gets stuck in a loop

dedupe(arr) should return the array with duplicates removed, keeping the first occurrence of each value.

Infinite loop

pythonHard
Nested loops timing out

count_pairs(nums, target) should count the pairs that add up to target, so count_pairs([1, 2, 3], 4) returns 1.

Infinite loop

javascriptHard
Search algorithm freezing indefinitely

indexOfValue(sorted, target) should return the index of target in a sorted array, or -1 if it is absent.

Infinite loop

javascriptMedium
String utility crashes the browser

reverseString(text) should return the string backwards, so "abc" becomes "cba".

Infinite loop

javascriptMedium
Loop hanging on specific conditions

countOdds(nums) should count the odd numbers, so countOdds([1, 2, 3]) returns 2.

Infinite loop

pythonMedium
Function hangs when calculating totals

sum_digits(n) should add up the digits of a number, so sum_digits(123) gives 6.

Infinite loop

pythonMedium
Math loop hanging on odd inputs

shrink_to_one(n) applies the Collatz rule — halve even numbers, and turn odd n into 3n + 1 — counting steps until n reaches 1. shrink_to_one(6) returns 8.

Infinite loop

javascriptMedium
Parser getting stuck on mismatches

countUppercase(text) should count the uppercase letters, so countUppercase("aBc") returns 1.

Infinite loop

pythonMedium
String formatter freezing the app

squash_spaces(text) should collapse runs of spaces into one, so squash_spaces("a b") returns "a b".

Infinite loop

pythonMedium
Timer getting stuck in infinite loop

countdown(n) should return a list counting down from n to 1, e.g. 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