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

The `if` statement in Swift is a control flow construct that conditionally executes a block of code based on the evaluation of a Boolean expression. Swift enforces strict type safety; the evaluated condition must explicitly resolve to a `Bool` type. Implicit type coercion (such as evaluating non-zero integers or non-nil objects as true) is not permitted by the compiler.

## Syntax Rules

* **Parentheses:** Parentheses `()` enclosing the condition are optional.
* **Braces:** Curly braces `{}` defining the execution block are mandatory, regardless of whether the block contains a single line or multiple lines of code.

```swift theme={"dark"}
if condition {
    // statements
} else if secondaryCondition {
    // statements
} else {
    // statements
}
```

## Optional Binding (`if let` and `if var`)

The `if` statement integrates directly with Swift's `Optional` type system to perform optional binding. This mechanism safely unwraps an `Optional<Wrapped>` value. If the optional contains a value, it is assigned to a new temporary constant (`let`) or variable (`var`) scoped exclusively to the true branch of the `if` block. Starting in Swift 5.7, a shorthand syntax allows shadowing the existing variable name without explicitly rewriting it.

```swift theme={"dark"}
// Explicit binding
if let unwrappedConstant = optionalValue {
    // unwrappedConstant is available here as a non-optional type
}

// Swift 5.7+ shorthand binding
if let optionalValue {
    // optionalValue is shadowed as a non-optional type within this block
}
```

## Pattern Matching (`if case`)

The `if` statement supports pattern matching using the `case` keyword. This evaluates whether a value matches a specific pattern, such as an enumeration case, and can simultaneously extract and bind associated values into local constants or variables.

```swift theme={"dark"}
if case .success(let data) = result {
    // Executes if 'result' matches the .success case, binding the associated value to 'data'
}
```

## Condition Lists

Swift allows multiple conditions within a single `if` statement, separated by commas `,`. The comma acts as a syntactic separator for a *condition list*, not an operator, and functions as a short-circuiting logical AND. This syntax is required when chaining multiple optional bindings or combining bindings with standard Boolean expressions, as the standard `&&` operator cannot separate binding declarations.

```swift theme={"dark"}
if let firstValue = optionalA, 
   let secondValue = optionalB, 
   firstValue < secondValue {
    // Evaluates sequentially. Short-circuits if any binding fails or condition is false.
}
```

## `if` as an Expression (Swift 5.9+)

Starting in Swift 5.9, `if` constructs can be evaluated as expressions, allowing the `if` block to directly return a value that can be assigned to a variable or returned from a function. Each branch must consist of a single expression. By default, the compiler infers the return type, requiring all branches to return the exact same type. However, branches can return different types if an explicit contextual type (such as `Any` or a shared protocol) is provided.

```swift theme={"dark"}
// Implicitly typed (branches must return the same type)
let result = if booleanCondition {
    "Condition met"
} else {
    "Condition failed"
}

// Explicitly typed (branches can return different types conforming to the contextual type)
let mixedResult: Any = if booleanCondition {
    1
} else {
    "String"
}
```

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