Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java

Andy Stevens
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Andy Stevens
Front End Web Development Techdegree Graduate 20,417 Points

Can someone explain why if I set a Boolean value to false and then write an if else statement, the variable stays false?

Example code below:

boolean isWon = false; if(isWon == true){ System.out.println(“congrats you won”); }else { System.out.println(“Oops not quite, try again!”); }

1 Answer

seth aruby
seth aruby
9,354 Points

Hey Andy, your conditional statement only checks to see if isWon==true, it doesn't actually change the value of isWon to be 'true'. Since you have declared isWon to be false, then it will always be false unless something in your code changes its value. Hope this helps...

Andy Stevens
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Andy Stevens
Front End Web Development Techdegree Graduate 20,417 Points

Hi Seth,

I did think that after I posted my comment, good to have my thoughts confirmed. Thank you for taking the time to answer.

I’ll admit logic is my enemy, is this something you can learn?