Swift short-circuiting with logical operators not working as expected

swift ternary conditional operator
ternary operator swift stack overflow
swift custom operator
nil coalescing operator swift
swift operator
swift operator overloading
what does mean in swift
var anArray = [3, 2, 1]
func sortAndCheck(array: inout [Int]) -> Bool{
    return array.contains(3)

if anArray.contains(3){
    print(anArray) // Prints [3, 2, 1]

if anArray.contains(3) && sortAndCheck(array: &anArray){
    print(anArray) // Prints [1, 2, 3]

For the second if statement, since anArray.contains(3) is already true, why does sortAndCheck(array: &anArray) still get evaluated and sort anArray?

Short circuiting means that the next part of the expression is not evaluated only if the result is already clear. If the part before && is true then the result can still be both false and true and the next part has to be evaluated.

The cases are:

1. true && true => true
2. true && false => false
3. false && false => false
4. false && true => false

And after evaluating the left operand we have:

true && ??

which can end either in case 1 or 2, which have different results.

On the other hand, if we had:

false && ??

Then the result would be either case 3 or 4, which are both false and the expression will short-circuit.

Basic Operators, The assignment operator ( = ) doesn't return a value, to prevent it from being mistakenly used when the equal to operator ( == ) is intended. operator in C and Objective-C, the assignment operator in Swift does not itself return a value. Like the Logical AND operator above, the Logical OR operator uses short-circuit​  * JS will not evaluate the right-hand side operand. 'falsey' and 'truthy' When working with 'non-boolean' operands, logical operators will treat them as 'falsey' or 'truthy.' For example, "" is 'falsey,' as is 0. "a" or 1 are 'truthy.' In essence, JS treats 'empty-ish' or null-ish things as 'falsey.' Combining this with the short circuiting:

The result of an AND (&&) boolean operation can only be true if both sides of the && operator are true, so even if the first operand is true, the second one still needs to be evaluated.

Short-circuiting can only happen if you are using a OR operator (||) and its first operand evaluates to true (resulting in the whole expression evaluating to true) or if you are using an AND operator and its first operand evaluates to false (resulting in the whole expression evaluating to false).

IOS 11 Programming Fundamentals with Swift: Swift, Xcode, and , Swift, Xcode, and Cocoa Basics Matt Neuburg 84 string, 91 where instance expected, 194 live issues, 405 LLDB, 417 loading a nib, 347,356 410 C structs, 411 logical operators, 83 looping, 252,264 shortcircuiting, 269 lowercased, 94  Questions: I read the C# Language Specification on the Conditional logical operators || and &&, also known as the short-circuiting logical operators. To me it seemed unclear if these existed for nullable booleans, i.e. the operand type Nullable<bool> (also written bool?), so I tried it with non-dynamic typing: bool a = true; bool? b =

since anArray.contains(3) is already true, why does sortAndCheck(array: &anArray) still get evaluated

Because X does not imply X && Y. If the expression used || instead, then you'd be right, and the second part wouldn't need to be evaluated. But both operands in an AND expression has to be true for the expression to be true.

IOS 9 Programming Fundamentals with Swift: Swift, Xcode, and Cocoa , Swift, Xcode, and Cocoa Basics Matt Neuburg isEqual:, 457 Issue navigator, 291 issues, live, 381 iteration, 235 iTunes Connect, 431 Binary With Libraries build phase, 303 linking, 321 literals where instance expected, 387 logical operators, 77 looping, 235, 248–253 looping, shortcircuiting, 253 lowercaseString, 86  It doesn't matter whether y is less than 20 or not, it won't affect the outcome because an or statement is always true if either of the terms is true. Python guarantees not to evaluate the second term if the first term is true. Short circuiting with the and operator. Short circuiting with the and operator works in a similar way.

Since you are using && operator in if statement, sortAndCheck(array: &anArray) function should be executed.

If you use || operator in if statement, sortAndCheck(array: &anArray) won't be executed when anArray.contains(3) is true.

Migrating to Swift from Flash and ActionScript, front of numeric values and variables and do exactly what you would expect them to: Despite being initially part of the language, they have been voted out of Swift 3, in order to keep code simple and easy to read. Following are the three logical operators you can use in Swift: • Logical NOT: Here is a brief reminder of  The &, ^, and | operators are called the logical operators. It then goes on to further break down the built-in logical operators into integer, enumeration, Boolean and nullable-Boolean logical operators. There are also user-defined logical operators; see the spec for details. In section 7.12 we have

The Electrical World, That this claim is not unsupported is attested by the indorsement the system has already demands of the present and the very greatly enlarged output expected in the future. to electrical distribution and which is technically known as a ' short circuit. This new field for cabinet work is well occupied by the Charles N. Swift  Basic Operators¶. An operator is a special symbol or phrase that you use to check, change, or combine values. For example, the addition operator (+) adds two numbers, as in let i = 1 + 2, and the logical AND operator (&&) combines two Boolean values, as in if enteredDoorCode && passedRetinaScan.

&&(_:_:), Performs a logical AND operation on two Boolean values. Swift Standard Library This operator uses short-circuit evaluation: The left-hand side ( lhs ) is lhs evaluates to false and rhs is not evaluated, preventing a divide-by-zero error in  This operator does not have an existing meaning in Swift, and so it is given its own custom meaning below in the specific context of working with Vector2D instances. For the purposes of this example, +++ is treated as a new “prefix doubling” operator.

LINQ to object short circuit evaluation not working, I was wondering if the short circuit evaluation for logical operators is working with LINQ to Objects or not as I am having the below code and the  Because it's not the expected use of the AND operator it's hard to find a page explaining how short-circuiting can create if-like behavior. Using this behavior can appeal to developers because of its brevity and cleverness, but it makes the code more difficult to work with. Use an if statement for conditional execution.

  • With short-circuit execution, your code is equivalent to if anArray.contains(3) { if sortAndCheck(array: &anArray) { print(anArray) } }. Since anArray.contains(3) evaluates to true, the return value of sortAndCheck(array: &anArray) will be evaluated. Assuming that true && x is always true would be a mistake.
  • && can be short-circuited iff left side is false
  • Shortcut executing takes place in a third case: Optional chaining
  • @Carpsen90 you're right, but that's unrelated to boolean operators, which this question was about