Not ( ! )
What is it?
The NOT operator negates, or inverts the value of, whatever logical statement it is attached to. For any given condition, if expression A equates to True, expression !A equates to False.
In the figure below, any condition that produces a value of True from statement A will produce a value of False when you apply negation to the statement. The conditions which produce True are represented by green shading in both cases.

What is the Syntax?
The NOT operator’s syntax is demonstrated in the following example, where we evaluate one expression, then negate the expression and evaluate it again.
2 > 4 = False
!( 2 > 4 ) = True
This is the programming equivalent of saying “2 is NOT greater than 4”, a statement that we know to be correct.
Why might I want to use this?
You might find it difficult to exclude search results with one specific criteria if you were only allowed to use the AND and OR operators we discussed in the previous sections.
Going back to our example of someone searching for properties on a website, let’s say the person searching has decided that they no longer want to view houses only, but instead want to see listings for every property type except bungalow.
In our previous expression we used the following expression, containing AND and OR statements:
( PropertyType = "House" ) & ( ( PostCode = "X12 3QZ" ) | ( PostCode = "X14 7BF" ) );
Let’s say there are six types of property listed on the website. If we were limited to AND and OR statements, we would have to write something like this in order to exclude bungalows from the search results:
( ( PropertyType = "House" ) | ( PropertyType = "Apartment" ) | ( PropertyType = "Land" ) | ( PropertyType = "Commercial Property" ) | ( PropertyType = "Other" ) ) | ( ( PostCode = "X12 3QZ" ) | ( PostCode = "X14 7BF" ) );
This is clearly not the best way to write the expression, because every time the website adds a new property type we would need to modify our expression to accommodate the addition. The more property types, the longer and more cumbersome the expression; here instead is a better example using a NOT operator:
!( PropertyType = "Bungalow" ) & ( ( PostCode = "X12 3QZ" ) | ( PostCode = "X14 7BF" ) );
In this example you can see that by negating the expression PropertyType = “Bungalow”, we are saying that so long as prospective properties are in the postcodes we specify and are NOT bungalows, we want to see them as part of our search results.
<< Previous: Or ( | ) | Next: Logical Operator Precedence >>