Page 1 of 1
About Question enthuware.ocajp.i.v8.2.1193 :
Posted: Sat Apr 23, 2016 11:40 am
by Jokumo♫
isn't the while part unreachable code?
do { break ; } while (true) ;
I tried it out and it compiles without error. So I tried it out with some code in the while part and managed to crash the compiler

(reported it on
http://bugreport.java.com as suggested by the compiler)
Code: Select all
public class DoWhileBug {
public static void main(String... args){
Integer i = 100;
do{ break; } while(i++ < 10);
}
}
however, on using int instead of an Integer it compiles without error, but wouldn't it be unreachable code?
Re: About Question enthuware.ocajp.i.v8.2.1193 :
Posted: Sat Apr 23, 2016 9:05 pm
by admin
Jokumo♫ wrote:isn't the while part unreachable code?
The compiler doesn't execute the code so it doesn't know that the code in the body will always cause the loop to break. That is why it cannot flag the unreachable error.
The compiler can only look at the compile time constant values to determine unreachable code.
Here is a good discussion about this:
http://stackoverflow.com/questions/3795 ... iler-error
Re: About Question enthuware.ocajp.i.v8.2.1193 :
Posted: Sun Apr 24, 2016 4:26 am
by Jokumo♫
but if I add a System.out.println(); after the break the compiler complains about it as unreachable code.
good discussion by the way, but I have no problem to just accept that java doesn't accept unreachable code, exept of if(false){ ... }. And in real life I would never ever write such a silly peace of code as mentioned in my first post. But since we have to know every tiny silliness and exception for the exam, I am wondering if this is another exception of the rule, a bug in the compiler or something completely different I hadn't thought about it until now.
Re: About Question enthuware.ocajp.i.v8.2.1193 :
Posted: Mon Aug 14, 2017 12:37 pm
by shambhavi
Even I have the doubt : if I add a System.out.println(); after the break the compiler complains about it as unreachable code.
so in that case, the compiler knows about the break causing an unreachable statement. then why will the while() in the question not become unreachable ?
Re: About Question enthuware.ocajp.i.v8.2.1193 :
Posted: Mon Aug 14, 2017 8:28 pm
by admin
did you go through the stackoverflow link mentioned above?
Re: About Question enthuware.ocajp.i.v8.2.1193 :
Posted: Tue Aug 15, 2017 12:58 am
by shambhavi
its a little confusing. can you explain please
Re: About Question enthuware.ocajp.i.v8.2.1193 :
Posted: Tue Aug 15, 2017 1:02 am
by admin
Attribute it to inconsistency in the language

As you found out in the discussion here:
https://coderanch.com/t/683311/certific ... piled-Code
Re: About Question enthuware.ocajp.i.v8.2.1193 :
Posted: Tue Aug 15, 2017 1:30 am
by shambhavi
oh okay. is it really an inconsistency but ?
Re: About Question enthuware.ocajp.i.v8.2.1193 :
Posted: Sun Dec 15, 2019 10:53 am
by tcroots19
the answer the third says, "You cannot have break or continue in an 'if' or 'else' block without being inside a loop." but then goes on to give an if/else with a label before it and a break inside...which does compile. So it seems you can do it outside a loop you just need a label that is in scope, i.e. a label for the if statement. Is that correct?
Interesting discussion on it here,
https://stackoverflow.com/questions/415 ... va-what-if enjoyed the "herd theory" comment
Re: About Question enthuware.ocajp.i.v8.2.1193 :
Posted: Sun Dec 15, 2019 8:43 pm
by admin
Correct. The first statement of the explanation is talking about just the break statement (which is applicable only in a loop). The exception to this rule is the "labeled break" statement, which is valid in an if statement.