Reputation: 362
I studied the Conditions material today and got some homework, some of them are pretty easy and some of them are tricky to implement.
However, I got this following code:
boolean bool1, bool2, bool3;
bool1 = false;
bool2 = true;
bool3 = true;
if(bool1 == true || bool2 == true && bool3 == true)
System.out.println("true");
else
System.out.println("false");
I don't understand why this program returning true
while there's a false statement - bool1
is declared as FALSE in the beginning of the program, and when it's checked (bool1 == true
) the if should return false.
Upvotes: 1
Views: 1182
Reputation: 3104
The problem is in the following line:
if(bool1 == true || bool2 == true && bool3 == true)
In this statement you're asking for two conditions:
Bool1 == true
ORBool2 == true && Bool3 == true
.As you might have mentioned ||
means 'OR'.
In you're code you have declared both Bool2
and Bool3
as true
. So the second condition in the if-statement is correct and will cause the block to continue, which in your case will print True
.
What I think you're trying to do is the following:
Bool1
ORBool2
(or both) is/are true. ANDBool3
is true.In that case you'll have to use the following code:
if((bool1 == true || bool2 == true) && bool3 == true)
Upvotes: 1
Reputation: 21965
Let's break this down a bit :
bool1 == true // FALSE : because bool1 has the value of "false"
bool2 == true // TRUE : because bool2 has a value of "true"
bool3 == true // TRUE : because bool2 has a value of "true"
Let's go back to the expression and replace :
(FALSE || TRUE && TRUE)
FALSE || ...
: Since it is a ||
and the first value is false
, we have to check the second value.
TRUE && ...
: Now we're checking the second value, since it is true
and we are using &&
, we have to check the second expression. Since in this case it is true
, the whole expression becomes TRUE
Wiki's Page for Boolean Expressions
Upvotes: 1
Reputation: 107
That's because ||
means 'or' and &&
means 'and'. So basically in your case, if bool1
OR bool2
are true, the condition is correct. Try your code changing the value of bool2
to false
aswell, and you will see that the program will return you a false
statement.
Upvotes: 0
Reputation: 59
The condition returns true because of the OR operator: ||. This operator returns true if at least one of the two expressions next to it is true. In your case, you have true or false, therefore true.
Upvotes: 0
Reputation: 17132
if(bool1 == true || bool2 == true && bool3 == true)
&&
is prior to ||
, so bool2 == true && bool3 == true
is evaluated first (== true
). The condition becomes:
if(bool1 == true || true)
which evaluates to true
also. Hence you get "true" as output.
Upvotes: 0
Reputation: 281
|| stand for OR. If the left or the right side of OR is TRUE then the result is TRUE . The right side is TRUE and this is why you get the result TRUE.
Upvotes: 0
Reputation:
if(bool1 == true || bool2 == true && bool3 == true)
System.out.println("true");
The above if statement says, if either bool1 or bool2 is true, and if bool3 is true print me "true"
Read more about || operator.
The above operator means, atleast one should be true out of two expressions.
&&, all expressions must be true. ||, atleast one is enough.
Upvotes: 0