Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

Which of these compile, and why? (1)

Java
return 1;
System.out.println("x");

(2)

Java
while (false) {
x++;
}

(3)

Java
if (false) {
x++;
}

(4)

Java
while (true) { }
System.out.println("after");

(5) try { } catch (java.io.IOException e) { }

Only (3) compiles. (1) fails because a statement after return can never run, and Java treats unreachable statements as compile errors. (2) fails because the body of while (false) is unreachable when the condition is a constant false. (4) fails because while (true) with no break can never complete normally, so the statement after it is unreachable. (5) fails because catching a checked exception that the try block cannot throw is an error, although catching Exception or an unchecked type is allowed. (3) compiles because the language deliberately excludes if from these rules, so that constant flags can switch code on and off.

Why this question is asked

Most languages warn about dead code; Java refuses to compile some of it. That rule surprises developers who comment out half a method while debugging, and it explains why the same-looking code compiles with if but not with while. Interviewers ask this set of five because each one tests a different part of the rules, and the one that compiles is the most interesting. In this answer, we will recap what reachable and complete normally mean, go through the five cases one at a time, look at why the designers treated if differently from while, and see how this rule affects real code.

1A Quick Recap: Reachable and Complete Normally

The Java Language Specification has a section on unreachable statements, and it says it is a compile-time error if a statement cannot be executed because it is unreachable. The analysis is deliberately simple. It does not run the program or reason about variable values; it follows fixed rules about the structure of the code.

Two ideas drive the rules. A statement is reachable if the statement before it can complete normally, which means control can flow off its end into the next statement. A statement completes abruptly when it leaves by return, throw, break or continue instead. So return can never complete normally, an infinite loop with no break can never complete normally, and whatever follows them is unreachable.

The five cases
CaseCompiles?Rule
(1) return 1; println("x");noreturn cannot complete normally
(2) while (false) { x++; }nobody unreachable: constant false condition
(3) if (false) { x++; }yesif is excluded, for conditional compilation
(4) while (true) { } println("after");noloop with no break cannot complete normally
(5) try { } catch (IOException e) { }nochecked exception never thrown in the try

2Cases 1 and 4: Nothing Can Come After

In case (1), return 1; always leaves the method, so it can never complete normally, and the println after it has no way to run. The compiler reports an unreachable statement. Case (4) is the same idea with a loop. A while loop can complete normally only if its condition is not the constant true, or if there is a reachable break that exits it. while (true) { } has neither, so it runs forever, and the println after it is unreachable.

Java
int f() {
return 1;
System.out.println("x"); // error: unreachable statement
}
void g() {
while (true) { }
System.out.println("after"); // error: unreachable statement
}
void h() {
while (true) { if (done()) break; }
System.out.println("after"); // compiles: the break lets the loop complete
}

Notice the last method. The compiler does not check whether done() can ever return true; it only sees that a reachable break exists, so the loop can complete normally. The analysis is about structure, not about what will actually happen.

3Cases 2 and 3: while (false) Versus if (false)

For a while loop, the rule says the body is reachable only if the condition is not a constant expression with the value false. A constant expression is one the compiler can evaluate at compile time, such as false, 1 + 2, or a final variable initialised with a constant. while (false) has a constant false condition, so its body is unreachable and case (2) is an error.

By that logic, if (false) { x++; } should fail too, but it compiles. The specification makes an explicit exception: the rules for if do not look at whether the condition is a constant. Both branches of an if are treated as reachable whenever the if itself is reachable.

Java
int x = 0;
while (false) { x++; } // error: unreachable statement
if (false) { x++; } // compiles; javac leaves this code out of the class file
static final boolean DEBUG = false;
if (DEBUG) { log.debug("state " + state); } // the conditional compilation idiom

The second part shows why. With a constant flag like DEBUG, we can switch whole blocks of code on and off by changing one line, and javac does not even generate bytecode for the disabled block. If if (false) were an error, this idiom would be impossible.

4Case 5: A Catch That Can Never Run

