Does the NOT operator if(!value) work the same as in boolean algebra / propositional logic?

33 views Asked by At

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.

1

There are 1 answers

0
Santhosh On

Logical NOT:

The ‘!’ operator returns true the when condition in consideration is not satisfied. Otherwise it returns false.

For example,

 !A returns true if A is false, i.e. when A=0.

It always reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false.