# Multiple Conditional Statement using !=

This could be embarrassing : but why doesn't the following conditional evaluate to true if tmp does not equal 0 or 5

`````` if (tmp != 0 || tmp != 5) {      //If tmp is not equal to 0 or 5
//do something to fix
}
``````

this works fine though ?

``````   if (tmp == 0 || tmp == 5) {      // Not  0 or 5 so corrupt
}
else {
//do something to fix
}
``````
``````if (tmp != 0 || tmp != 5) {
``````

If tmp is 0, the first clause is false, so the whole statement is false.
If tmp is 1, the first clause is true, and the second one is also true, so the whole statement is true.
If tmp is 5, the first clause is true, and the second one is false, so the whole statement is false.

What were you expecting?

Thanks for the reply. I was expecting that if tmp did NOT equal 0 OR tmp did NOT equal 5 then the condition would be true. In other words if tmp is anything other than 0 or 5 then do something.

I suspect that you mean that if tmp is not 0 AND tmp is not 5, do something.

Perhaps it should be

``````if (tmp != 0 && tmp != 5)
``````

...R

tmp will always be != 0 OR != 5. If it is 0 it will be != 5. If if is 5 it will be != 0.

If you want the opposite of (tmp == 0 || tmp == 5) try (!(tmp == 0 || tmp == 5)).

PaulS:
I suspect that you mean that if tmp is not 0 AND tmp is not 5, do something.

Hmm, maybe mincing words. I want it to evaluate to TRUE if tmp is neither 0 or 5.

johnwasser:
tmp will always be != 0 OR != 5. If it is 0 it will be != 5. If if is 5 it will be != 0.

If you want the opposite of (tmp == 0 || tmp == 5) try (!(tmp == 0 || tmp == 5)).

Thanks will give it a try.

Hmm, maybe mincing words.

No, it isn't. Instead of OR (||) in your statement, you need AND (&&).

Your statement should be

``````if(tmp != 0 && tmp != 5)
{
// Do this
}
``````

0 is not not equal to 0, so that part is false, and the body is not executed.
1 is not equal to 0 and it is not equal to 5, so the body is executed.
2 is not equal to 0 and it is not equal to 5, so the body is executed.
3 is not equal to 0 and it is not equal to 5, so the body is executed.
4 is not equal to 0 and it is not equal to 5, so the body is executed.
5 is not equal to 0 and it is not not equal to 5, so that part is false, and the body is not executed.

Thanks. Both examples work. Much appreciated

``````if (tmp != 0 && tmp != 5)
{
//Do This
}

//or

if (!(tmp == 0 || tmp == 5))
{
// Do this
}
``````

Bryanpl:
Thanks. Both examples work. Much appreciated

There is a rule of boolean logic which explains why both work:

!A && !B == !(A || B)

That's why logic gate chips are usually all NAND and NOR gates. If you have a bunch of NAND gates (!(A&&B)) you can invert the inputs to make an OR gate.

Bryanpl:
This could be embarrassing : but why doesn't the following conditional evaluate to true if tmp does not equal 0 or 5

`````` if (tmp != 0 || tmp != 5) {      //If tmp is not equal to 0 or 5
``````

//do something to fix
}

``````

this works fine though ?

``````

if (tmp == 0 || tmp == 5) {      // Not  0 or 5 so corrupt
}
else {
//do something to fix
}

Just for a drill - have you tried tmp 1 thru 4 or 6 and up ?

Logic is nice , but sometime the math is gooder.

I did run a 1 and 6 as a drill and both worked as they should. Did stumble upon a number of boolean calculators online. Good stuff for testing.