Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The || (Logical OR) operator is a binary infix operator that evaluates two Boolean expressions, returning true if at least one of the operands evaluates to true, and false only if both operands evaluate to false.
let lhsExpression = false
let rhsExpression = true
let result = lhsExpression || rhsExpression // true

Evaluation Mechanics

Short-Circuit Evaluation and @autoclosure Swift employs short-circuit evaluation for the || operator to optimize execution and prevent unnecessary computation. This is implemented at the language level by wrapping the right-hand side operand in an @autoclosure. The evaluation strictly follows a left-to-right order:
  1. The left-hand side is evaluated first.
  2. If the left-hand side evaluates to true, the overall expression immediately resolves to true. Because the right-hand side is an @autoclosure, its execution is deferred and ultimately bypassed.
  3. If the left-hand side evaluates to false, the right-hand side closure is executed to determine the final Boolean result.
Truth Table
LHSRHSResultRHS Evaluated?
truetruetrueNo
truefalsetrueNo
falsetruetrueYes
falsefalsefalseYes

Operator Characteristics

  • Type Requirement: The left-hand operand must evaluate to a Bool, while the right-hand operand is defined in the standard library as an @autoclosure () throws -> Bool. Swift is strictly typed and does not support implicit truthiness or type coercion (e.g., non-zero integers or non-nil objects cannot be evaluated using ||).
  • Associativity: Left-associative. Chained operations are grouped and evaluated from left to right. An expression like a || b || c is parsed as (a || b) || c.
  • Precedence: The || operator belongs to the LogicalDisjunctionPrecedence group. It has a lower precedence than the && (Logical AND) operator, which belongs to the LogicalConjunctionPrecedence group. In mixed expressions without explicit parentheses, && is evaluated before ||.
let exprA = false
let exprB = true
let exprC = false

// Precedence visualization: && evaluates before ||
let mixedResult = exprA || exprB && exprC // false

// The compiler parses the above as:
let explicitResult = exprA || (exprB && exprC) // false
Master Swift with Deep Grasping Methodology!Learn More