How to Use This Boolean in an If Statement

if (boolean condition) in Java

Okay, so..

// As you already stated, you know that a boolean defaults to false.
boolean turnedOn;
if(turnedOn) // Here, you are saying "if turnedOn (is true, that's implicit)
{
//then do this
}
else // if it is NOT true (it is false)
{
//do this
}

Does it make more sense now?

The if statement will evaluate whatever code you put in it that returns a boolean value, and if the evaluation returns true, you enter the first block. Else (if the value is not true, it will be false, because a boolean can either be true or false) it will enter the - yep, you guessed it - the else {} block.

A more verbose example.

If I am asked "are you hungry?", the simple answer is yes (true). or no (false).

boolean isHungry = true; // I am always hungry dammit.
if(isHungry) { // Yes, I am hungry.
// Well, you should go grab a bite to eat then!
} else { // No, not really.
// Ah, good for you. More food for me!

// As if this would ever happen - bad example, sorry. ;)
}

Syntax for an If statement using a boolean

You can change the value of a bool all you want. As for an if:

if randombool == True:

works, but you can also use:

if randombool:

If you want to test whether something is false you can use:

if randombool == False

but you can also use:

if not randombool:

boolean in an if statement

First off, the facts:

if (booleanValue)

Will satisfy the if statement for any truthy value of booleanValue including true, any non-zero number, any non-empty string value, any object or array reference, etc...

On the other hand:

if (booleanValue === true)

This will only satisfy the if condition if booleanValue is exactly equal to true. No other truthy value will satisfy it.

On the other hand if you do this:

if (someVar == true)

Then, what Javascript will do is type coerce true to match the type of someVar and then compare the two variables. There are lots of situations where this is likely not what one would intend. Because of this, in most cases you want to avoid == because there's a fairly long set of rules on how Javascript will type coerce two things to be the same type and unless you understand all those rules and can anticipate everything that the JS interpreter might do when given two different types (which most JS developers cannot), you probably want to avoid == entirely.

As an example of how confusing it can be:

var x;
x = 0;console.log(x == true); // false, as expectedconsole.log(x == false); // true as expected
x = 1;console.log(x == true); // true, as expectedconsole.log(x == false); // false as expected
x = 2;console.log(x == true); // false, ??console.log(x == false); // false

If statement that uses 3 boolean values

Least you can get is

if(boolean1 && boolean2 && boolean3 &&..){

}

Because since they are already booleans you need not to check for their value. Afaik, no other simple way.

If you have toooooooo many, create an array and write a simple util method to check if any is false



Related Topics



Leave a reply



Submit