#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# dealing with a boolean array (how to execute when all bool elements of the array is true?)

edited August 2015

I'm having a problem writing my code. I wanted to execute a statement when more than one expression is true

``````boolean checking(){
boolean myVar = false;
boolean[] each = new boolean [8];
for (int i = 0; i < 8; i++){
if(i > 0){   //just a random expression
each[i] = true;
}
}
if(each[0] && each[1] && each[2] && each[3] && each[4] && each[5] && each[6] && each[7]){ //i need a better way to write that since the elements aren't always going to be 8 elements
myVar = true;
}
return myVar;
}
``````

this is a boolean function example (not the whole code)

Tagged:

• edited August 2015 Answer ✓
``````static final boolean checkAllTrue(boolean... bools) {
for (boolean b : bools)  if (!b)  return false;
return true;
}
``````
• thank you! it worked but i didn't get it, can't understand what `:` means in the `for` loop i guess i'll have to look that up, thanks!

• thank you, and the way you've put `return false;` if an element is false solves the problem i had in the beginning so thanks very much!

• edited August 2015 Answer ✓

Just to make things clearer between `for ( : )` & `for ( ;; )` loops, check it out checkAllTrue() w/ those 2 approaches together for easier comparison: :D

``````static final boolean checkAllTrue(boolean... bools) {
for (boolean b : bools)  if (!b)  return false;
return true;
}

static final boolean checkAllTrue(boolean... bools) {
for (int i = 0; i < bools.length; ++i) {
boolean b = bools[i];
if (!b)  return false;
}
return true;
}
``````
• yeah I found out yesterday, it wasn't as hard as I thought. Thanks though :)