This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

if , while , do while and for loop

 
Ranch Hand
Posts: 53
1
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All ,

i want to understand below point, please help me to do so.



Even compiler know that it will never execute this if , still it will not complain.
However below code


But in this case compiler know that while block does not execute since false will be always false. Therefore it will give below error.
unreachable statement
But when we use true , then


       while( true )
       {

       }




It will not complain , however it will cause infinite loop , so why compiler does not complain ? Why false inside if condition does not give any compiler error.


       while( true )
       {
           System.out.println( "print me " );
       }

       System.err.println( "print me" );



If i put like this ,it will complain that for unreachable statement. Now compiler know that while loop is infinite loop.

Same thing is for do while and for loop too.



unreachable statement.

 
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

prateek shaw wrote:Even compiler know that it will never execute this if , still it will not complain. so why compiler does not complain ? Why false inside if condition does not give any compiler error.

Yes in this case the rule for if statement is bit relaxed as compared to while. Just to make sure really rule for if statement is relaxed Or not, you can see in below code although if statement's expression has false value, compiler assumes It might break the infinite loop so doesn't complain about It and compiles successfully where we know if statement will never gets executed causing while loop execute infinitly.
 
Ranch Hand
Posts: 76
3
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

prateek shaw wrote:
But when we use true , then


       while( true )
       {

       }




It will not complain , however it will cause infinite loop , so why compiler does not complain ? Why false inside if condition does not give any compiler error.


There is no problem with this. An infinite loop by itself is perfectly acceptable. Think about it. Where is the unreachable statement in that code? Nowhere, right? There is no unreachable statement, so such code is perfectly logical.



       while( true )
       {
           System.out.println( "print me " );
       }

       System.err.println( "print me" );



If i put like this ,it will complain that for unreachable statement. Now compiler know that while loop is infinite loop.

...

unreachable statement.


Yes. Now the compiler sees your infinite loop as a problem, because now there is a statement after the loop. This statement is unreachable, because the loop can never normally end.
 
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually if(false) meant more than not executed.when compiler figure out this code it actually removes the statement in the bytecode leading to be not executed.
You can veryfiy it by using a static final boolean flag
(Assigned false) rather than simply false,and then pointout the change b/w the bytecode of the 2 implementation.you will notice a difference.so the extra lines (as compared to flag implementation) will be ommited in the earlier implementation.
Actually compiler allow this because sometimes we need to put out some debugging code or the code which will or might be used later.so one can put it here beside someother place.it really helps.
 
Drove my Chevy to the levee but the levee was dry. A wrung this tiny ad and it was still dry.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic