This question is about understanding how a language interprets if statements.
I know that if(a)
checks for truthfullness of a
.
However, when we use the NOT operator like such:
if (!a) {
...
}
Does it work the same as the NOT operator in boolean algebra, i.e.
----------
| a | !a |
----------
| T | F |
| F | T |
----------
and still check for truthfulness, or does it change the success condition to now be false?
This is inconsequential in all cases, as it will result in the same thing happening regardless of implementation, but I feel it would help me write better if statements / understand them better.
For example, if we look at the following if statement:
if (!(a && b))
and a truth table:
----------------------------
| a | b | a & b | !(a & b) |
----------------------------
| T | T | T | F |
| T | F | F | T |
| F | T | F | T |
| F | F | F | T |
----------------------------
We can see that it's a little more complex - so knowing if I can always rely on a truth table to ensure proper functionality of my if statement would make me much less confused.
To try to guess, I imagine the success condition is always true
, and that the NOT operator works exactly the same as the boolean algebras NOT operator by merely inverting the current value.
Is this correct?
Thanks.
Logical NOT:
The ‘!’ operator returns true the when condition in consideration is not satisfied. Otherwise it returns false.
For example,
It always reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false.