+ 3
Hi,
a = true;
b = false;
! (a&&b) = true
This is because the && operator needs both operands to be true to return true.
a is true and b is false, so when ANDed together, false is returned, however because !false = true , true is returned.
In your example false is returned from the statements and then !false = true, so true is returned
The actual statements are not reversed just the opposite boolean value is returned
+ 3
Just knowing that :
- && needs both operands to be true to evaluate to true overall otherwise it is false
- | | the or operator needs at least one operand to be true so it can be true overall otherwise it is false
- the ! operator flips the overall boolean value so if it was overall true it is now false and if it was overall false then it is now true
+ 3
Glad I could help a bit, hopefully it becomes more clearer with practice! Good Luck
+ 2
Haha not at all, I aim to help until it is understood.
Let's say we have:
boolean t = true;
boolean f = false;
if (t) // same as saying if t == true
{
print 'trueee'
}
else
{
print 'falseee'
}
Here 'trueee' will be printed as t is true. The else will be ignored because t is not false.
More complex with some repetition:
An && operator will return true if both operands on either side of it are true else it will return false:
1 if ( t && t) // this will return true
print 'both true so I am printed'
2 if ( t && f ) // this will return false
print 'one is false so I will not be printed'
Only statement 1 is printed as they are both true so the overall value is true
Statement 2 is not printed because one value is false so overall it is false
Adding the ! operator now:
Do you know how I said statement 2 would not be printed?
if we change it to this instead by just adding a ! :
if (!( t && f ))
This now returns true and the statement will be printed because the false is swapped for the opposite which is true.
If we do the same for statement 1 :
if (!( t && t ))
This will now return false and the statement won't be printed anymore.
You have:
int childone = 18;
int childtwo = 14;
if (! (childone > 17 && childtwo > 17))
childone > 17 == true because 18 > 17
childtwo > 17 == false because 14 < 17
Thus you have:
true && false which is overall false and
(! ( true && false )) which is overall true thanks to the !
So the actual statements are not changed just the boolean values which are evaluated and returned change.
Hopefully this has helped and if you have anymore questions please ask away.
+ 2
When using the ! operator, true will always be swapped for false and false will always be swapped for true in general.
And yes you are correct about how the computer will see it as overall being true