Win a copy of Spark in Action this week in the Open Source Projects forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Why is it possible to have duplicate hard-coded values in if-elseif-else statements?

 
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Consider the following code.



Why does this compile? Isn't a large part of this code unreachable at compile time? For example the first else if statement, which cannot ever be reached.

Regards,
Shane
 
author
Posts: 23877
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shane Jensen wrote:
Why does this compile? Isn't a large part of this code unreachable at compile time? For example the first else if statement, which cannot ever be reached.



The unreachable code rules, are relaxed for if-else blocks, in order to allow for conditional compilation. This is needed as using debugging flags, to activate/deactivate code, is a common practice.

Henry
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shane Jensen wrote:Why does this compile? Isn't a large part of this code unreachable at compile time? For example the first else if statement, which cannot ever be reached.


Luckily for you, the "unreachable code" topic is one of the more popular ones in this forum. So using the search function you'll find plenty of topics about "unreachable code". Here is a list which could probably be added to my certification book
  • return value for method.
  • Confused point about infinite loop
  • continue and break in a loop would generate compilation error?
  • Which of these is unreachable code?
  • Unreachable Code
  • Which is the first line to cause error?
  • Maybe "unreachable code" should be "dead code" in page 70, (Java OCA 8 Programmer I Study Guide)
  • Throwing a second exception question
  • System.exit() and unreachable code
  • good examples of unreachable code?

  • I think you know what to do this weekend

    Hope it helps!
    Kind regards,
    Roel
     
    Rebecca Wolf
    Ranch Hand
    Posts: 113
    7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks, guys.

    Roel De Nijs wrote:
    I think you know what to do this weekend


    I have my exam this Monday, so I'll save it for later. ;)
     
    Roel De Nijs
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Shane Jensen wrote:I have my exam this Monday


    Best of luck!
     
    Rebecca Wolf
    Ranch Hand
    Posts: 113
    7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Roel De Nijs wrote:

    Shane Jensen wrote:I have my exam this Monday


    Best of luck!


    Thanks! I'm sure I'll be fine, largely thanks to everyone's help on this forum.
     
    Every time you till, you lose 30% of your organic matter. But this tiny ad is durable:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
      Bookmark Topic Watch Topic
    • New Topic