biswaranjan.rath said:
Is it better to have multiple if statements rather having multiple
conditions in single if statement ?
which one is better:
if (cond1 || cond2 && (cond3 || cond4) || cond5)
if(cond1 || cond5)
if(cond2 && (cond3 || cond4))
this is an example, there might be more conditions.
They aren't equivalent, so whichever is right is better.
And-ed conditions at the top level can be replaced by nested ifs, but with a
catch:
if ( cond1 && cond2 )
blah1;
else
blah2;
becomes
if ( cond1 )
if ( cond2 )
blah1;
else
blah2;
else
blah2; /* again */
Not pretty.
Or-ed conditions at top level (as in your example) are equivalent to
if-elses:
if ( cond1 || cond2 )
blah1;
else
blah2;
becomes
if ( cond1 )
blah1;
else if ( cond2 )
blah1; /* again */
else
blah2;
which isn't very pretty either.
On the occasions where you've really got a choice, it shouldn't make a
difference, and any difference it might make isn't going to be predictable.
If it really really matters, all you can do is look at the generated code on
your platform. Even then, you can't necessarily assume that it will be
consistent and infer any principles. For instance, if there are no side
effects or traps to worry about, it might apply the as-if rule and rearrange
the whole thing so as to test what's already to hand before testing what has
to be fetched or calculated.