> ## 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 For-In Loop

A `for-in` loop is a control flow statement that iterates over any type conforming to the `Sequence` protocol. Under the hood, it abstracts the creation of an `Iterator` (via the `makeIterator()` method) and repeatedly calls its `next()` method until `nil` is returned, executing the loop body for each unwrapped element.

```swift theme={"dark"}
for element in sequence {
    // Statements to execute
}
```

## Core Mechanics

* **Implicit Declaration:** The iteration variable (`element`) is implicitly declared as a constant (`let`) that is scoped exclusively to the loop's body. It is initialized with the current sequence value at the start of each iteration.
* **Mutable Iteration Variables:** While the iteration variable is a constant by default, Swift natively supports mutable iteration variables directly in the loop signature. By explicitly declaring the variable with `var`, the loop allocates a mutable local copy of the element for that specific iteration.

```swift theme={"dark"}
for var element in sequence {
    element.mutate() // Mutates the local copy, not the underlying sequence element
}
```

* **Sequence Conformance:** The right-hand side of the `in` keyword must evaluate to a type that conforms to `Sequence`. Common standard library types include `Array`, `Dictionary`, `Set`, `String`, and `Range`.

## Syntax Variations

**Pattern Matching (`for case`)**
The `for-in` loop integrates directly with Swift's pattern matching engine. By utilizing the `for case` syntax, the loop evaluates each element against a pattern, executing the body only for matching elements and silently skipping the rest. This is highly idiomatic for extracting associated values from enumerations or unwrapping non-nil optionals.

```swift theme={"dark"}
// Iterating over specific enum cases and binding associated values
for case let .success(value) in results {
    // Executes only for .success cases
}

// Iterating over non-nil optionals (the `?` pattern)
for case let item? in sequenceOfOptionals {
    // Executes only for non-nil elements, binding the unwrapped value to 'item'
}
```

**Range Iteration**
Iterating over a `ClosedRange` (`...`) or `Range` (`..<`).

```swift theme={"dark"}
for index in 1...5 {
    // Iterates 1, 2, 3, 4, 5
}

for index in 1..<5 {
    // Iterates 1, 2, 3, 4
}
```

**The Wildcard Pattern**
If the current value of the sequence is not required within the loop body, the wildcard pattern (`_`) is used to bypass variable allocation and suppress compiler warnings.

```swift theme={"dark"}
for _ in 1...10 {
    // Executes 10 times; the iteration value is discarded
}
```

**Filtering with `where` Clauses**
A `where` clause can be appended to the loop signature to evaluate a boolean condition before executing the loop body. The body is only executed if the condition evaluates to `true`. This acts as an inline filter, bypassing the need for a nested `if` statement.

```swift theme={"dark"}
for number in 1...10 where number % 2 == 0 {
    // Executes only for 2, 4, 6, 8, 10
}
```

**Tuple Destructuring**
When iterating over collections whose elements are tuples (such as a `Dictionary`, which is a sequence of `(Key, Value)` pairs), the `for-in` loop supports inline tuple destructuring.

```swift theme={"dark"}
for (key, value) in dictionary {
    // 'key' and 'value' are implicitly declared constants
}
```

**Striding**
For non-standard step increments, the `for-in` loop consumes the sequence generated by the global `stride(from:to:by:)` (exclusive) or `stride(from:through:by:)` (inclusive) functions.

```swift theme={"dark"}
for value in stride(from: 0, to: 10, by: 2) {
    // Iterates 0, 2, 4, 6, 8
}
```

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