Loops/Foreach: Difference between revisions

m
Fixed lang tags.
(Added HaXe language)
m (Fixed lang tags.)
Line 3:
=={{header|Ada}}==
===arrays===
<lang Ada>with Ada.Integer_Text_IO;
with Ada.Integer_Text_IO;
use Ada.Integer_Text_IO;
 
Line 17 ⟶ 16:
end loop;
 
end For_Each;</lang>
</lang>
 
===doubly linked lists===
{{works with|Ada 2005}}
<lang Ada>with Ada.Integer_Text_IO, Ada.Containers.Doubly_Linked_Lists;
<lang Ada>
with Ada.Integer_Text_IO, Ada.Containers.Doubly_Linked_Lists;
use Ada.Integer_Text_IO, Ada.Containers;
 
Line 47 ⟶ 44:
DL_List.Iterate (Print_Node'Access);
end Doubly_Linked_List;</lang>
</lang>
 
===vectors===
{{works with|Ada 2005}}
<lang Ada>with Ada.Integer_Text_IO, Ada.Containers.Vectors;
<lang Ada>
with Ada.Integer_Text_IO, Ada.Containers.Vectors;
use Ada.Integer_Text_IO, Ada.Containers;
 
Line 77 ⟶ 72:
V.Iterate (Print_Element'Access);
end Vector_Example;</lang>
</lang>
 
=={{header|ALGOL 68}}==
Line 84 ⟶ 78:
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}}
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}}
<lang algol68>[]UNION(STRING, INT, PROC(REF FILE)VOID) collection = ("Mary","Had",1,"little","lamb.",new line);
<pre>
[]UNION(STRING, INT, PROC(REF FILE)VOID) collection = ("Mary","Had",1,"little","lamb.",new line);
 
