Foreach loop: Difference between revisions

Content deleted Content added
m →‎Haskell: WP:LINK: update-standardize, needless WP:PIPE > WP:NOPIPE.
m Cut needless carriage return whitespace characters in sections: standardize, aid work via small screens.
Line 1:
{{Short description|Control flow statement for traversing items in a collection}}
 
[[File:For-Loop-Mint-Programming-Language-Type-2.gif|thumb|{{mono|foreach}} loops are almost always used to iterate over items in a sequence of elements.]]
{{Loop constructs}}<!-- NOTE: Please don't remove. Discuss navigation concept at [[Talk:Do_while_loop#Helpbox_experiment] -->
Line 21 ⟶ 20:
 
== Language support ==
 
[[Programming language]]s which support foreach loops include [[ABC (programming language)|ABC]], [[ActionScript]], [[Ada (programming language)|Ada]], [[C++11]], [[C Sharp (programming language)|C#]], [[ColdFusion Markup Language]] (CFML), [[Cobra (programming language)|Cobra]], [[D (programming language)|D]], [[Daplex]] (query language), [[Delphi (software)|Delphi]], [[ECMAScript]], [[Erlang (programming language)|Erlang]], [[Java (programming language)|Java]] (since 1.5), [[JavaScript]], [[Lua (programming language)|Lua]], [[Objective-C]] (since 2.0), [[ParaSail (programming language)|ParaSail]], [[Perl]], [[PHP]], [[Prolog]],<ref>{{Cite web|url=https://www.swi-prolog.org/pldoc/man?predicate=foreach/2|title=SWI-Prolog -- foreach/2|website=www.swi-prolog.org|access-date=2020-02-10}}</ref> [[Python (programming language)|Python]], [[R (programming language)|R]], [[REALbasic]], [[Rebol (programming language)|Rebol]],<ref>{{Cite web|url=http://www.rebol.com}}</ref> [[Red (programming language)|Red]],<ref>{{Cite web|url=http://www.red-lang.org}}</ref> [[Ruby (programming language)|Ruby]], [[Scala (programming language)|Scala]], [[Smalltalk]], [[Swift (programming language)|Swift]], [[Tcl]], [[tcsh]], [[Unix shell]]s, [[Visual Basic .NET]], and [[Windows PowerShell]]. Notable languages without foreach are [[C (programming language)|C]], and [[C++]] pre-C++11.
 
=== ActionScript 3.0 ===
 
[[ActionScript]] supports the ECMAScript 4.0 Standard<ref>{{cite web|url=https://www.ecma-international.org/activities/Languages/Language%20overview.pdf |title=Proposed ECMAScript 4th Edition – Language Overview |access-date=2020-02-21}}</ref> for <code>for each .. in</code><ref>{{cite web|url=https://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/statements.html#for_each..in |title=for each..in |access-date=2020-02-21}}</ref> which pulls the value at each index.
 
Line 53 ⟶ 50:
=== Ada ===
{{Wikibooks|Ada Programming|Control}}
 
[[Ada (programming language)|Ada]] supports foreach loops as part of the normal [[for loop]]. Say X is an [[Array data structure|array]]:
 
Line 174 ⟶ 170:
 
=== C++ ===
 
[[C++11]] provides a foreach loop. The syntax is similar to that of [[Foreach loop#Java|Java]]:
 
Line 266 ⟶ 261:
 
=== C++/CLI ===
 
The [[C++/CLI]] language proposes a construct similar to C#.
 
Line 282 ⟶ 276:
 
==== Script syntax ====
 
<syntaxhighlight lang="cfs">
// arrays
Line 322 ⟶ 315:
 
==== Tag syntax ====
 
<syntaxhighlight lang="CFM">
<!--- arrays --->
Line 339 ⟶ 331:
 
=== Common Lisp ===
 
[[Common Lisp]] provides foreach ability either with the ''dolist'' macro:
<syntaxhighlight lang="LISP">
Line 358 ⟶ 349:
=== D ===
{{Main|D (programming language)}}
 
<syntaxhighlight lang="D">
foreach(item; set) {
Line 375 ⟶ 365:
=== Dart ===
{{Main|Dart (programming language)}}
 
<syntaxhighlight lang="Java">
for (final element in someCollection) {
Line 384 ⟶ 373:
=== Object Pascal, Delphi ===
{{Main|Object Pascal|Delphi (software)}}
 
Foreach support was added in [[Delphi (software)|Delphi]] 2005, and uses an enumerator variable that must be declared in the ''var'' section.
 
Line 395 ⟶ 383:
 
=== Eiffel ===
 
The iteration (foreach) form of the [[Eiffel (programming language)|Eiffel]] loop construct is introduced by the keyword <code lang=Eiffel>across</code>.
 
Line 421 ⟶ 408:
 
=== Go ===
 
[[Go (programming language)|Go]]'s foreach loop can be used to loop over an array, slice, string, map, or channel.
 
Line 439 ⟶ 425:
 
=== Groovy ===
 
[[Groovy (programming language)|Groovy]] supports ''for'' loops over collections like arrays, lists and ranges:
 
Line 478 ⟶ 463:
 
=== Haskell ===
 
[[Haskell]] allows looping over lists with [[Monad (functional programming)|monadic]] actions using <code>mapM_</code> and <code>forM_</code> (<code>mapM_</code> with its arguments flipped) from [http://hackage.haskell.org/package/base-4.6.0.1/docs/Control-Monad.html Control.Monad]:
 
Line 517 ⟶ 501:
 
=== Java ===
 
In [[Java (programming language)|Java]], a foreach-construct was introduced in [[Java Development Kit]] (JDK) 1.5.0.<ref name="jdk5release">
"Enhanced for Loop - This new language construct[...]"
Line 602 ⟶ 585:
=== Lua ===
{{Main|Lua (programming language)}}
 
Source:<ref>{{Cite web|url=https://en.wikibooks.org/wiki/Lua_Programming/Tables#Foreach_loop|title=Lua Programming/Tables - Wikibooks, open books for an open world|website=en.wikibooks.org|language=en|access-date=2017-12-06}}</ref>
 
Line 616 ⟶ 598:
 
=== Mathematica ===
 
In [[Mathematica]], <code>Do</code> will simply evaluate an expression for each element of a list, without returning any value.
 
Line 634 ⟶ 615:
=== MATLAB ===
{{Main|MATLAB}}
 
<syntaxhighlight lang="MATLAB">
for item = array
Line 642 ⟶ 622:
 
=== Mint ===
 
For each loops are supported in Mint, possessing the following syntax:
<syntaxhighlight lang="Ruby">
Line 672 ⟶ 651:
 
=== Objective-C ===
 
Foreach loops, called [[Objective-C#Fast enumeration|Fast enumeration]], are supported starting in [[Objective-C]] 2.0. They can be used to iterate over any object that implements the NSFastEnumeration protocol, including NSArray, NSDictionary (iterates over keys), NSSet, etc.
 
Line 719 ⟶ 697:
 
=== OCaml ===
 
[[OCaml]] is a [[functional language]]. Thus, the equivalent of a foreach loop can be achieved as a library function over lists and arrays.
 
Line 747 ⟶ 724:
 
=== ParaSail ===
 
The [[ParaSail (programming language)|ParaSail]] parallel programming language supports several kinds of iterators, including a general "for each" iterator over a container:
 
Line 769 ⟶ 745:
 
=== Pascal ===
 
In [[Pascal (programming language)|Pascal]], ISO standard 10206:1990 introduced iteration over [[Pascal (programming language)#Set types|set types]], thus:
 
Line 784 ⟶ 759:
 
=== Perl ===
 
In [[Perl]], ''foreach'' (which is equivalent to the shorter for) can be used to traverse elements of a list. The expression which denotes the collection to loop over is evaluated in list-context and each item of the resulting list is, in turn, aliased to the loop variable.
 
Line 829 ⟶ 803:
=== PHP ===
{{Main|PHP syntax and semantics}}
 
<syntaxhighlight lang="php">
foreach ($set as $value) {
Line 862 ⟶ 835:
=== Python ===
{{Main|Python (programming language)}}
 
<syntaxhighlight lang="python">
for item in iterable_collection:
Line 892 ⟶ 864:
=== R ===
{{Main|R (programming language)}}
 
<syntaxhighlight lang="R">
for (item in object) {
Line 922 ⟶ 893:
 
=== Raku ===
 
In [[Raku (programming language)|Raku]], a sister language to Perl, ''for'' must be used to traverse elements of a list (''foreach'' is not allowed). The expression which denotes the collection to loop over is evaluated in list-context, but not flattened by default, and each item of the resulting list is, in turn, aliased to the loop variable(s).
 
Line 996 ⟶ 966:
=== Ruby ===
{{Main|Ruby (programming language)}}
 
<syntaxhighlight lang="Ruby">
set.each do |item|
Line 1,022 ⟶ 991:
=== Rust ===
{{Main|Rust (programming language)}}
 
The <code>for</code> loop has the structure <syntaxhighlight lang="rust" inline>for <pattern> in <expression> { /* optional statements */ }</syntaxhighlight>. It implicitly calls the [https://doc.rust-lang.org/std/iter/trait.IntoIterator.html <code>IntoIterator::into_iter</code>] method on the expression, and uses the resulting value, which must implement the [https://doc.rust-lang.org/std/iter/trait.Iterator.html <code>Iterator</code>] trait. If the expression is itself an iterator, it is used directly by the <code>for</code> loop through an [https://doc.rust-lang.org/std/iter/trait.IntoIterator.html#impl-IntoIterator-25 implementation of <code>IntoIterator</code> for all <code>Iterator</code>s] that returns the iterator unchanged. The loop calls the <code>Iterator::next</code> method on the iterator before executing the loop body. If <code>Iterator::next</code> returns <code>[[option type|Some(_)]]</code>, the value inside is assigned to the [[pattern matching|pattern]] and the loop body is executed; if it returns <code>None</code>, the loop is terminated.
 
Line 1,056 ⟶ 1,024:
=== Scala ===
{{Main|Scala (programming language)}}
 
<syntaxhighlight lang="Scala">
// return list of modified elements
Line 1,090 ⟶ 1,057:
 
=== Swift ===
 
[[Swift (programming language)|Swift]] uses the <code>for</code>…<code>in</code> construct to iterate over members of a collection.<ref>{{Cite web|url=https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/swift_programming_language/ControlFlow.html#//apple_ref/doc/uid/TP40014097-CH9-XID_153|title=Control Flow — the Swift Programming Language (Swift 5.5)}}</ref>
 
Line 1,114 ⟶ 1,080:
 
=== SystemVerilog ===
 
[[SystemVerilog]] supports iteration over any vector or array type of any dimensionality using the <code>foreach</code> keyword.
 
Line 1,157 ⟶ 1,122:
 
=== Tcl ===
 
[[Tcl]] uses foreach to iterate over lists. It is possible to specify more than one iterator variable, in which case they are assigned sequential values from the list.
{| class="wikitable"