Sometimes an indefinite loop does not end, creating an infinite loop.

An infinite loop is a loop where the condition can never be reached, causing the loop to run forever.

Sometimes this can be dangerous as it can make a program appear to be frozen to the user. But for something like a web server that constantly needs to be running, these infinite loops are critical.

While an infinite loop can be any loop that does not end, a purposefully created infinite loop is written like this:

for { // Loop body logic // This repeats forever } // This is never reached

There is also a way to stop these loops at a certain point in code, which will be discussed in the next exercise!



You run a 24/7 gym and want to count all the groups of members that walk in.

Begin by compiling and running the starting code contained in gym.go.

After running the command, click “Check Work” to continue.


Time to count all of the members.

Write an infinite loop that repeatedly calls the count function to get the total number of guests!

Try running the code again and see what happens!

When ready, click the “Check Work” button to test if the loop is set up properly.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?