How to Stop an Infinite Loop

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.

Step

Open the source code in the programming software.

Step

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

Insert a line within the loop structure to determine the value of the variable such as in the following Basic language code:

Step

while x < 10 print x / line added to monitor variable/

Step

end while

Step

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

Modify the source code to satisfy the loop's condition to exit:

Step

while x < 10 print x / line added to monitor variable/ x = x + 1 end while

Step

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

Step

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