<p>Yes, FOR loops. Those great things. There are good for shortening code. But there may be downsides I'm not aware of. I'm referring to Python style FOR loops. So, what are the disadvantages of including foreach loops? This is not a duplicate of <a href="https://langdev.stackexchange.com/questions/193/what-are-advantages-and-disadvantages-of-c-style-for-loops">this question</a> because that talks about Python Style for loops compared to C style for loops while mine talks about Python style for loops compared to not having for loops.</p>

<syntax><optimization><loops><foreach-loop>
What are the disadvantages of including foreach loops?
CC BY-SA 4.0
2023-07-04T15:07:16.847Z
Downsides compared to what? Do you mean compared to manually using an iterator with `next` in a `while` loop, or do you mean compared to some other paradigm where iterators aren't used at all? You can't really talk about the advantages and disadvantages of for-each loops separately from "iterables" which mainly exist to be used in for-each loops.
kaya3
CC BY-SA 4.0
2023-07-04T15:07:44.133Z
Compared to having no for loop @kaya3-supportthestrike
Starship
CC BY-SA 4.0
2023-07-04T15:09:37.770Z
So if you don't have a for-each loop, are you intending to use iterators manually in `while` loops, or are you doing away with iterators entirely? You would only leave them out of the language if they were unnecessary or superfluous. As written, your question might as well be "what are the disadvantages of having both arms?"
kaya3
<p>There really are none, except maybe adding yet another keyword or discouraging newbies from learning the &quot;general&quot; loop constructs of your language that might allow for more flexibility.</p>
<p>Other than that, iterating through an array (or other collection-like data structure) is such a common use-case that a dedicated syntax for it makes sense. Especially considering that it leaves a lot of room for optimization.</p>

CC BY-SA 4.0
2023-07-04T15:12:27.117Z
2023-07-04T15:29:47.330Z
2023-07-04T15:29:47.330Z
Philipp
# What are the disadvantages of including foreach loops?
Posted by: username_0 on 2023-07-04
Yes, FOR loops. Those great things. There are good for shortening code. But there may be downsides I'm not aware of. I'm referring to Python style FOR loops. So, what are the disadvantages of including foreach loops? This is not a duplicate of [this question](https://langdev.stackexchange.com/questions/193/what-are-advantages-and-disadvantages-of-c-style-for-loops) because that talks about Python Style for loops compared to C style for loops while mine talks about Python style for loops compared to not having for loops.
**Tags:** syntax optimization loops foreach-loop
> **Comments:**
> **username_1**: Downsides compared to what? Do you mean compared to manually using an iterator with `next` in a `while` loop, or do you mean compared to some other paradigm where iterators aren't used at all? You can't really talk about the advantages and disadvantages of for-each loops separately from "iterables" which mainly exist to be used in for-each loops.  
> **username_0**: Compared to having no for loop @kaya3-supportthestrike  
> **username_1**: So if you don't have a for-each loop, are you intending to use iterators manually in `while` loops, or are you doing away with iterators entirely? You would only leave them out of the language if they were unnecessary or superfluous. As written, your question might as well be "what are the disadvantages of having both arms?"  
---
## 1 answers
### Answer by username_2 on 2023-07-04. Score: 4 | Accepted answer
There really are none, except maybe adding yet another keyword or discouraging newbies from learning the "general" loop constructs of your language that might allow for more flexibility.

Other than that, iterating through an array (or other collection-like data structure) is such a common use-case that a dedicated syntax for it makes sense. Especially considering that it leaves a lot of room for optimization.
---
