# If Statement- Check if multiple variables equal to the same number.

Hi there,
All I need to do is to check for all my sensors on my project at the setup.

But i was wondering if there is a simplest way to do that, because I don't want to waste the processor's memory, because I use a lot of sensors.

What I am doing now:

``````   if (stepper1limit1ok==1 && stepper1limit2ok==1) {
if (ir1ok==1 && ir1ok==1) {
sensorsok=1;
}
}
``````

I cannot tell from this code snippet whether you are wasting memory or not. PLEASE post your entire code.
Certainly, having "ir1ok==1" show up twice in the if-statement looks peculiar.
When you start naming things a1, a2, etc. it is usually suggested that you learn about arrays. This is unlikely to save you memory when it comes to just a couple of things, but could save a lot of memory if you are talking about, say, five things or more.

there is no sneaky way of checking whether several variables are equal to the same number. You need to check that each of them is equal to that number. Or, that one of them is equal to that number, and the other variables are equal to the first one. If you have 4 independent variables, you are going to have to make 4 comparisons.

Examples:

``````int a,b,c,d ;
/*  Give a,b,c and d a value somehow */

if ( a == 1 && b == 1 && c==1 && d == 1 )  { /* do something */ }
else { /* do something else */ }

if ( a == 1 && b == a && c == a && d == a ) {} else {}  /*  still 4 comparisons */

if ( (a-b)==0 && (c-d)==0 && (a==c) && (a==1) ) {} else {}  /* still 4 comparisons */

/* Unless your are trying to obfuscate,  the simplest is usually the best */
``````

Depending on the values of the variables you may be able to do this

``````if (stepper1limit1ok + stepper1limit2ok + ir1ok == 3 )
{
//do stuff
}
``````

From the names of the variables I would guess that the values derive from switches and will only be HIGH or LOW so the addition trick can work.

If you want to check that ALL the variables have a value of 1 or 0 you could add the values together. Then (if they should ALL be 0) you can check if the total is 0.

Another trick is to assign powers of 2 values to each variable when it is ON 1,2,4 8 16 etc. and 0 when they are OFF

Then the sum of the variables will be unique for every combination of the variables. In effect each value represents a bit position.

...R