Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

While(false) causes unreachable statement compilation error

I was recently removing a block of code from our code base before a release and used an if(false) statement to prevent execution:

if (false) {
    ArrayList<String> list = new ArrayList<String>();
    ...
}

This compiles fine and will prevent execution of the offending block of code (right or wrong, that's not the current argument).

However, kind of by accident, I changed the block above to:

while (false) {
    ArrayList<String> list = new ArrayList<String>();
    ...
}

and received an unreachable statement compilation error.

I appreciate the compilation error and understand the reasons, however, I'm struggling to comprehend the difference between the two blocks and why the former compiles fine but the latter does not when they both have unreachable statements.

like image 525
Richard Avatar asked May 08 '13 14:05

Richard


People also ask

What causes an unreachable statement?

The Unreachable statements refers to statements that won't get executed during the execution of the program are called Unreachable Statements. These statements might be unreachable because of the following reasons: Have a return statement before them. Have an infinite loop before them.

How do you resolve an unreachable statement?

1(a) is compiled, line 12 raises an unreachable statement error because the break statement exits the for loop and the successive statement cannot be executed. To address this issue, the control flow needs to be restructured and the unreachable statement removed, or moved outside the enclosing block, as shown in Fig.

Is unreachable code a compile time error?

If any code can not be executable in any of the possible flows, then it is called unreachable code. Unreachable code in java is a compile time error.

How do you fix unreachable statement in Java?

If you keep any statements after the return statement those statements are unreachable statements by the controller. By using return statement we are telling control should go back to its caller explicitly .


2 Answers

In both case the compiler should raise an error, because the code between braces is essentially pointless, but if (false) was kept in Java to simulate C/C++ preprocessor #if 0, quite a common way of disabling parts of code for testing or debugging.

EDIT: for reference, "conditional compiling" is detailed at the end of chapter 14.21 of the Java Language Specification.

like image 54
Stefano Sanfilippo Avatar answered Nov 15 '22 01:11

Stefano Sanfilippo


"Java uses a simple flow analysis algorithm to find most common cases of unreachable code, and all such unreachable code blocks will be flagged as compile-time errors. That's why your "while (false) { ... }" statement produces an error.

However, Java makes a special exception for "if (false) { ... }", because programmers often use this construct during development to temporarily disable part of the program. That's why the compiler accepts this statement.

If you're interested in the nitty-gritty details, refer to the Java Language Specification's description of unreachable statements @ http://docs.oracle.com/javase/specs/#14.21."

Quoted from http://www.coderanch.com/t/266678/java-programmer-SCJP/certification/false-false

like image 31
Jim Avatar answered Nov 15 '22 02:11

Jim