• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Mysterious if statement.

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all...
Look at the below 2 cases
Case 1:-
boolean b = false;
if (b)
{
System.out.println("Does not Execute ");
}
Case 2:-
boolean b = false;
if (b);
{
System.out.println("Executed ");
}
Can someone explain me why the semicolon at end of if statement resulted
into Execution of S.o.P in case 2, even when the value of b = false.
But if u change the value of b to true, then in both cases the S.o.p Executes.
Why is it so ???
Thanks.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
If I am not mistaken, the semicolon terminates the
if statement. Thus, either way the S.o.p is executed as if the "mysterious if" didn't exist.
Hope that helps
Regards
Alex
 
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
without the semi-colon, then the statement following in the block is unreachable. But with the semi-colon, that terminates the if statement, and the block below is an independent block meaning that it will always be executed, and will always be reachable.
 
reply
    Bookmark Topic Watch Topic
  • New Topic