The last case uses a related rule about checked exceptions. A checked exception is one the compiler tracks, such as IOException; a method must declare it or handle it. Because the compiler knows exactly which checked exceptions a try block can throw, it knows when a catch for one of them can never run. An empty try block cannot throw IOException, so catch (IOException e) is rejected with an error saying the exception is never thrown in the body of the try statement.

Java
try { } catch (java.io.IOException e) { } // error: IOException is never thrown
try { } catch (Exception e) { } // compiles: Exception includes unchecked ones
try { } catch (IllegalStateException e) { } // compiles: unchecked
try { Files.readString(path); } catch (java.io.IOException e) { } // compiles: it can be thrown

Notice that catching Exception or any unchecked exception is always allowed. The compiler cannot prove that no RuntimeException or Error will be thrown, because almost any code can throw one, so only a catch for a specific checked exception can be proven dead.

5Why the Designers Treated if Differently From while

The specification itself explains the choice. A while loop with a constant false condition is almost always a mistake, so rejecting it helps. But an if with a constant condition is a useful tool, the closest thing Java has to the conditional compilation of C's preprocessor. Being strict about if would have forced developers into tricks like if (DEBUG && true), so the designers excluded if from the rule.

The rule also explains a habit every Java developer has: to disable code temporarily, wrapping it in if (false) compiles, while putting a return in the middle of a method does not. In practice, a feature flag read at run time or simply deleting the code is usually cleaner.

Step throughHow javac decides that a statement is reachable
  1. Can the previous statement complete normally?
    YesNext checkNoError: unreachable statement
  2. Is it the body of a while with a constant false condition?
    YesError: unreachable statementNoNext check
  3. Is it a branch of an if?
    YesReachable whatever the conditionNoApply the usual rules

3 steps in order. Play them, or step through them one at a time.

The if exception is written into the rules on purpose.

6Follow-up questions

Why did the language designers treat if differently from while?

To allow conditional compilation. With static final boolean DEBUG = false; if (DEBUG) { ... }, a single constant switches debug code on or off, and javac omits the disabled block from the bytecode. A while (false) loop has no such use and is almost always a bug, so it stays an error.

Is unreachable code after throw also an error?

Yes. throw can never complete normally, just like return, so any statement directly after it in the same block is unreachable and does not compile.

Does the analysis use the values of variables?

Only constant expressions. If DEBUG were initialised from a method call, while (DEBUG) { } would compile, because the compiler no longer knows the value. The rules look at structure and constants, never at what the program will actually do at run time.

7Conclusion

In this answer, we saw that Java rejects statements it can prove unreachable, using simple structural rules. A statement after return, the body of while (false), and a statement after while (true) without a break are all errors, and so is a catch for a checked exception the try block cannot throw. if (false) compiles because the language deliberately excludes if from the rules, which keeps constant flags usable for conditional compilation.

What to say in the interviewAbout 80 seconds aloud

Open with

Only the third one compiles. Java treats unreachable statements as compile errors, and it decides reachability with simple structural rules: a statement is reachable if the one before it can complete normally.

Then, if they want more

In the first case return never completes normally, so the println after it is unreachable. In the second, the body of a while is unreachable when the condition is a constant false, so while (false) with a body is an error. In the fourth, while (true) with no break cannot complete normally, so the statement after it is unreachable.

If there were a reachable break, it would compile, even if the break could never actually run. The fifth is a related rule for checked exceptions: an empty try cannot throw IOException, so a catch for it is rejected, while catching Exception or an unchecked exception is always allowed because the compiler cannot prove those are never thrown.

The third case, if (false), compiles because the specification deliberately excludes if from the constant check. The reason is conditional compilation: with a static final DEBUG flag set to false, if (DEBUG) lets us switch code off, and javac leaves that block out of the bytecode. A while (false) has no such use, so it stays an error.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Which of these compile, and why? (1)": https://scrivio-interview-library.netlify.app/java/operators-and-control-flow/da59996e44/