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

Boolean and if statement confusion

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your help Sunni with visualizing what christina had stated.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
we can reduce the complexity as in
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and Welcome to JavaRanch Bryce Wade
 
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Python Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Well THAT's new! Comfort me, reliable tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic