Infinite Loops

From CompSciWiki
Jump to: navigation, search

COMP 1010 Home > Loops


Introduction

Infinity.png
An infinite loop is any loop that either has no end condition or the end condition will never evaluate to false. It is a common problem that occurs when creating loops.


How to Tell You Have an Infinite Loop

You can usally tell when you have an infinite loop if your program seems to freeze up. A good way to pinpoint exactly where the problem is, is to add a line of code that will display to System.out any variables found in the test conditions. As long as you are capturing the output when you run your program inside Dr. Java or TextPad you will be able to see exactly what is going on. This simple example will display the variables x and y with each iteration or run through.

 double x, y;        //Since x and y are doubles they can hold decimal places

//Initiate x to less than 1 and y to 1
x = 0.5;
y = 1;

//This loop continues while x is less than y
while(x < y) {
    //This line will print the values x and y on their own lines
    System.out.println("x: " + x + "\ny: " + y);

    x*=x;           //This line will multiply x by itself, since x is a decimal it will stay less than 1
} 

Since the loop is so short it will display the values extremely fast while the program is still running. Once the program is stopped, as long as the input was captured, you will be able to look through much of the output from before it was stopped. The first five prints that the loop does would look like this:

 x: 0.5
y: 1.0
x: 0.25
y: 1.0
x: 0.0625
y: 1.0
x: 0.00390625
y: 1.0
x: 1.52587890625E-5
y: 1.0 

From this we can tell that since x is less than 1 it is actually getting smaller with each iteration. Therefore, it will never be larger than 1. This is simple to fix in our simple program, but if the value of x was taken from the user with Scanner or JOptionPane, there would need to be error checking to ensure they aren't inserting value that may cause problems.

Causes

Infinite loops are caused by a loop's condition(s) not evaluating to false. Some common examples of how this can happen are:


The control variable may never allow the condition to evaluate to false.

 int x = 1;
while( x > 0 )
{
    x = (x + 2) % 10 ;       //Here, when the condition is evaluated, x will always be greater than zero.
}                            //Because of this, the loop is infinite. 


The condition for the loop may never be met because it will always be true. Here, the if statement will never execute so the boolean "flag" will never be set to true. So, the loop will execute infinitely.

 boolean flag = false;
while(!flag)                 //always true
{
    if(flag)                 //the line below will never be executed because the conditional will always evaluate to ''false''.
        flag = true;
} 


Changing the value of the control variable inside of the loop should not be done and can cause the loop to repeat infinitely.

 for(int i = 0; i < 20; i++)   //because '''i''' is reset to 0 every time, the condition will always evaluate to ''true''.
{
    i = 0;
} 


Summary

Infinite loops can be avoided by making sure it is possible to satisfy the end condition. Be careful to watch your conditions and bounds (eg: integer bounds) and you should be able to spot where and why your infinite loop is happening. To break out of the loop in Dr. Java press the "Reset" button in the menu bar or select the menu option Tools -> Reset Interactions.

Previous Page: Scope Next Page: Multiple Control Statements