• 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

simple if stmt query

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys,

In an If else stmt, if the first if is true, will the comiler even bother goin on to test the next if stmt?

for example


in the above, message will always have a value and never will be null, and so that is all that ever gets executed even if message value is hi, it will not print "hi is the value of message" just the contents of message.
This this just the behavior of an if else or could it be improved to work the way i want it.
ie
print contents of message when not null and if it is set to hi, print the hi is the value of message message.

Thanks guys, probably something really small to change
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The results of a conditional statment will be resolved at runtime not compile time. So, the compiler will "go" to the else if part of your statement, since it need to compile this code.

Even if you write this:

the compiler will not complain. However, you will never see the output at runtime.


...all that ever gets executed even if message value is hi, it will not print "hi is the value of message" just the contents of message.



Your conditional as it is written (assuming your println method just prints to stdout) will print the value of the message if it is not null. You will never see "hi is the value of message".

If you want it to behave differently you just need to write it in a different way. The easiest to understand would be to nest another conditional statement. In pseudo-code this would look something like:

[ October 16, 2006: Message edited by: Paul Sturrock ]
 
Mark Hughes
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yea that is what is happening, it is only printing the contents of message,
Thats just how the conditional stmt works in java yea?

can ya advise on any method that will work the way i would like it to work?


This will execute the below if else, but also the first if at the same time,
i would like to just execute the first if when hi and bye are not stored in message, is this possible
Regards
mark
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

i would like to just execute the first if when hi and bye are not stored in message, is this possible



Why don't you do exactly what you stated, and execute the first "if" only "when hi and bye are not stored in message"?



Henry
 
Mark Hughes
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i should really stop every so often and just think things through...! ,

Thanks guys
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic