> ## 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 While Let

The `while let` construct is a control flow statement that combines a `while` loop with optional binding. It repeatedly evaluates an expression returning an `Optional`, unwraps the underlying value if it exists, binds it to a newly declared local constant or variable, and executes the loop body. The loop terminates immediately when the evaluated expression returns `nil`.

## Syntax

```swift theme={"dark"}
while let boundConstant = optionalExpression {
    // Loop body
    // boundConstant is available here as a non-optional type
}
```

## Execution Mechanics

1. **Evaluation:** The `optionalExpression` is evaluated at the start of every iteration.
2. **Pattern Matching:**
   * If the expression evaluates to `.some(Wrapped)`, the payload is unwrapped and bound to `boundConstant`. Control flow enters the loop body.
   * If the expression evaluates to `.none` (`nil`), the binding fails, the loop terminates, and control flow transfers to the first statement following the loop's closing brace.
3. **Scoping:** The lexical scope of `boundConstant` is strictly limited to the body of the `while` loop. It cannot be accessed outside of the loop.
4. **Re-evaluation:** After the loop body finishes executing, control jumps back to step 1. The `optionalExpression` must eventually evaluate to `nil` (typically through mutation or state advancement during the evaluation itself), otherwise the construct results in an infinite loop.

## Structural Variations

**Mutable Binding**
You can substitute `let` with `var` if you need to mutate the unwrapped value locally within the loop body. This mutation does not affect the original optional.

```swift theme={"dark"}
while var mutableBoundValue = optionalExpression {
    mutableBoundValue += 1 
    // mutableBoundValue is modified locally
}
```

**Compound Conditions**
A `while let` statement can evaluate multiple optional bindings and boolean conditions sequentially in a single line, separated by commas. Evaluation short-circuits from left to right; if any optional binding evaluates to `nil` or any boolean condition evaluates to `false`, the loop terminates immediately.

```swift theme={"dark"}
while let firstValue = firstOptional, 
      let secondValue = secondOptional, 
      firstValue < secondValue {
    // Executes only if both optionals are non-nil AND the boolean condition is true
}
```

## Mechanical Example

The following demonstrates the mechanics of `while let` using an array's `popLast()` method, which returns an `Optional<Element>` and mutates the array, ensuring the loop eventually terminates.

```swift theme={"dark"}
var stack = [10, 20, 30]

// popLast() returns Int?
while let topElement = stack.popLast() {
    print(topElement) 
}
// Iteration 1: stack becomes [10, 20], topElement is 30
// Iteration 2: stack becomes [10], topElement is 20
// Iteration 3: stack becomes [], topElement is 10
// Iteration 4: popLast() returns nil, loop terminates
```

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