• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

continue and break in a loop would generate compilation error?

 
nemo zou
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the code below. I don't understand why only the first one is fine with the compiler while the second and third will not compile.




 
Henry Wong
author
Marshal
Pie
Posts: 22124
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nemo zou wrote:Look at the code below. I don't understand why only the first one is fine with the compiler while the second and third will not compile.


Well, technically, all three have unreachable code, and all three should not compile. However, the Java specification relaxes the unreachable code requirements for the "if" condition; this is done to allow you to conditionally compile (which is common for debugging / testing code). This is why the first case works, when obviously, it contains unreachable code.

Henry
 
nemo zou
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Well, technically, all three have unreachable code, and all three should not compile. However, the Java specification relaxes the unreachable code requirements for the "if" condition; this is done to allow you to conditionally compile (which is common for debugging / testing code). This is why the first case works, when obviously, it contains unreachable code.

Henry


Does the dead code/unreachable code compilation error only happen when you have code after break/continue in for/while loops? How about switch-case or other situations? Thanks~
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anytime it's unreachable, with the exception noted above. Try/catch can be a little tricky.


 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nemo zou wrote:Does the dead code/unreachable code compilation error only happen when you have code after break/continue in for/while loops? How about switch-case or other situations?

Very important: dead code is not the same as unreachable code! If your code has unreachable code, you'll get a compiler error; if your code has dead code, the code compiles successfully. An example of unreachable codeThis code will not compile.

And an example of dead codeThis code compiles successfully.

It's very hard to explain every possible situation about when code is unreachable and thus will generate a compiler error. Therefore the best thing you can do, is create dozens of little code snippets and check if the code compiles or not. If you think about a situation (e.g. a while with continue statement), create a code snippet and see what happens. Then change continue with break, then add an if statement and so on. Some examples as starting point and it's up to you to decide if these little code snippets compile or not. Good luck! See how easy it is to create a gazillion of code snippets

Using the search function on this forum you'll probably find some other topics about unreachable code (e.g. here and here).

Hope it helps!
Kind regards,
Roel
 
nemo zou
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! I will just try to remember several examples and hope the test won't go so detailed on this topic
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nemo zou wrote:I will just try to remember several examples and hope the test won't go some detailed on this topic

Not really the right attitude
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To further add to the mind numbing stupidity

is unreachable, while

is just fine
 
Sergej Smoljanov
Ranch Hand
Posts: 468
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JLS7:Chapter 14. Blocks and Statements
JLS8:Chapter 14. Blocks and Statements
http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.28 and this one.
for me this part of jls is hard to read but lead to good understand of unreachable.
when i work with it i write many code, and after understanding rule - i wrote it to my conspectus (than many time repeat).
(second link may be same because i pass exam for java 7 i read just for java 7)
and this type of question (about unreachable) was at my exam so recommend fight for every point on actual exam!
and second problem when right answer was compilation false - if you not figure it you will not only will have wrong answer but you may waste time on actual exam
Any tricks about how to manage the 120 min for so many question?
 
Henry Wong
author
Marshal
Pie
Posts: 22124
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nemo zou wrote:
Does the dead code/unreachable code compilation error only happen when you have code after break/continue in for/while loops? How about switch-case or other situations? Thanks~


If I remember correctly (and I don't remember why I know this originally)... All the blocks in the switch block are indeed checked for unreachable code, but the labels themselves are not. Meaning all of the case labels are assumed reachable even if it is switching on a compile time constant that is determined to not reach that label.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 22124
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guillermo Ishi wrote:To further add to the mind numbing stupidity

is unreachable, while

is just fine


Well, arguably ... the first is a compile time constant and the second is not. And, to be fair, the compiler can only work at compile time, and hence, can only determine values at compile time.

Henry
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guillermo Ishi wrote:To further add to the mind numbing stupidity

is unreachable, while

is just fine

No stupidity here! Like I already mentioned in another topic, the compiler is very lazy and doesn't execute any code at all. So that means (like Henry already mentioned) if it's a compile time constant, the compiler knows the value and can decide if the code is unreachable or not. If it's not a compile time constant, the compiler doesn't know (bother) and thus can't evaluate if the code is (un)reachable.

So if you add the keyword final to variable b, b will be a compile time constant and the code won't compile due to unreachable code.

Hope it helps!
Kind regards,
Roel
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand the explanation. I just don't accept it
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guillermo Ishi wrote:I understand the explanation. I just don't accept it

The Java compiler is like Chuck Norris: you don't want to mess with him
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3820
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java 8 has incorporated the use of variables that "effectively final". i.e. the local variables not declared as final but that are never changed in the code. There is no reason why the compiler cannot make use of this information to flag even more unreachable code such as the one mentioned above by Guillermo.
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's the idea. There are no limits on the compiler except what they create. I'm spoiled by old C, before C++ even, where it was just remarkably consistent and you knew intuitively what to expect. It was like one guy to another, one anonymous engineer to another, you know? That is beautiful. But in fairness maybe, a lot of this stuff only shows up in these weird cases you need to be aware of for this test. In reality you'd be typing along and some little thing happens and you make some little change and no problem and no further thought about it. And Chuck Norris might know karate but on this side of the pond a lot of guys know Smith & Wesson
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic