Search code examples
iosobjective-cshort-circuiting

What order are conditions evaluated in short circuit evaluation


So I've got 3 conditions for an if-statements, and I want to ensure the first two are true before checking the third one. Here's some pseudo code demonstrating what I mean:

if (A && B && C) {
   doSomething
}

Is that equivalent to:

if (A && B) {
   if (C) {
      doSomething
   }
}

Or can we not rely on the ordering of the conditions in a short circuit evaluation?

Thanks in advance!


Solution

  • Yes, short circuiting respects the order and will only evaluate until it finds a failure (hence taking a short cut when it knows it can't possibly succeed any more no matter how any future check might evaluate).