How to Use an Infinite Loop
The patterns and methods you use in programming reflect your own approach to problem solving. Often, a programming requirement may potentially be solved using any of a number of possible solutions. It's advisable to not only use an approach that will be effective and efficient, but also one that makes sense to you. When you attempt to compile and debug your programs, having a clear understanding of what is happening when the code executes is essential. Infinite loops are a useful programming technique in many cases, but should only be used in certain situations, as a poorly conceived infinite loop can cause serious problems.
- Difficulty:
- Moderate
Instructions
-
-
1
Learn what an infinite loop does. All loops in programming work on the basis that the instructions within the loop will execute over and over again until a certain condition is met. With an infinite loop, you set the loop up so that this will never happen and the loop will simply continue to execute endlessly. In some cases, a program will indeed require a part of the code to continue executing endlessly, but in many cases programmers use infinite loops that will indeed be stopped during execution, using a "break" or "return" statement.
-
2
Consider whether an infinite loop is best suited to the problem at hand. Think about the alternatives and work through how these will impact how well your code functions and how efficiently it will function. Only use an infinite loop if it is going to carry out the required task properly and without causing any unnecessary processing.
-
3
Construct your infinite loop. There are many patterns in code that will result in an infinite loop and different programming languages have varying approaches. These examples work in many languages, including Java and PHP:
for(;;)
{
//loop code goes here
}
while(true)
{
//loop code goes here
}
Each time a loop iterates, the computer carries out a specified test to check whether to go around again. A typical example loop, one that is not infinite, is one in which a counter is incremented each time the loop iterates and the test checks whether the counter is less than a certain number. When the counter reaches the number, the test returns a "false" result and the loop does not iterate again. The program then goes on to whatever instructions lie after the loop. With infinite loops, the test will always return a "true" result, and the loop will always go around again, unless the code inside the loop contains a "break" or "return" statement.
-
4
Test your program. Make sure you test the function that the infinite loop is being used for. Test that the functionality of the loop performs well with different inputs and throughout the range of interaction that is possible within your application, particularly if it involves a user interface. If your program does not perform as expected and gets stuck in the infinite loop, you may need to force it to close.
-
5
Debug your infinite loop. If the loop does not function as expected, take another look at it and consider what will happen when the program executes, including what happens with each iteration. Make changes to the code accordingly. If your infinite loop still does not work the way you need it to, consider using a different type of control structure instead.
-
1
Tips & Warnings
Try writing down on paper what will happen with each iteration of your loop as an aid to understanding its function.
Infinite loops naturally pose a risk where your program can become stuck. Only use them when you really feel that they are the best possible option.
Related Searches
References
Resources
- Photo Credit Loop D Loop image by BG Designs from Fotolia.com