• Post Reply Bookmark Topic Watch Topic
  • New Topic

Boolean and if statement confusion  RSS feed

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey everyone,

i am learning to code in java and have come across a problem.
the thing is the program seems completely fine to me.
id like to know if you see any reason why this code will not print "true".

Thanks for any help you can provide this n00b.

 
Ranch Hand
Posts: 33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your If loop is setting it to true and then right after you are setting it to false.
Just remove the tnt=false line because that's what's resetting it back to false
or if you really want you could make the false into your else clause
 
Greenhorn
Posts: 24
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christina Bremmerman wrote:Your If loop is setting it to true and then right after you are setting it to false.
Just remove the tnt=false line because that's what's resetting it back to false
or if you really want you could make the false into your else clause

Just writing the above in code so that it becomes clear to you...



If you want output based on whatever your if condition evaluates then make it as below
 
Bryce Wade
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your help Sunni with visualizing what christina had stated.
 
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we can reduce the complexity as in
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and Welcome to JavaRanch Bryce Wade
 
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:we can reduce the complexity as in

Or even simpler
 
Ranch Hand
Posts: 46
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A word of advice Bryce:
Braces make code more readable, use them.

Hope this helps.
 
Ranch Hand
Posts: 177
Hibernate Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christina Bremmerman wrote:Your If loop...


Sorry to correct you but if statements are no loops!
I totally agree with Venu that you should use braces in statements even when they aren't needed.

If you add a line of code later and you forget that you didn't use braces in the first place you will get weird behavior and maybe a difficult bug to find depending on code size.
 
Christina Bremmerman
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manuel Petermann wrote:
Christina Bremmerman wrote:Your If loop...


Sorry to correct you but if statements are no loops!
I totally agree with Venu that you should use braces in statements even when they aren't needed.

If you add a line of code later and you forget that you didn't use braces in the first place you will get weird behavior and maybe a difficult bug to find depending on code size.


Correction is fine...I'm a newb too and my weakness is definitely terminology...I actually looked at his post trying to see if I could figure out what was wrong with my own if statement because I am having similar issues, LoL
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!