This Season
 

How to Use an Infinite Loop

How to Use an Infinite Loopthumbnail
Infinite loops are useful, but should be used carefully.

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.

Related Searches:
    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.

    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

    Read Next:

    Comments

    You May Also Like

    • Purpose of an Infinite Loop

      An infinite loop is often caused by faulty programming, generally by incorrectly setting or testing loop-control variables. By consuming a great deal...

    • 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...

    • Common Causes of Infinite Loops

      Common Causes of Infinite Loops. A computer programming loop executes an iterative process that runs a finite number of times (n). Set...

    • Define Infinite Loop

      In computer programming, a loop is a series of instructions a computer executes a fixed number of times. An infinite loop is...

    • How Can Infinite Loops Be Created?

      In computing, an infinite loop is a sequence of instructions (or code) that will repeat endlessly, as there is no condition that...

    • How to Fix an Infinite Loop

      When in the course of programming an application, the program is going to run across several errors. This is inevitable, as programming...

    • How to Make an Infinite Loop Program in Basic

      The Basic programming language is just that--basic. It uses commands and terms that make basic sense to just about anyone that would...

    • How to Prevent a Mail Loop

      A mail loop can quickly become a hassle for any home or business computer. A loop occurs when a chain is formed...

    • How to Prevent Infinite Loop

      When you create an animation using Adobe Flash software, the animation is set by default to play on an infinite loop, meaning...

    • How to Resolve a Java Language Stack Overflow Error

      Exit the Java program run in Step 3. If you still see a Stack overflow exception output to the console, then you...

    Follow eHow

    Related Ads