Whizz Mirray Whizz Mirray - 1 month ago 7
C Question

How to check if all values in bool array are true?

In C,
what is the best and simplest way to check if all values in a bool table are true?
I tried something like this but it doesn't work

for(i = 0; i < value; i++){
if(bool_table[i] == 0)
table_true = 0;
else
table_true = 1;
}


The problem with this code is that sometimes if the first value is true then it will set
table_true = 1

Answer

If the following loop goes through the entire array without finding a false entry, then at the end of the loop i will equal value

for(i = 0; i < value; i++)
    if ( !bool_table[i] )
        break;

table_true = (i == value);