Quick question about using if statements with large arrays

Hi all,

Let's say I wanted to check to see if every single member of an array (size 100) is or is not 0. The not so refined way of doing so would be to open an if statement and reference every single member, one by one, and check to see if they all equal 0.

It would be quite the hassle to write down all 100 reference, not to mention the fact that this method probably is very error prone.

Is there a "better" way to do this comparison?

Thank you.

You could add some logic that sets a boolean flag 'true' any time a non-zero value is written to the array. Then the if just becomes:

if( bNonZeroFlag )
{
...
}//if

you want to make sure that every member is 0? or is not 0? or an output that contains information on which are 0 and which are non-zero?

for the former two:

byte i=0;
while(array[i++] && i<100);
if (i==100){
Serial.println("all are non-zero");
}

or

byte i=0;
while((!array[i++]) && i<100);
if (i==100){
Serial.println("all are zero");
}

The "for" statement makes this easy.

//
// UNTESTED !
//
int myArray[100];
bool foundIt = false;
for (int i=0;i<100;i++) {
  if (myArray[i] == 0) {
    foundIt = true;
    break;
  }
}

There are many variations on this that can also be used to code this problem easily.

Thank you all.

All the responds make perfect sense. Thank you again!