Anonymous user
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>▼
use Ada.Integer_Text_IO, Ada.Containers;
Line 77 ⟶ 72:
V.Iterate (Print_Element'Access);
end Vector_Example;</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);▼
▲[]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>
Output:
▲Mary Had +1 little lamb.
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
Loop, Parse, string, `,
MsgBox %A_LoopField%</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.
{
▲ for (container_type::iterator i = container.begin(); i != container.end(); ++i)
▲}</lang>
▲ std::cout << *i << "\n";
However the idiomatic way to output a container would be
▲ std::copy(container.begin(), container.end(),
▲ std::output_iterator<container_type::value_type>(std::cout, "\n"));
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)▼
▲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>
The next version of the standard will allow the following simplified syntax:
<lang cpp>#include <iterator_concepts>
for (auto element: container)
{
std::cout << element << "\n";
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>
for (auto const& element: container)
{
std::cout << element << "\n";
▲}</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}}==
<
or
<
=={{header|D}}==
Line 209 ⟶ 187:
=={{header|Forth}}==
=={{header|F_Sharp|F#}}==
We can use ''for'' directly or list iteration.
=={{header|HaXe}}==
Line 224 ⟶ 202:
=={{header|Haskell}}==
which is the same as
=={{header|J}}==
Line 266 ⟶ 244:
=={{header|Lisaac}}==
<lang Lisaac>"Lisaac loop foreach".split.foreach { word : STRING;▼
▲"Lisaac loop foreach".split.foreach { word : STRING;
word.print;
'\n'.print;
};</lang>
=={{header|Logo}}==
=={{header|MAXScript}}==
<lang maxscript>for i in collect do
(
print i
=={{header|Metafont}}==
Line 298 ⟶ 272:
=={{header|Io}}==
=={{header|MOO}}==
Line 352 ⟶ 326:
for v = b
disp(v); % v is the column vector [1;5], then [2;6] ...
endfor</lang>
We can also iterate over structures:
Line 373 ⟶ 346:
{{works with|Rakudo|#21 "Seattle"}}
<lang
say $i;
}</lang>
Line 391 ⟶ 364:
=={{header|Pop11}}==
Iteration over list:
<lang pop11>lvars el, lst = [1 2 3 4 foo bar];▼
▲lvars el, lst = [1 2 3 4 foo bar];
for el in lst do
printf(el,'%p\n');
endfor;</lang>
=={{header|PowerShell}}==
<lang powershell>foreach ($x in $collection) {
Line 410 ⟶ 381:
For example:
<lang python>lines = words = characters = 0
f = open('somefile','r')
for eachline in f:
Line 420 ⟶ 390:
chracters += 1
print lines, words, characters</lang>
=={{header|R}}==
Line 487 ⟶ 456:
=={{header|TI-89 BASIC}}==
<
Define strs = {"Lorem","ipsum","dolor"}
For i, 1, dim(strs)
Disp strs[i]
EndFor</
=={{header|UNIX Shell}}==
Line 499 ⟶ 468:
=={{header|V}}==
=={{header|VBScript}}==
=={{header|Visual Basic .NET}}==
<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.
</xsl:for-each>
|