tags:

views:

262

answers:

4
if( (A) && (B) )
{
  //do something
}
else
  //do something else

The question is, would the statement immediately break to else if A was FALSE. Would B even get evaluated?

I ask this in the case that B checking the validity of an array index say array[0] when the array is actually empty and has zero elements. Therefore throwing a segfault because we are trying to access something that is out of bounds of the array. Specifically

if( (array.GetElements() > 0) && (array[0]))
  array[0]->doSomething();
else
  //do nothing and return

This may be dangerous if array[0] actually gets evaluated because it segfaults without the first check to the left of the '&&'. Precedence tells me that the left side will definitely take precedence but it doesn't tell me that it won't evaluate the right side if the left is FALSE.

+24  A: 

In C and C++, the && and || operators "short-circuit". That means that they only evaluate a parameter if required. If the first parameter to && is false, or the first to || is true, the rest will not be evaluated.

The code you posted is safe, though I question why you'd include an empty else block.

John Millikin
Yes my comment was incorrect, changed to //do nothing and return which is more accurate in this case.
BuckFilledPlatypus
Kristo
@Kristo, I did not know that! However, I don't think there are any sane cases to overload that operator. Overloading the bool cast operator would be more useful I think.
strager
John Feminella
A: 

yes, if( (A) && (B) ) will fail on the first clause, if (A) evaluates false.

this applies to any language btw, not just C derivatives. For threaded and parallel processing this is a different story ;)

david bowies labyrinth crotch
Not every language. Some languages, such as Visual Basic, have logical operators which do *not* short circuit.
John Millikin
I would like to point out that this has not been the case with VB for a long time, before the "OrElse" and "AndAlso" were introduce. Even now, the "Or" and "And" do not short circuit. The reason for this is that previously the logical and bitwise "Or" and "And" operators were the same.
DevByDefault
Some languages, such as Ada, have both options available ("AND"/"AND THEN" and "OR"/"OR ELSE").
Greg Hewgill
That means that VB is brain-damaged (go figure). Are we trying to appease the R->L language people by not caring about this?
xcramps
Pavel Minaev
@xcramps - short circuit booleans are convenient most of the time, but they are a language design CHOICE. There is nothing that makes them inherently a better choice in every situation. Pretending they are the only way to do things is silly and misleading. They are just the most common way to do things.
Michael Kohne
david bowies labyrinth crotch
strager
Pavel Minaev
MSalters
+9  A: 

You are asking about the && operator, not the if statement.

&& short-circuits, meaning that if while working it meets a condition which results in only one answer, it will stop working and use that answer.

So, 0 && x will execute 0, then terminate because there is no way for the expression to evaluate non-zero regardless of what is the second parameter to &&.

strager
+3  A: 

Yes, it is called Short-circuit Evaluation.

If the validity of the boolean statement can be assured after part of the statement, the rest is not evaluated.

This is very important when some of the statements have side-effects.

ghills