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.
Instructions
-
-
1
Open the source code in the programming software.
-
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.
-
-
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
-
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.
-
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 + 1
end while -
6
Execute the source code and confirm that the infinite loop has been resolved.
-
7
Remove the line of code added to debug the infinite loop.
-
1
Tips & Warnings
Reviewing code can often identify the source of an infinite loop before the code is executed.