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

By Paul Leahy, About.com Guide to Java

Monday's Programming Question

Monday September 15, 2008

This week's question is inspired by a piece of code that die.hard posted in the forum. The code comes from a book that he's using to learn Java. What surprised me is that it uses a combination of an infinite loop and the break statement. Personally, I use the break statement as a last resort as it breaks the logical flow of a program and makes it harder to read.

I've simplified the code so that it keeps the same style but is easier to see what's going on:

public class InfiniteLoop {

  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);

    //infinite loop begins...
    while (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)
      {
        shouldLoopEnd = true;
      }

      //if we've guessed correctly break loop
      if (shouldLoopEnd == true)       {
        System.out.println ("Woo hoo! We have a match!");
        break; // break loop
      }
      else
      {
        //around the loop we go again
        System.out.println ("Let's try again..");
      }
    }
  }
}

Can you rewrite the above code so that the program still works the same but it no longer uses the break statement? I'll post my version on Sunday.

Comments
September 16, 2008 at 8:38 am
(1) Shovan says:

public class InfiniteLoop {

public static void main(String[] args) {

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

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

//infinite loop begins…
while (loopEnd==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;
}

//if we’ve guessed correctly break loop
if (shouldLoopEnd == true) {
System.out.println (”Woo hoo! We have a match!”);
loopEnd=true; // break loop
}
else
{
//around the loop we go again
System.out.println (”Let’s try again..”);
}
}
}
}

Can this help..Added another extra boolean variable…:)

Cheers
-S

September 16, 2008 at 5:15 pm
(2) Jose Ayerdis says:

This is mine a little bit reduce but i think is understandable….. i use both number to make the match on the while enjoy:

public class InfiniteLoop {

public static void main(String… args) {

// generate a random number
int targetNumber = (int) (Math.random() * 10);
System.out.println (”The target number is: ” + targetNumber);
int guessNumber=-1;
//pseudo-infinite loop begins…
while (guessNumber!=targetNumber)
{
//first Time we don’t want this
if(guessNumber!=-1)
System.out.println (”Let’s try again..”);

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

}
//is out mean that we did it
System.out.println (”Woo hoo! We have a match!”);
}
}

October 4, 2008 at 9:29 pm
(3) Aj says:

public class InfiniteLoop {

public static void main(String[] args) {

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

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

//infinite loop begins…
while (loopEnd==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;
}

//if we’ve guessed correctly break loop
if (shouldLoopEnd == true) {
System.out.println (”Woo hoo! We have a match!”);
loopEnd=true; // break loop
}
else
{
//around the loop we go again
System.out.println (”Let’s try again..”);
}
}
}
}

Added another extra boolean variable

Leave a Comment

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

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.