This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Svelte and Sapper in Action and have Mark Volkmann on-line!
See this thread for details.
Win a copy of Svelte and Sapper in Action this week in the JavaScript 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

compiler-time condition

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,




I wonder, why does it work in that way ? Sometimes compiler treats condition with if(true) like either 'compile-time' or not.
Thanks for any suggestions

Greets
 
Bartender
Posts: 4107
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Difference in your example1 and example2 is that in the example1 you use a non-final boolean variable (b) so the compiler doesn't know whether the value of b will always be true, whereas example2 uses boolean literal "true" inside the if condition so the compiler knows it exactly. Both example3 & 4 share the same reason above.
 
Lukasz Wozniak
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, i understand that. But example 4 is the main point of my question : like you said Vijitha, compiler knows exactly what 'if(true)' is. So, in my opinion, it should be a compile error on line 19, because line is unreachable since we throw new exception. But line 19 compiles fine.
 
author
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lukasz Wozniak wrote:Yes, i understand that. But example 4 is the main point of my question : like you said Vijitha, compiler knows exactly what 'if(true)' is. So, in my opinion, it should be a compile error on line 19, because line is unreachable since we throw new exception. But line 19 compiles fine.



The reasoning for example 1 and 2, is different for example 3 and 4. Example 1 and 2 is explained by Vijitha above.

As for example 3 and 4, the "unreachable code" checks are disabled for "if" conditions. The reasoning is to allow for conditional code. It is common practice to have something like this...



where "debugging" is a compile time constant. So, if the checks were not disabled, and debugging is off, you can have unreachable inside the "if" block.

Henry
 
Vijitha Kumara
Bartender
Posts: 4107
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry Wong . I didn't know that.
 
Lukasz Wozniak
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All clear, thanks a lot!
    Bookmark Topic Watch Topic
  • New Topic