If with 3 conditions and at least 2 true?

3

Suppose I have the following if

if(a || b || c == true)

In this case, if only one of the values is true to activate the condition, but I would like the condition to be activated only when at least 2 of the past values are true, is it possible? If so, how?

    
asked by anonymous 02.03.2018 / 21:33

3 answers

5

Gambiarra based on type juggling :

if(a + b + c >= 2) {

}

Explanation: With the addition operator, Boolean values will be converted to numbers, with true being 1 and false being 0 . If the result of the sum is 2 or more, it means that it has at least 2 true . This works as long as your variables are of type Boolean, or of type Number with values 0 or 1 . If necessary, convert them before if .

    
02.03.2018 / 21:41
3

I do not know if there is an "automatic" way to control this, but you can group conditions:

if ((a && b) || (a && c) || (b && c))
    
02.03.2018 / 21:35
1

To use two conditions, you must use & & instead of ||

|| means OU

& amp; means E

if (a & b || c)

If A and B are true OR C

That is, if C is true, it was already. Passed.

    
02.03.2018 / 21:36