Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Swift 2.0 Collections and Control Flow Control Flow With Conditional Statements Recap: Control Flow with Conditional Statements

Wang Lei
Wang Lei
2,766 Points

what's the meaning of short circuit evaluation?

what's the meaning of short circuit evaluation? can you give me an example?

1 Answer

Simon Coates
Simon Coates
28,694 Points

I'm going to guess. In some languages, they only evaluate expressions if required. so if the condition is (x && y), if x is false, it doesn't evaluate y. If the conditional is (x || y) then if x is true, then evaluating y is bypassed. It can lead to conditionals like if(myobject==null || myobject.method()). THe second half is only tested if the first part is false. In this case, you're relying on it not testing when true, since it would result in an exception (calling a method on a null). So the short of it is, it only evaluates what it needs to, and there are some situations where this is useful knowledge. It's discussed at https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/BasicOperators.html . Just search for "circuit"