Logic to test that 3 of 4 are True

I suggest writing the code in a manner that indicates what you mean. If you want 3 values to be true, it seems natural to me that the value 3 appears somewhere.

For instance, in C++:

if ((int)a + (int)b + (int)c + (int)d == 3)
    ...

This is well defined in C++: the standard (ยง4.7/4) indicates that converting bool to int gives the expected values 0 or 1.

In Java and C#, you can use the following construct:

if ((a?1:0) + (b?1:0) + (c?1:0) + (d?1:0) == 3)
    ...

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)