FOR index FROM LWB collection TO UPB collection DO
print((collection[index]," "))
OD</lang>
</pre>
Output:
<lang algol68>Mary Had +1 little lamb. </lang>
<pre>
Mary Had +1 little lamb.
</pre>
Note: [[ALGOL 68S]] actually has a reserved word FOREACH that is used to
break arrays in to portions, and process in parallel.
Line 109 ⟶ 99:
ENDPROC</lang>
=={{header|AutoHotkey}}==
<lang AutoHotkey>string = mary,had,a,little,lamb
string = mary,had,a,little,lamb
Loop, Parse, string, `,
MsgBox %A_LoopField%</lang>
</lang>
 
=={{header|AWK}}==
Line 133 ⟶ 121:
=={{header|C++}}==
C++ does not (yet) have a "for each" loop. The following is a generic loop which works with any standard container except for built-in arrays. The code snippet below assumes that the container type in question is typedef'd to <tt>container_type</tt> and the actual container object is named container.
<lang cpp>for (container_type::iterator i = container.begin(); i != container.end(); ++i)
<lang cpp>
{
for (container_type::iterator i = container.begin(); i != container.end(); ++i)
std::cout << *i << "\n";
{
}</lang>
std::cout << *i << "\n";
}
</lang>
However the idiomatic way to output a container would be
<lang cpp>std::copy(container.begin(), container.end(),
<lang cpp>
std::output_iterator<container_type::value_type>(std::cout, "\n"));</lang>
std::copy(container.begin(), container.end(),
std::output_iterator<container_type::value_type>(std::cout, "\n"));
</lang>
There's also an algorithm named <tt>for_each</tt>. However, you need a function or function object to use it, e.g.
<lang cpp>void print_element(container_type::value_type const& v)
<lang cpp>
void print_element(container_type::value_type const& v)
{
std::cout << v << "\n";
Line 152 ⟶ 135:
 
...
std::for_each(container.begin(), container.end(), print_element);</lang>
</lang>
 
The next version of the standard will allow the following simplified syntax:
<lang cpp>#include <iterator_concepts>
#include <iterator_concepts>
 
for (auto element: container)
{
std::cout << element << "\n";
}</lang Ada>
}
</lang>
Here <tt>container</tt> is the container variable, <tt>element</tt> is the loop variable (initialized with each container element in turn), and auto means that the compiler should determine the correct type of that variable automatically. If the type is expensive to copy, a const reference can be used instead:
<lang cpp>#include <iterator_concepts>
#include <iterator_concepts>
 
for (auto const& element: container)
{
std::cout << element << "\n";
}</lang>
}
</lang>
Of course the container elements can also be changed by using a non-const reference (provided the container isn't itself constant).
 
Line 184 ⟶ 162:
 
=={{header|Common Lisp}}==
<prelang language="lisp">(loop for i in list do (print i))</prelang>
or
<prelang language="lisp">(map nil #'print list)</prelang>
 
=={{header|D}}==
Line 209 ⟶ 187:
 
=={{header|Forth}}==
<lang forth>create a 3 , 2 , 1 ,
: .array ( a len -- )
cells bounds do i @ . cell +loop ; \ 3 2 1</lang>
 
=={{header|F_Sharp|F#}}==
We can use ''for'' directly or list iteration.
<lang fsharp>for i in [1 .. 10] do printfn "%d" i
 
List.iter (fun i -> printfn "%d" i) [1 .. 10]</lang>
 
=={{header|HaXe}}==
Line 224 ⟶ 202:
 
=={{header|Haskell}}==
<lang haskell>import Control.Monad (forM_)
forM_ collect print</lang>
which is the same as
<lang haskell>mapM_ print collect</lang>
 
=={{header|J}}==
Line 266 ⟶ 244:
 
=={{header|Lisaac}}==
<lang Lisaac>"Lisaac loop foreach".split.foreach { word : STRING;
<lang Lisaac>
"Lisaac loop foreach".split.foreach { word : STRING;
word.print;
'\n'.print;
};</lang>
</lang>
 
=={{header|Logo}}==
<lang logo>foreach [red green blue] [print ?]</lang>
=={{header|MAXScript}}==
<lang maxscript>for i in collect do
for i in collect do
(
print i
)</lang Ada>
)
</lang>
 
=={{header|Metafont}}==
Line 298 ⟶ 272:
 
=={{header|Io}}==
<lang io>collection foreach(println)</lang>
 
=={{header|MOO}}==
Line 352 ⟶ 326:
for v = b
disp(v); % v is the column vector [1;5], then [2;6] ...
endfor</lang>
</lang>
 
We can also iterate over structures:
Line 373 ⟶ 346:
{{works with|Rakudo|#21 "Seattle"}}
 
<lang perlperl6>for @collect -> $i {
say $i;
}</lang>
Line 391 ⟶ 364:
=={{header|Pop11}}==
Iteration over list:
<lang pop11>lvars el, lst = [1 2 3 4 foo bar];
<pre>
lvars el, lst = [1 2 3 4 foo bar];
for el in lst do
printf(el,'%p\n');
endfor;</lang>
</pre>
=={{header|PowerShell}}==
<lang powershell>foreach ($x in $collection) {
Line 410 ⟶ 381:
For example:
 
<lang python>lines = words = characters = 0
lines = words = characters = 0
f = open('somefile','r')
for eachline in f:
Line 420 ⟶ 390:
chracters += 1
 
print lines, words, characters</lang>
</lang>
 
=={{header|R}}==
Line 487 ⟶ 456:
=={{header|TI-89 BASIC}}==
 
<prelang style="font-family:'TI Uni'"ti89b>Local i,strs
Define strs = {"Lorem","ipsum","dolor"}
For i, 1, dim(strs)
Disp strs[i]
EndFor</prelang>
 
=={{header|UNIX Shell}}==
Line 499 ⟶ 468:
 
=={{header|V}}==
<lang v>[1 2 3] [puts] step</lang>
 
=={{header|VBScript}}==
<lang vbscript>dim items(2)
items(0)="Apple"
items(1)="Orange"
items(2)="Banana"
 
For Each x in items
WScript.Echo x
Next</lang>
 
=={{header|Visual Basic .NET}}==
 
<lang vbnet>Dim list As New List(Of String)
list.Add("Car")
list.Add("Boat")
list.Add("Train")
For Each item In list
Console.WriteLine(item)
Next</lang>
 
=={{header|XSLT}}==
For-each is the only iteration construct that is built into XSLT. All other iteration is either implied by applying a template to all members matching an XPath expression, or built from recursive application of a template. You have access to something like a loop counter with the one-based "position()" function.
<lang xml><fo:block font-weight="bold">Adults:</fo:block>
<xsl:for-each select="person[@age &amp;gt;= 21]">
<fo:block><xsl:value-of select="position()"/>. <xsl:value-of select="@name"/></fo:block></lang>
</xsl:for-each>
Anonymous user