Start learning free

LESSON 118 OF 338 · WHILE LOOPS & FOR LOOPS

Infinite While Loops in Go: Why a Loop Never Stops

Lesson Read the full explanation

Loops bring great power but also great responsability. The biggest danger when using loops is writing an infinite loop.

An infinite loop is a loop that never terminates. For example, if we forget to increment i in our previous example:

i := 1
for i < 5 {
	fmt.Println(i)
	// i++ => forgot this
}

This program will never terminate as i will always have value 1 and thus the condition i < 5 will always be satisfied.

The program would basically print 1 forever never exiting the while loop.


Pro Tip:

As programmers, we want to make sure we never write infinite loops as they make our program run forever and completely unusable.


Assignment
Follow the Coding Tutorial and practice with infinite while loops!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    GO · WHILE LOOPS & FOR LOOPS

    Infinite While Loops in Go, explained

    What an infinite loop is

    An infinite loop is a loop that never terminates. In Go it is a for whose condition stays true forever. A program stuck in one runs without end and looks frozen, so avoiding infinite loops matters in every program you write.

    Cause 1: forgetting the update

    The simplest way to get one is to leave out the line that changes the loop variable:

    i := 1
    for i < 5 {
        fmt.Println(i)
        // i++ is missing
    }
    

    The value of i is always 1, so i < 5 is always true. The program prints 1 over and over and never exits.

    Cause 2: a condition the variable skips over

    A subtler case is an update that is present but never lands on the value the condition is waiting for. The exercise has exactly this shape. The program should print the odd numbers from 15 down to 1 and then stop:

    counter := 15
    for counter != 0 {
        fmt.Println(counter)
        counter -= 2
    }
    

    Follow the values: 15, 13, 11, 9, 7, 5, 3, 1. The next value is -1, not 0. The counter jumps over 0 because it starts odd and steps by 2, so it can never be 0. The condition counter != 0 stays true for every value, and the loop continues with -1, -3, -5 and so on without end.

    The fix: test a range, not one exact value

    The loop should stop once the counter is no longer positive. A comparison against a range does that, even if the value skips over the boundary:

    counter := 15
    for counter > 0 {
        fmt.Println(counter)
        counter -= 2
    }
    

    This prints 15, 13, 11, 9, 7, 5, 3 and 1. After 1 is printed, counter becomes -1. The check -1 > 0 is false, so the loop ends. It does not matter that 0 was skipped, because the condition only asks whether the value is still above 0.

    Why a larger step makes the first fix risky

    With a step of 1, counter != 0 would work for a countdown from 15: the values 15, 14, ..., 1, 0 do include 0. The condition looks correct, and it is only the step of 2 that breaks it. This is why the bug is easy to miss. If someone later changes the step, a loop that used to work becomes endless. A range comparison keeps working for any step.

    How to avoid infinite loops

    • Check that the body changes a variable used in the condition.
    • Check that the change moves the variable toward making the condition false: down for > and >=, up for < and <=.
    • Prefer <, <=, > and >= over != when the variable moves in steps, because steps can jump over an exact value.
    • Trace the last few passes by hand, including the first value after the final one you want.

    Intentional infinite loops

    Not every endless loop is a mistake. In Go, a bare for { ... } with no condition loops forever on purpose, and the code inside ends it with break or return when some situation is reached. That is a deliberate design with an exit. The loops in this lesson are accidents, because nothing in them can ever make the condition false.

    If your program is stuck

    If you run a program in a terminal and it does not stop, press Ctrl+C to interrupt it. Then look at the loop condition and the update. Printing the loop variable on every pass, as in these examples, makes the problem visible quickly, because you can see the values going past the point where you wanted them to stop.

    Key takeaways

    • A loop is infinite when its condition can never become false.
    • Two common causes are a missing update and a condition on an exact value that the variable skips.
    • Use a range comparison such as counter > 0 when the step size could jump past the boundary.
    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free