The behaviour if the code
private int Run(){
while (true){
if(false){
break;
}
}
}
is a compile-error or not is defined in the Java Language Specification, specifically in Chapter 14. Blocks and Statements. Statements can "complete normally" or "complete abruptly". Based on these information, the execution of other statements are affected.
Chapter 14.15. The break Statement defines that the break
statement will complete the "break target" normally:
A break
statement with no label attempts to transfer control to the innermost enclosing switch
, while
, do
, or for
statement of the immediately enclosing method or initializer; this statement, which is called the break target, then immediately completes normally.
Also chapter 14.21. Unreachable Statements defines the following:
("iff" means "if and only if", as defined in chapter 14.21 and explained in https://en.wikipedia.org/wiki/If_and_only_if)
The first condition is false in our case as we have the condition expression true
for the while
loop. Notice that this section defines that a while
loop with while(true) {...}
without any break
statements will not "complete normally".
Now we need to check if there is a "reachable" break
statement.
The break
statement is "reachable" because of the following chain:
- The block that is the body of a constructor, method, instance initializer, or static initializer is reachable.
The {...}
block of the method.
The first statement in a non-empty block that is not a switch block is reachable iff the block is reachable.
So the while
loop is reachable.
The contained statement is reachable iff the while statement is reachable and the condition expression is not a constant expression whose value is false
.
As the while
loop is reachable and the condition expression is not false
, the inner block {...}
of the while
statement is reachable. The if()
statement inside the {...}
block is reachable again because it is the first statement in a reachable block (see above).
The then-statement is reachable iff the if-then statement is reachable.
The {...}
block of the if-statement is reachable, so is the first statement break
of that block (see above).
Notice that the condition of the if()
statement is not checked during this analysis of the compiler (as indicated in the non-normative section at the bottom of chapter 14.21).
This whole "reachable" checks means that the while
loop can complete normally. This means that the {...}
block of the method can complete normally, see chapter 14.2. Blocks:
If all of these block statements complete normally, then the block completes normally
And that means that a return
statement is required, as chapter 8.4.7. Method Body defines it:
If a method is declared to have a return type (§8.4.5), then a compile-time error occurs if the body of the method can complete normally (§14.1).
Therefore, you need a return
statement, even though you have a break
statement which cannot be executed. But having the reachable break
statement there changes everything. This is what is "happening with java", regardless of what C# or C++ is doing.