- Foreach
For each (or foreach) is a
computer language idiom 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 [cite web |url=http://www.digitalmars.com/d/statement.html#ForeachStatement |title=D Programming Language foreach Statement Documentation |accessdate=2008-08-04 |last= |first= |coauthors= |date= |work= |publisher=usually] maintain no explicit counter: they essentially say "do this to everything in this set", rather than "do this "x" times". This can potentially avoidoff-by-one error s and make code simpler to read. In object-oriented languages aniterator , even if implicit, is often used as the means of traversal.The Python programming language is notable in having only a "foreach" loop (called simply
for
), requiring explicit counting (often via therange
function) to achieve "standard" "for" behavior.yntax
Syntax varies among languages. Most use the simple word
for
, roughly as follows:for item in set: do something to item
Language support
Some of the languages with support for foreach loops include ABC, Ada, C#, Cobra, D,
ECMAScript , Java,Javascript ,Perl ,PHP , Python,REALbasic , Ruby,Smalltalk ,Tcl ,tcsh ,Daplex (a query language),Unix shell s,Visual Basic .NET andWindows PowerShell . Notable languages without foreach are C andC++ .
= Ada =Ada supports foreach loops as part of the normal
for loop . Say X is anarray :;Note: This syntax is mostly used on arrays but will also work with other types when a full iteration is needed.
= Cobra =Curly bracket programming language
In
curly bracket programming language s, the syntax is similar to this:
= C# =
= D =
= Java =JavaScript JavaScript also has a
for each...in
statement, which iterates over the values in the object, not the keyscite web |url=http://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Statements/for_each...in |title=JavaScript - for each...in statement |accessdate=2008-10-03] .In order to limit the iteration to the object's own properties, excluding the ones inherited through the prototype chain, it is advisable to add a hasOwnProperty() test, if supported by the JavaScript engine (for WebKit/Safari, this means "in version 3 or later").
Also note that it is inadvisable to use either a
for...in
orfor each...in
statement on an array in JavaScript, due to the above issue, and also because it is not guaranteed to iterate over the elements in any particular ordercite web |url=http://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Statements/for...in#Description |title=JavaScript - for...in statement on arrays |accessdate=2008-10-03] . A regular C-style for loop should be used instead.PHP PHP has an idiosyncratic syntax:
You can also extract the key from the set using this syntax:
The result would be:
name has a value of John
address has a value of 123 Main Street
= Python =Smalltalk Contrary to other languages, in
Smalltalk the foreach loop is not a language construct but defined in the class Collection as a method with one parameter, which is the body as aclosure . Also, Smalltalk definescollect
,select
,reject
, etc. as known from OCL, however Smalltalk predates OCL by about twenty years.Visual Basic .NET Windows PowerShell foreach ($item in $set) { # do something to $item }
C++ C++ does not have foreach, but its standard library includes afor_each
function (in
) which applies a function to all items between two iterators, and theQt toolkit provides a foreachpseudo -keyword for its container classes, implemented as a macro. [cite web |url=http://doc.trolltech.com/4.2/containers.html#the-foreach-keyword |title=Qt - The Foreach Keyword |accessdate=2008-08-04 |last= |first= |coauthors= |date= |work= |publisher=] There is also a similar macro in boost which performs within a few percent of the equivalent hand-coded loop. [cite web |url=http://boost-sandbox.sourceforge.net/libs/foreach/doc/html/ |title=Boost Library - Foreach Keyword Documentation |accessdate=2008-08-04 |last= |first= |coauthors= |date= |work= |publisher=]ee also
*
Do while loop
*For loop
*While loop
*Map (higher-order function) References
Wikimedia Foundation. 2010.