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.
Things You'll Need
- Programming software
- Source code with infinite loop
Open the source code in the programming software.
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.
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
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.
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
Execute the source code and confirm that the infinite loop has been resolved.
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.