1. Home
  2. Computing & Technology
  3. Java
photo of Paul Leahy
Paul's Java Blog

By Paul Leahy, About.com Guide to Java

Answer to Monday’s Question

Sunday September 21, 2008

The goal of Monday's programming question was to rewrite a piece of code to change an infinite while loop with the removal of a break statement.

I'm not going to say there's a 'right' way to rewrite the code. The key was to make the program function the same but to let the code leave the while loop logically rather than making a sudden jump. If your code did that then well done!

Here's how I did it:

public class InfiniteLoopRewrite {

  public static void main(String[] args) {

    //boolean variable to determine if the loop should end
    boolean shouldLoopEnd = false;

    // generate a random number
    int targetNumber = (int) (Math.random() * 10);
    System.out.println ("The target number is: " + targetNumber);

    //no longer an infinite loop..
    while (shouldLoopEnd == false) {

      // generate a random number to try and match the target number
      int guessNumber = (int) (Math.random() * 10);
      System.out.println ("The loop guesses: " + guessNumber);

      //check to see if the guess is correct
      if (guessNumber == targetNumber) {

        shouldLoopEnd = true;

      } //end if
      else {

        //else: round the loop we go again
        System.out.println ("Let's try again..");

      } //end if/else

    } //end loop

    System.out.println ("Woo hoo! We have a match!");

  } //end method

} //end class
Comments
September 27, 2008 at 1:26 pm
(1) Oktay says:

I think it is better to re-write the code in this way:

public class InfiniteLoop {

public static void main(String[] args) {

//boolean variable to determine if the loop should end
boolean shouldLoopContinue = true;

// generate a random number
int targetNumber = (int) (Math.random() * 10);
System.out.println (”The target number is: ” + targetNumber);

//infinite loop begins…
while (shouldLoopContinue == true)
{
// generate a random number to try and match the target number
int guessNumber = (int) (Math.random() * 10);
System.out.println (”The loop guesses: ” + guessNumber);

//check to see if the guess is correct
if (guessNumber == targetNumber)
{
shouldLoopContinue = false;
System.out.println (”Woo hoo! We have a match!”);
}
else
{
//around the loop we go again
System.out.println (”Let’s try again..”);
}
}
}
}

September 27, 2008 at 1:27 pm
(2) Oktay says:

I think it is better to re-write the code in this way:

public class InfiniteLoop {

public static void main(String[] args) {

//boolean variable to determine if the loop should end
boolean shouldLoopContinue = true;

// generate a random number
int targetNumber = (int) (Math.random() * 10);
System.out.println (”The target number is: ” + targetNumber);

//infinite loop begins…
while (shouldLoopContinue == true)
{
// generate a random number to try and match the target number
int guessNumber = (int) (Math.random() * 10);
System.out.println (”The loop guesses: ” + guessNumber);

//check to see if the guess is correct
if (guessNumber == targetNumber)
{
shouldLoopContinue = false;
System.out.println (”Woo hoo! We have a match!”);
}
else
{
//around the loop we go again
System.out.println (”Let’s try again..”);
}
}
}
}

September 27, 2008 at 1:51 pm
(3) Oktay says:

I think it is better to re-write the code this way:

public class InfiniteLoop {

public static void main(String[] args) {

//boolean variable to determine if the loop should end
boolean shouldLoopContinue = true;

// generate a random number
int targetNumber = (int) (Math.random() * 10);
System.out.println (”The target number is: ” + targetNumber);

//infinite loop begins…
while (shouldLoopContinue == true)
{
// generate a random number to try and match the target number
int guessNumber = (int) (Math.random() * 10);
System.out.println (”The loop guesses: ” + guessNumber);

//check to see if the guess is correct
if (guessNumber == targetNumber)
{
shouldLoopContinue = false;
System.out.println (”Woo hoo! We have a match!”);
}
else
{
//around the loop we go again
System.out.println (”Let’s try again..”);
}
}
}
}

September 27, 2008 at 5:16 pm
(4) Paul Leahy says:

yep, that’s a perfectly fine way of doing it too.

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

Discuss
Community Forum
Explore Java
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Java

©2009 About.com, a part of The New York Times Company.

All rights reserved.