ALI
ALI

Reputation: 339

Alternative of IF and "OR"

In this code (a,b,c,d,e,f,g,h,i,j) are variables):

if ( a>b || c>d || e==f || g<h || i!=j )
{
//Some statement; 
}

If one condition is true among the five, the if will be executed. However, my actual requirement is that if any three or more of these five conditions are true then if should be executed. In the actual code there may be more conditions (10 or more). How can I change the code to set a minimum number of true conditions? I am coding in MATLAB.

Upvotes: 4

Views: 135

Answers (1)

Oleksii Shmalko
Oleksii Shmalko

Reputation: 3778

You may sum up results of your comparisons and check sum against some number. For example:

if ( (a>b) + (c>d) + (e==f) + (g<h) + (i!=j) >= 3 )

Upvotes: 8

Related Questions