1

As part of my APCS course, I have to fix little problems in a program that's already been written so it can be referenced later. This section is on while-loops and I never learned about unreachable statements so I don't have a clue how to go about fixing it. Attached is the while loop from the program. The error shows up at the first curly bracket.

while( false ) {
    //Since the guess doesn't match, determine if it is too low or too high.
    if (userGuess > secretNumber) {
        System.out.print("Guess number " + numGuesses + " is too LOW. ");
    }
    else if (userGuess < secretNumber) {
        System.out.print("Guess number " + numGuesses + " is too HIGH. ");
    }
}
Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95
  • 1
    When do you believe `while( false )` would ever enter the loop? โ€“ Andreas Feb 28 '18 at 17:19
  • 1
    The error seems pretty self-explanatory - it is a statement which cannot be reached no matter what your code does. Think "what does this mean in English", not "what might Java want this error message to mean". โ€“ Bernhard Barker Feb 28 '18 at 17:19
  • Related: [Unreachable statement compile error in Java](https://stackoverflow.com/q/18308159). [Why does Java have an "unreachable statement" compiler error?](https://stackoverflow.com/q/3795585) โ€“ Bernhard Barker Feb 28 '18 at 17:21

3 Answers3

4

The specific part of the language spec (for Java 9, at least) is Sec 14.21, which describes the conditions under which statements are considered unreachable.

In particular:

It is a compile-time error if a statement cannot be executed because it is unreachable.

and (emphases mine):

A while statement can complete normally iff at least one of the following is true:

  • The while statement is reachable and the condition expression is not a constant expression (ยง15.28) with value true.

  • There is a reachable break statement that exits the while statement.

The contained statement is reachable iff the while statement is reachable and the condition expression is not a constant expression whose value is false.

So, the body of your while loop is considered unreachable because the value of the expression is false, and because it's constant.

To fix it, change either of those conditions; or remove the while loop, since it doesn't logically do anything anyway.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
2

while(false){//...} line will never allow the code to reach either the inner if or the else statement.

The if and else statement will be only evaluated if the condition expression in the while construct is not a constant false, which is not the case in your code.

Hence, the compiler complains that your if & else statements cannot be reached in the current scenario.

You can instead use a variable and assign it the boolean value, as demonstrated by Mr. Elliott Frisch in his answer.

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
0

With the loop construct while ( boolean ) the body of the loop is only entered if boolean evaluates to true. A boolean literal false cannot evaluate to true, thus the body is unreachable code.

boolean loopControl = false;
while ( loopControl ) { // <-- this is fine ..
   // ...
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249