Negation is the process of reversing the meaning of a Boolean expression. There are two approaches used to negate a Boolean expression.

First Approach

The first approach is the easiest one. Just use a NOT operator in front of the original Boolean expression and your negated Boolean expression is ready! For example,

PHP

if the original Boolean expression is $x > 5 && $y == 3,

the negated Boolean expression becomes !($x > 5 && $y == 3).

Notice: Please note that the entire expression must be enclosed in parentheses. It would be completely incorrect if you had written the expression without parentheses, as !$x > 5 && $y == 3. In this case the NOT operator would negate only the first Boolean expression, $x > 5.

Java, C++, C#

if the original Boolean expression is x > 5 && y == 3,

the negated Boolean expression becomes !(x > 5 && y == 3).

Notice: Please note that the entire expression must be enclosed in parentheses. It would be completely incorrect if you had written the expression without parentheses, as !x > 5 && y == 3. In this case the NOT operator would negate only the first Boolean expression, x > 5.

Visual Basic

if the original Boolean expression is x > 5 And y = 3,

the negated Boolean expression becomes Not (x > 5 And y = 3).

Notice: Please note that the entire expression must be enclosed in parentheses. It would be completely incorrect if you had written the expression without parentheses, as Not x > 5 And y = 3. In this case the NOT operator would negate only the first Boolean expression, x > 5.

Python

if the original Boolean expression is x > 5 and y == 3,

the negated Boolean expression becomes not (x > 5 and y == 3).

Notice: Please note that the entire expression must be enclosed in parentheses. It would be completely incorrect if you had written the expression without parentheses, as not x > 5 and y == 3. In this case the NOT operator would negate only the first Boolean expression, x > 5.

Second Approach

The second approach is a little bit more complex but not difficult to learn. All you must do is negate every operator according to the following table.

PHP

Original Operator

Negated Operator

==

!=

!=

==

>

<=

<

>=

<=

>

>=

<

&&

||

||

&&

!

!

Notice: Please note that the NOT operator ( ! ) remains intact.

For example,

if the original Boolean expression is $x > 5 && $y == 3,

the negated Boolean expression becomes $x <= 5 || $y != 3.

Java, C++, C#

Original Operator

Negated Operator

==

!=

!=

==

>

<=

<

>=

<=

>

>=

<

&&

||

||

&&

!

!

Notice: Please note that the NOT operator ( ! ) remains intact.

For example,

if the original Boolean expression is x > 5 && y == 3,

the negated Boolean expression becomes x <= 5 || y != 3.

Visual Basic

Original Operator

Negated Operator

=

<>

<>

=

>

<=

<

>=

<=

>

>=

<

And

Or

Or

And

Not

Not

Notice: Please note that the Not operator remains intact.

For example,

if the original Boolean expression is x > 5 And y = 3,

the negated Boolean expression becomes x <= 5 And y <> 3.

Python

Original Operator

Negated Operator

==

!=

!=

==

>

<=

<

>=

<=

>

>=

<

in

not in

not in

 in

and

or

or

and

not

not

Notice: Please note that the not operator remains intact.

For example,

if the original Boolean expression is x > 5 and y == 3,

the negated Boolean expression becomes x <= 5 or y != 3.

Leave a Reply

Your email address will not be published. Required fields are marked *

This article is taken from a book of mine. If you enjoyed it, please visit my author's page inand check out all of my books!