How to Stop an Infinite Loop

By Keith Perry

Loops are programming devices that specify that a section of a program is to be performed one or more times. Typically they are divided into two categories, "for loops" and "while loops". A "for loop" is executed a set number of times. A "while loop" continues to execute until a predefined condition is met. Creating an infinite loop is a very common occurrence for developers.

Things You'll Need

  • Programming software
  • Source code with infinite loop

Step 1

Open the source code in the programming software.

Step 2

Identify the infinite loop in the source code. During an infinite loop a program often does not return output or accept input. Start by looking at the code in the area after the last successful input or output action occurred.

Step 3

Insert a line within the loop structure to determine the value of the variable such as in the following Basic language code:while x < 10 print x /* line added to monitor variable*/ end while

Step 4

Execute the code and monitor the value of the variable. Determine why the variable is not reaching the value required to fulfill the conditional statement.

Step 5

Modify the source code to satisfy the loop's condition to exit:while x < 10 print x /* line added to monitor variable*/ x = x + 1end while

Step 6

Execute the source code and confirm that the infinite loop has been resolved.

Step 7

Remove the line of code added to debug the infinite loop.

Tips & Warnings

  • Reviewing code can often identify the source of an infinite loop before the code is executed.

References & Resources