• 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

If-else

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a stmt from K&B book...

If an else statement is used and the expression evaluates to false, then the code following the else will be performed. If the else is not used, then none of the code associated with the if statement will execute.



I thought single if statements do work properly in Java. Am I wrong or does every if statment require an else attached with it?

TIA
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No , if statement doesn't require else mandatory ...

can you tell me the page no where you found this statement ...
[ February 07, 2005: Message edited by: rathi ji ]
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, the quote is quite correct. It is your understanding of it that may be flawed.
Nowhere does it state that else is required, in fact it specifically mentions what happens when there is no else clause provided.

You may have been confused by the wording "none of the code associated with the if statement will execute. " but remember that this is related to the first part of the statement which says "If an else statement is used and the expression evaluates to false" which indicates that the entire statement explains only the flow of program logic in the case that the logical expression in the if statement returns false.
If that is the case then indeed the code inside the conditional expression is not executed and optional code contained inside an else clause will be evaluated in its stead.
 
Dee Irun
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks so much!
 
reply
    Bookmark Topic Watch Topic
  • New Topic