Answer to Monday’s Question
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


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..”);
}
}
}
}
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..”);
}
}
}
}
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..”);
}
}
}
}
yep, that’s a perfectly fine way of doing it too.