# need clarification with ! (not) function

Hi. I want to say the following:
if x ! (<70 && >5)
digitalWrite (LED, HIGH);

in words: I want LED to light if x is not between 5 and 70. I do need to use the “not” function because if I use a function like if x >70 || x <5 I do not get the LED to light if x = nan (like my temperature sensor got disconnected)

Thanks much

Test for NaN separately

karotto:
if x >70 && x <5 I do not get the LED to light if x = nan (like my temperature sensor got disconnected)

Thanks much

I doubt x simultaneously will be higher than 70 && (and) lower than 5. Try || (or) instead of &&.

karotto:
Hi. I want to say the following:
if x ! (<70 && >5)
digitalWrite (LED, HIGH);

in words: I want LED to light if x is not between 5 and 70. I do need to use the “not” function because if I use a function like if x >70 || x <5 I do not get the LED to light if x = nan (like my temperature sensor got disconnected)

Thanks much

The entire expression must be within the parenthesis and your syntax is incorrect. But your premise is not entirely correct because every such expression has a complement that can be expressed without the negation:

``````if (not (x <70 && x>5) )
``````

can be:

``````if (x >= 70 || x <= 5)
``````

How a failed reading is reported depends on the sensor library code.

Also “!” is not a function, it is an operator.

Ok.

C provides >, <, ==, !=, <= and >= to compare numbers.
C provides parenthesis for grouping
C provides !, &&, and || to combine logical operators.
There isn’t a range function like “5 <= x <= 70” to check if x is between 5 and 70.

So, to check if a number is between 5 and 70, you use

``````(x >= 5) && (x <= 70)
``````

To check the converse of this, x isn’t between 5 and 70 (inclusive)

``````! ((x >= 5) && (x <= 70))
``````

Now, you need to know DeMorgan’s laws of boolean algebra. They are (using C language operators)

`! (A && B)` is the same as `!A || !B`, and
`! (A || B)` is the same as `!A && !B`.

Thus

``````! ((x >= 5) && (x <= 70))
``````

Is the same as

``````(!(x >= 5) || !(x <= 70))
``````

But the negation of “greater than or equal to” is “less than”. So this can be rewritten as

``````(x < 5) || (x > 70)
``````

“if x is less than five or greater than seventy, light the LED”.

``````if(x < 5 || x > 70) {
light_the_LED();
}
``````