if statement evaluation order

What order are the operands in an if statement evaluated? Say my code looks as follows:

if (a && b && c) {
… do this;
}

is a evaluated first? I’m making an assumption that the evaluation is halted at the first False variable and the program falls through to the first statement after the if. I want to make my program as fast as possible and if a variable is false 99% of the time and the other variables false only 50% of the time I want the 99% variable evaluated first.

Tom

Tom,

Although I don’t recall if the compiler halts evaluation after the 1st false, one thing you could do is use nested if’s or if-else’s to break up your evalations.

Thanks,

If I ever get time I’ll run some tests.

Tom

Please Disregard my previous post. I dug deeper and got some additional information.

Due to order of operations it will evaluate in the compiler as:

if ( ( a && b ) && c) )

If your compiler supports shortcutting (most don’t, even though it is in the language specs) it will only evaluate a && b, if it results a false, it will exit without checking && c.

Although I was right, for best speed in the event that a is normally false, I would go with a nested if:

if ( a)
{
if ( b )
{
if ( c )
{
}
}
}

However, it’s double edged sword. An if statement adds complexity to the code, so if a and b and c are normally true, then the above nested ifs take more time than your first if statement, you lose clock cycles.

In the end, I would actually go with your initial version. Clean code is always better than fast code. Besides, checking a Boolean value takes very little time. I would be more worried about code that runs loops and such.