Foreach Loop Code Segment
(Redirected from Foreach Loop)
Jump to navigation
Jump to search
A Foreach Loop Code Segment is a for loop code segment that starts with a foreach statement (which does not maintain an explicit counter).
- Example(s):
- a Python foreach Loop Code Segment, with a Python foreach statement.
- a Perl foreach Loop Code Segment, with a Perl foreach statement.
- …
- Counter-Example(s):
- See: Data Iterator, Control Structure, Infinite Loop, Control Flow, Collection Class, Statement (Programming), Off-by-One Error, Iterator, Loop Optimization.
References
2016
- (Wikipedia, 2016) ⇒ https://en.wikipedia.org/wiki/foreach_loop Retrieved:2016-7-25.
- For each (or foreach) is a control flow statement for traversing items in a collection. Foreach is usually used in place of a standard for statement. Unlike other for loop constructs, however, foreach loops usually maintain no explicit counter: they essentially say "do this to everything in this set", rather than "do this x times". This avoids potential off-by-one errors and makes code simpler to read. In object-oriented languages an iterator, even if implicit, is often used as the means of traversal.
The foreach statement in some languages has some defined order, processing each item in the collection from the first to the last.
The foreach statement in many other languages does not have any particular order, especially array programming languages, in order to support loop optimization in general and in particular to allow vector processing to process some or all of the items in the collection simultaneously.
- For each (or foreach) is a control flow statement for traversing items in a collection. Foreach is usually used in place of a standard for statement. Unlike other for loop constructs, however, foreach loops usually maintain no explicit counter: they essentially say "do this to everything in this set", rather than "do this x times". This avoids potential off-by-one errors and makes code simpler to read. In object-oriented languages an iterator, even if implicit, is often used as the means of traversal.