Shorthand test of Array elements

Is there a shorthand way to test all elements of an array being false (or true)? if each entry is binary, say for a list of switch states, is there a way to test "if all elements in the array are false, then..."?

The way I can do it at the moment is like so:

if(list[0] && list[1] && list[2] && list[3] && ...etc) {
print("boo!");
}

I guess that isn't too much typing, but always like to learn shortcuts :slight_smile:

a way to write list[any] and list[all] would be great

The way I can do it at the moment is like so:

if(list[0], list[1], list[2], list[3]...etc) {
print("boo!");

That won't work.

I'd ditch bools, and use single bit flags packed into bytes and test the bytes for zero/non-zero.

Or loop over them:

bool allTrue = true;
for(byte i = 0; i < Max; i++){
  allTrue &= list[i];
}

if(allTrue){
  Serial.print("Boo");
}

Groove:
That won’t work.

I’d ditch bools, and use single bit flags packed into bytes and test the bytes for zero/non-zero.

Sorry - I updated the post, I must have edited it as you were posting the reply - I meant to include && of course.

septillion:
Or loop over them:

bool allTrue = true;

for(byte i = 0; i < Max; i++){
  allTrue &= list[i];
}

if(allTrue){
  Serial.print(“Boo”);
}

great thanks!!

Aidan

allTrue &= list[i];

Surprised if that works.

boolrules:

allTrue &= list[i];

Surprised if that works.

Then you should be suprised now.

What made you doubt it would work?

Whandall:
Then you should be suprised now.

What made you doubt it would work?

I couldn’t get it to work (buttons are normally closed, but using pullup resistors):

allOff = false;
for(int i = 0; i < 6; i++) {
buttonState = digitalRead(buttonPin*);*
if(buttonState && !buttonLatch*) {*
_ buttonLatch = true;
* MP3player.stopTrack();
MP3player.playTrack(i);
}_

else if(!buttonState _&& buttonLatch) {
buttonLatch = false;
}
}
for(byte i = 0; i < 6; i++) {
allOff &= buttonState;
}*_

I couldn’t get it to work

Because the compiler hates italics.

I couldn't get it to work

If the code you incorrectly posted a snippet of compiled, it worked. You can be 100% certain of that.

That it did not do what you expect means only that your expectations are wrong. You need to describe what your expectations are, post ALL of your code IN CODE TAGS, and post a schematic, if you want us to help you re-align your expectations.

Yep, please use code tags.

And next, why loop twice?

And if you want to flip the logic you need to change the operator as well :wink: Because if you want to know is all are false (which is NOT all are off :wink: ) then a AND isn't going to give you that. Because false AND true = false. So even if not all are false you get false :wink: Use a OR :smiley:

Or, flip the logic while reading the buttons. If you know a read of LOW is a pressed switch you can just map that to true, simply do a !digitalRead() :smiley:

septillion:
Yep, please use code tags.

And next, why loop twice?

Yeah I see that it is pointless, thanks!

septillion:
And if you want to flip the logic you need to change the operator as well :wink: Because if you want to know is all are false (which is NOT all are off :wink: ) then a AND isn't going to give you that. Because false AND true = false. So even if not all are false you get false :wink: Use a OR :smiley:

Or, flip the logic while reading the buttons. If you know a read of LOW is a pressed switch you can just map that to true, simply do a !digitalRead() :smiley:

Ah yep derp! That makes total sense thanks :slight_smile: I never thought about using !digitalRead() as well thanks!