> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/ios/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/ios/swift/flow/loop/for...in-vs.-foreach.md).

# for...in vs. forEach

[Swift](/ios/swift.md) ⟩ [flow control](/ios/swift/flow.md) ⟩ [loop](/ios/swift/flow/loop.md) ⟩ for...in vs. .forEach()

<table><thead><tr><th width="157.33333333333331">flow control</th><th>for ... in</th><th>.forEach()</th></tr></thead><tbody><tr><td><mark style="color:purple;"><strong>continue</strong></mark></td><td>✅ skip to the <mark style="color:red;"><strong>next</strong></mark> iteration.</td><td>⛔ not allowed</td></tr><tr><td><mark style="color:purple;"><strong>break</strong></mark></td><td>✅ break the iteration <mark style="color:red;"><strong>completely</strong></mark>. </td><td>⛔ not allowed</td></tr><tr><td><a data-mention href="/pages/zHYBHx5i0445bpguZAHc">/pages/zHYBHx5i0445bpguZAHc</a></td><td>❌ <mark style="color:red;"><strong>invalid</strong></mark> outside of a <strong>function</strong>.</td><td>❗skip to the <mark style="color:red;"><strong>next</strong></mark> iteration.</td></tr></tbody></table>

{% tabs %}
{% tab title="🧨 雷區" %}

* :point\_right: [replit](https://replit.com/@pegasusroe/forin-vs-forEach#main.swift)

{% hint style="danger" %}
⛔ <mark style="color:red;">**error**</mark>: [return](/ios/swift/flow/transfer/return.md) <mark style="color:red;">**invalid**</mark>**&#x20;**<mark style="color:yellow;">**outside**</mark> of a <mark style="color:blue;">**func**</mark>.
{% endhint %}

```javascript
for n in 1...10 {
    if n == 3 { return }    // ⛔ error
    //          ^^^^^^
    // ⛔ error: return invalid outside of a func
}
```

* :point\_right: [replit](https://replit.com/@pegasusroe/forin-vs-forEach#main.swift)

{% hint style="danger" %}
⛔ <mark style="color:red;">**error**</mark>: [continue](/ios/swift/flow/transfer/continue.md) is <mark style="color:orange;">**only**</mark>**&#x20;**<mark style="color:yellow;">**allowed inside**</mark> a [loop](/ios/swift/flow/loop.md).
{% endhint %}

```javascript
(1...10).forEach { n in 
    if n > 2 { continue }    // ⭐ `continue` is inside a closure❗
    //         ^^^^^^^^
    // ⛔ error: 'continue' is only allowed inside a loop
}
```

* :point\_right: [replit](https://replit.com/@pegasusroe/forin-vs-forEach#main.swift)

{% hint style="danger" %}
⛔ <mark style="color:red;">**error**</mark>: <mark style="color:yellow;">**unlabeled**</mark> [break](/ios/swift/flow/transfer/break.md) is only allowed <mark style="color:yellow;">**inside**</mark> a <mark style="color:orange;">**loop**</mark> or [switch](/ios/swift/flow/branch/switch.md), a <mark style="color:yellow;">**labeled**</mark> [break](/ios/swift/flow/transfer/break.md) is <mark style="color:orange;">**required**</mark> to exit an [if](/ios/swift/flow/branch/if.md) or [do](/ios/swift/statement/do.md).
{% endhint %}

```javascript
(1...10).forEach { n in 
    if n > 2 { break }    // ⭐ `break` is inside a "closure"
    //         ^^^^^
    // ⛔ error: 
    //  • unlabeled 'break' is only allowed inside a loop or switch
    //  • labeled 'break' is required to exit an `if` or `do`
}
```

{% endtab %}

{% tab title="💈範例" %}

* :point\_right: [replit](https://replit.com/@pegasusroe/forin-vs-forEach#main.swift)

```swift
// ------------ for...in ------------------------------

for n in 1...10 {
    if n == 3 { continue }    // ⭐ skip to next iteration
    if n > 6 { break }        // ⭐ break loop completely❗
    a1.append(n)              // [1, 2, 4, 5, 6]
}

// ------------ .forEach ------------------------------

(1...10).forEach { n in 
    if n > 2 { return }     // ⭐ early return ( skip to next iteration❗)
    a2.append(n)            // [1, 2]     (n > 2 NOT appended)
}

(1...10).forEach { n in 
    a3.append(n)            // ⭐ number appended
    if n > 2 { return }     // ⭐ late return ( has no effect❗)
}                           // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
```

{% endtab %}

{% tab title="⭐️ 重點" %}
{% hint style="info" %}

{% endhint %}
{% endtab %}

{% tab title="👥 相關" %}

* [seq.forEach()](/ios/swift/collections/sequence/seq.foreach.md)
* [for...in](/ios/swift/flow/loop/for...in.md)
  {% endtab %}

{% tab title="📗 參考" %}

* [x] Advanced Swift, p.&#x20;
* [x] Sundell ⟩ [Picking between a for loop and forEach](https://www.swiftbysundell.com/tips/picking-between-for-and-for-each/)
  {% endtab %}
  {% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lochiwei.gitbook.io/ios/swift/flow/loop/for...in-vs.-foreach.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
