# If conditional statement

I want to write an if statement that if any one single condition becomes true at a given moment, the function will run. How would i format the conditions(4) into an if statement, could i just seperate them by commas like this ?

if(X > 100, X < 100, Y > 100, Y < 100)
{
//do this if any single condition is true
}

or what is the proper way to format this?

could i just seperate them by commas like this ?

No. You need to use the logical OR operator, ||, like so:

``````if(X > 100 || X < 100 || Y > 100 || Y < 100)
``````

However, that statement does not make sense. Hopefully, your real statement will.

Be aware that if you have (say) 3 tests in one IF statement

eg if ( A > 6 || B < 7 || C > 23) {

there are 2**3 = 8 possible outcomes options (or are there more?). It is very easy to make a mistake and test for the wrong outcome option.

…R

Edited in response to Reply #4

It’s also wise to remember that even the OR operator has it’s own rank within the order of precedence. So, rather than relying on memory to work out which functions have the greatest precedence, I’d always wrap each condition in brackets.

like so

``````if( (X > 100) || (X < 100) || (Y > 100) || (Y < 100) )
``````

Robin2:
Be aware that if you have (say) 3 tests in one IF statement

eg if ( A > 6 || B < 7 || C > 23) {

there are 2**3 = 8 possible outcomes (or are there more?). It is very easy to make a mistake and test for the wrong outcome.

No. There are only two possible outcomes, either the statement is true or it is false. There is one way to reach a false outcome, when all the tests are false.
There are three ways to reach a true outcome, when any one of the tests is true.
If more than one test is true, it won’t change the outcome. They are not exclusive ORs.
There are 8 possible results of the 3 tests.

joelaflop:
I want to write an if statement that if any one single condition becomes true at a given moment, the function will run. How would i format the conditions(4) into an if statement, could i just seperate them by commas like this ?

if(X > 100, X < 100, Y > 100, Y < 100)
{
//do this if any single condition is true
}

or what is the proper way to format this?

Those commas won’t compile much less work.
You need to spend time learning Boolean Operators and Compound Operators, for a start:
Booleans are on the left side about halfway down, Compounds are left bottom.
http://arduino.cc/en/Reference/HomePage

(( X < 100 ) || ( X > 100 )) is the same as ( X != 100 ), != is the opposite of ==, ! means NOT.

I want to write an if statement that if any one single condition becomes true at a given moment, the function will run. How would i format the conditions(4) into an if statement,…

Ohhh … wish you wouldn’t have asked that!

Four mutually exclusive conditions in one if statement:

``````void setup() {
Serial.begin(115200);

int x = 100;
int y = 100;

if (((x > 100) && ! (x < 100) && ! (y > 100) && ! (y < 100)) ||
((x < 100) && ! (x > 100) && ! (y > 100) && ! (y < 100)) ||
((y > 100) && ! (x < 100) && ! (x > 100) && ! (y < 100)) ||
((y < 100) && ! (x < 100) && ! (y > 100) && ! (x > 100)))
{
Serial.print("One single condition is true");
}
else
{
Serial.print("All conditions are false or multiple conditions are true");
}
}

void loop() {
}
``````

Logic diagram:

Henry_Best:
There are 8 possible results of the 3 tests.

That is what I was trying to convey.

I will amend my post to say "options" rather than "outcomes"

...R