While and For Loops


Run Time Errors


Description: Infinite loop

Signs of an infinite loop:

  1. It outputs values continuously.
  2. It asks for input continuously.
  3. Nothing is happening when you execute the program. In this case, move your cursor to the Interaction Pane. If the shape of the cursor changes to a time glass, then your program has an infinite loop.

Cause: In most cases, infinite loops occur because the exit condition/Boolean expression can never be met. In a for loop, this means that your counter can never reach the exit value. For example:

	for (int i = 0; i < 5; i--)

Since i is decreasing in value, it can never reach the exit value "5". Hence, the loop will run forever. In a while loop, an infinite loop occurs because the variable in the Boolean statement is never changing. For example:

	Scanner s = new Scanner(System.in);
	String input = s.nextLine();
	while (! input.equals("QUIT"))
	{ String p = s.nextLine();
	}

If the user does not enter "QUIT" the first time, you will have an infinite loop. This is because the result of the Boolean statement depends on the value of input. Since input never changes within the while loop, the Boolean statement will always return the same result (input will always not be equal to "QUIT"). Hence, there is no way of exiting the while loop.

Solution: If your program is still executing, you must first exit the infinite loop. If your program is not asking for input, simply click the "Reset" button in Dr. Java (3rd button from the right). Otherwise, hold down Enter and click on Reset continuously. This should reset your program.

When writing for and while loops, the exit condition needs to be satisfied eventually. Once you notice the cause of the infinite loop, change the exit condition or the increment value so that it can be reached by the counter/variable. For example, for (int i = 0; i < 5; i--) can be changed to for (int i = 0; i < 5; i++) or for (int i = 0; i > -5; i--), so that i eventually reaches 5 or -5.

For a while loop, change the Boolean statement so that it includes a variable whose value is changing within the loop. In the previous example, we can fix the problem by changing the code to:

	Scanner s = new Scanner(System.in);
	String p = s.nextLine();
	while (p.equals("QUIT"))
	{ p = s.nextLine();
	}

Notice that the Boolean expression will now depend on p, whose value is always changing inside the while loop.


Logic Errors


Description: The for/while loop is running only once no matter what Boolean expression/exit condition is used.

Example:

	for (int i = 0; i < 5; i++);
	   System.out.println("HELLO");

Hello is printed only once even though the for loop should be executed five times.

Cause: You should not put a semicolon at the end of a for or while loop line. If you do, Java thinks that the body of the loop is empty. It treats the line after the loop as a stand-alone statement. Hence, Hello is printed only once.

Solution: Remove the semicolon at the end of the loop line:

 	for (int i = 0; i < 5; i++)
   	   System.out.println("HELLO");