This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

simple if stmt query  RSS feed

 
Mark Hughes
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • 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
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
i should really stop every so often and just think things through...! ,

Thanks guys
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!