<p>When iterating over an array you can use an indexed for loop, like so:</p>
<pre><code>for (int i = 0; i &lt; vectorSize; i++) {
    e = element i of array
    use e for something
}
</code></pre>
<p>but you can also use a loop that goes through each item in the array without using an index:</p>
<pre><code>for (auto e : vector) {
    use e for something
}
</code></pre>
<p>I've noticed that some languages support both types of loops, while some only use one or the other. What are the pros and cons of implementing the 'indexed' for loop as opposed to implementing a for-each loop?</p>

<arrays><for-loops>
What are the pros and cons of implementing indexed for loops?
CC BY-SA 4.0
2023-05-19T00:20:20.820Z
Would that make it a duplicate of [my question](https://languagedesign.stackexchange.com/q/193/15)? Would still be closed, just for a different reason
Bbrk24
CC BY-SA 4.0
2023-05-19T00:21:39.133Z
That's true, I just realized... but I would still rather have it closed as duplicate than off-topic and have the answers be linked
Redz
CC BY-SA 4.0
2023-05-17T07:29:14.620Z
This is more of best practices than language design, no?
justANewbie
CC BY-SA 4.0
2023-05-17T07:31:22.020Z
True, but there are other questions based on 'best practices' that are similar and I think it is on-topic.
Redz
<p>Obviously the indexed loop is more flexible in how it can update the index. Depending on the rest of the language this can matter more or less. Consider things like:</p>
<ol>
<li>Iterating from the back.</li>
<li>Stepping every second or third index.</li>
<li>Skipping a variable amount of steps due to some data condition.</li>
<li>Removing entries during the loop.</li>
<li>Adding entries during the loop.</li>
<li>Storing the index where the loop terminated (if exiting using a <code>break</code> or similar)</li>
<li>Tracking the index.</li>
</ol>
<p>The above points may or may not be supported by foreach as well, but it gives examples of why an index may sometimes be more useful.</p>

CC BY-SA 4.0
2023-05-17T07:32:36.690Z
2023-05-17T07:32:36.690Z
Nuoji
# What are the pros and cons of implementing indexed for loops?
Posted by: username_0 on 2023-05-17
When iterating over an array you can use an indexed for loop, like so:
for (int i = 0; i < vectorSize; i++) {
    e = element i of array
    use e for something
}

but you can also use a loop that goes through each item in the array without using an index:
for (auto e : vector) {
    use e for something
}

I've noticed that some languages support both types of loops, while some only use one or the other. What are the pros and cons of implementing the 'indexed' for loop as opposed to implementing a for-each loop?
**Tags:** arrays for-loops
> **Comments:**
> **username_2**: This is more of best practices than language design, no?  
> **username_0**: True, but there are other questions based on 'best practices' that are similar and I think it is on-topic.  
> **username_1**: Would that make it a duplicate of [my question](https://languagedesign.stackexchange.com/q/193/15)? Would still be closed, just for a different reason  
> **username_0**: That's true, I just realized... but I would still rather have it closed as duplicate than off-topic and have the answers be linked  
---
## 1 answers
### Answer by username_3 on 2023-05-17. Score: 3 | Accepted answer
Obviously the indexed loop is more flexible in how it can update the index. Depending on the rest of the language this can matter more or less. Consider things like:

1. Iterating from the back.
2. Stepping every second or third index.
3. Skipping a variable amount of steps due to some data condition.
4. Removing entries during the loop.
5. Adding entries during the loop.
6. Storing the index where the loop terminated (if exiting using a `break` or similar)
7. Tracking the index.

The above points may or may not be supported by foreach as well, but it gives examples of why an index may sometimes be more useful.
---
