Thread: Number of Condition inside if statement

1. Number of Condition inside if statement

I have been assigned to review the below code and update for the changes.
Code:
```   if(condition1>val1 && condition2<=val2 && cond3>val3 &&  cond4<val4 && cond5==val5 && cond6==val6 && cond7==val7 && cond8<val8  && cond9==val9 && cond10==val10 && cond11==val11  && cond12==val12)
{
}```
What is the best method to update the above code, i see lot of conditional checks, i am sure it is not the best practice. It is not only one place.

2. Generally, when you start numbering variables like that, it becomes worthwhile to ask if arrays should be used instead. If arrays are used, then a loop becomes a natural option.

3. ISO 9899 5.2.4.1 § 1 suggests some limits, but not for the number of expressions inside a conditional block of an if. I think 127 (as in "127 parameters in one function call") is a good limit.

BTW: I've seen this limit imposed on MSVC. This expression, separated by &&, can be interpreted as nested ifs, like:
Code:
```if ( condition1 > val1 )
if ( condition2 <= val2 )
if ( ... )
...```
And more than 127 nested ifs (on MSVC) will cause a compilation error.

Try to simplify the expression or to shrink the number of comparisons... There are multiple ways to do it and to choose one depends on what you are tring to do. For example: You could notice that the first 4 tests are RANGE tests, and the last 6 are equality tests, so:

Code:
```_Bool found = 0;

// range tests.
if ( cond1 > val1 && cond2 <= val2 && cond3 > val3 && cond4 <= val4 )
{
// ISO 9899 suggests a limit of 4095 items in that block!
struct {
int cond, val;
} tests[] = { { cond5, val5 }, { cond6, val6 }, { cond7, val7 }, { cond8, val8 },
{ cond9, val9 }, { cond10, val10 }, { cond11, val11 } };

found = 1;

// "Non" equality tests.
for ( int i = 0; found && i < sizeof tests / sizeof tests[0]; i++ )
if ( tests[i].cond != tests[i].val )
found = 0;
}

if ( found )
{ ... }```
This approach has the problem that a copy of the pairs (cond,val) are made to the stack, but avoid the multiple nesting levels.

4. You say it appears in more than one place. Show a couple of the others.

5. Some formatting would help, so you're not scrolling sideways.
Code:
```if(condition1>val1  && condition2<=val2 && cond3>val3 &&
cond4<val4       && cond5==val5      && cond6==val6 &&
cond7==val7      && cond8<val8       && cond9==val9 &&
cond10==val10    && cond11==val11    && cond12==val12)
{
}```