> ## 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.

# Swift If Expression

An `if` expression in Swift is a control flow construct that evaluates a boolean condition and directly yields a value from the executed branch. Introduced in Swift 5.9, it allows `if` constructs to be parsed and evaluated as expressions rather than statements, enabling their direct use in variable assignments, return statements, and property initializers without mutating state or relying on the ternary operator (`?:`).

```swift theme={"dark"}
let variableName = if condition {
    expression1
} else if secondaryCondition {
    expression2
} else {
    expression3
}
```

## Core Mechanics and Compiler Rules

To be evaluated as an expression rather than a statement, the `if` construct must adhere to strict compiler rules regarding exhaustiveness, branch composition, and type resolution.

**1. Exhaustiveness Requirement**
Unlike an `if` statement, an `if` expression must guarantee that a value is produced regardless of the evaluation path. Syntactically, Swift strictly requires an `else` branch for an `if` expression. Without an `else` branch, the compiler parses the construct as a statement rather than an expression. This makes it invalid on the right side of an assignment or return, even if the expected return type of the expression is `Void`.

**2. Single-Expression Branches**
Each branch within the `if` expression must contain exactly one expression. The compiler implicitly returns the result of this single expression. If a branch contains multiple statements (such as logging or intermediate variable declarations), the compiler cannot infer the yield value, and the construct reverts to being treated as a standard `if` statement.

```swift theme={"dark"}
// Valid: Single expression per branch
let result = if isValid {
    "Authorized"
} else {
    "Denied"
}

// Invalid: Multiple statements in a branch break the expression evaluation
let result = if isValid {
    print("Checking validity") 
    "Authorized" // Compiler Error
} else {
    "Denied"
}
```

**3. Type Consistency and Inference**
The Swift compiler must be able to determine a single, unified return type for the entire `if` expression. By default, the single expression in every branch must evaluate to the exact same type.

```swift theme={"dark"}
// Valid: Both branches return 'Int'
let count = if hasItems { 10 } else { 0 }

// Invalid: Branches return 'Int' and 'String'
let value = if hasItems { 10 } else { "None" } // Compiler Error: Branches have mismatching types
```

**4. Contextual Type Resolution**
If the branches return different types that share a common superclass or conform to a common protocol, the compiler will not automatically infer the common ancestor type. You must explicitly annotate the type of the receiving variable to provide the compiler with the necessary context for implicit conversion.

```swift theme={"dark"}
// Explicit existential type annotation 'any CustomStringConvertible' allows 
// branches to yield different underlying types (Int and String).
let message: any CustomStringConvertible = if isNumeric {
    404
} else {
    "Not Found"
}
```

**5. Handling the `Never` Type**
If a branch evaluates to the `Never` type (for example, by calling `fatalError()` or throwing an error), the compiler successfully ignores that branch for type resolution. The expression's unified type is inferred entirely from the remaining valid branches.

```swift theme={"dark"}
// Valid: The 'else' branch evaluates to 'Never', so the inferred type is 'Int'
let count = if hasItems {
    10
} else {
    fatalError("Invalid state")
}
```

## Usage Contexts

The compiler recognizes an `if` expression in three specific syntactic positions:

* **Assignment:** On the right-hand side of a `=` operator (`let` or `var` declarations).
* **Return:** Directly following a `return` keyword in a function or closure.
* **Implicit Return:** As the sole expression in a single-expression function, closure, or computed property.

```swift theme={"dark"}
// Context: Implicit Return in a computed property
var statusIndicator: String {
    if isActive {
        "Online"
    } else {
        "Offline"
    }
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Swift Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
