Jump to content

Loops/Foreach: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 22:
*   [[Loops/Wrong ranges]]
<br><br>
 
=={{header|ACL2}}==
 
Line 222:
for (i, v in list(2, 3, 5, 7, 11, 13, 17, 18)) {
}</lang>
 
 
=={{header|ALGOL 68}}==
Line 719 ⟶ 718:
}
</lang>
 
=={{header|C sharp|C#}}==
<lang csharp>string[] things = {"Apple", "Banana", "Coconut"};
 
foreach (string thing in things)
{
Console.WriteLine(thing);
}</lang>
 
=={{header|C++}}==
Line 751 ⟶ 758:
{
element += 42;
}</lang>
 
=={{header|C sharp|C#}}==
<lang csharp>string[] things = {"Apple", "Banana", "Coconut"};
 
foreach (string thing in things)
{
Console.WriteLine(thing);
}</lang>
 
Line 1,008 ⟶ 1,007:
 
<code>cl.el</code> also offers a <code>loop</code> macro similar in style to [[#Common Lisp|Common Lisp]].
 
 
=={{header|Erlang}}==
Line 1,015 ⟶ 1,013:
However, to iterate over each element of a list, Erlang uses <tt>lists:map/2</tt>, except in the case of IO where <tt>lists:foreach/2</tt> has to be used as the evaluation order is defined to be the same as the order of the elements in the list.
<lang erlang>lists:foreach(fun(X) -> io:format("~p~n",[X]) end, Collection).</lang>
 
 
=={{header|ERRE}}==
Line 1,091 ⟶ 1,088:
2
</pre>
 
=={{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|Factor}}==
Line 1,109 ⟶ 1,112:
}</lang>
 
=={{header|friendly interactive shellForth}}==
<lang forth>create a 3 , 2 , 1 ,
Unlike, bash or csh, the PATH variable is automatically converted to real array.
: .array ( a len -- )
<lang fishshell>for path in $PATH
cells bounds do i @ . cell +loop ; \ 3 2 1</lang>
echo You have $path in PATH.
end</lang>
 
Sample output:
<pre>
You have /bin in PATH.
You have /usr/bin in PATH.
</pre>
 
=={{header|Fortran}}==
Line 1,139 ⟶ 1,135:
end program main</lang>
 
=={{header|Forthfriendly interactive shell}}==
Unlike, bash or csh, the PATH variable is automatically converted to real array.
<lang forth>create a 3 , 2 , 1 ,
<lang fishshell>for path in $PATH
: .array ( a len -- )
echo You have $path in PATH.
cells bounds do i @ . cell +loop ; \ 3 2 1</lang>
end</lang>
 
Sample output:
<pre>
You have /bin in PATH.
You have /usr/bin in PATH.
</pre>
 
=={{header|Frink}}==
Line 1,152 ⟶ 1,154:
println[n]
</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|GAP}}==
Line 1,234 ⟶ 1,230:
=={{header|Hy}}==
<lang clojure>(for [x collection] (print x))</lang>
 
=={{header|Io}}==
<lang io>collection foreach(println)</lang>
 
=={{header|Icon}} and {{header|Unicon}}==
Line 1,248 ⟶ 1,241:
<lang Icon>every write(!L)</lang>
 
=={{header|Io}}==
<lang io>collection foreach(println)</lang>
 
=={{header|J}}==
Line 1,363 ⟶ 1,358:
*/</lang>
 
=={{header|Jsish}}==
Jsi supports ''for of'' for looping over element of an array.
<lang javascript>for (str of "alpha beta gamma delta".split(' ')) { puts(str); }</lang>
 
{{out}}
<pre>alpha
beta
gamma
delta</pre>
 
=={{header|Julia}}==
{{trans|Python}}
<lang julia>for i in collection
println(i)
end</lang>
The Julia <code>for</code> statement is always a "foreach", and the built-in <code>start:end</code> or <code>start:step:end</code> "range" syntax can be used for iteration over arithmetic sequences. Many Julia objects support iteration: arrays and tuples iterate over each item, strings iterate over each character, dictionaries iterate over (key,value) pairs, numeric scalars provide a length-1 iteration over their value, and so on.
=={{header|jq}}==
'''Iterables''':
Line 1,410 ⟶ 1,389:
 
In both cases, the result is the stream of values: "a", "b", "c".
 
=={{header|Jsish}}==
Jsi supports ''for of'' for looping over element of an array.
<lang javascript>for (str of "alpha beta gamma delta".split(' ')) { puts(str); }</lang>
 
{{out}}
<pre>alpha
beta
gamma
delta</pre>
 
=={{header|Julia}}==
{{trans|Python}}
<lang julia>for i in collection
println(i)
end</lang>
The Julia <code>for</code> statement is always a "foreach", and the built-in <code>start:end</code> or <code>start:step:end</code> "range" syntax can be used for iteration over arithmetic sequences. Many Julia objects support iteration: arrays and tuples iterate over each item, strings iterate over each character, dictionaries iterate over (key,value) pairs, numeric scalars provide a length-1 iteration over their value, and so on.
 
=={{header|K}}==
<lang K> {`0:$x} ' !10</lang>
<lang K> _sin ' (1; 2; 3;)</lang>
 
=={{header|Kotlin}}==
Line 1,429:
</pre>
 
=={{header|LabVIEW}}==
[[LabVIEW]] has a feature known as an Auto-Indexed Tunnel. It is the very small orange box on the lower left of the for loop.<br/>{{VI solution|LabVIEW_Loops_Foreach.png}}
 
=={{header|Lang5}}==
<lang lang5>: >>say.(*) . ;
5 iota >>say.</lang>
 
=={{header|langur}}==
Line 1,467 ⟶ 1,473:
<lang Lasso>array(1,2,3) => foreach { stdoutnl(#1) }</lang>
<lang Lasso>with i in array(1,2,3) do { stdoutnl(#i) }</lang>
 
=={{header|K}}==
<lang K> {`0:$x} ' !10</lang>
<lang K> _sin ' (1; 2; 3;)</lang>
 
=={{header|LFE}}==
Line 1,479 ⟶ 1,481:
(lists:seq 1 10))
</lang>
 
=={{header|LabVIEW}}==
[[LabVIEW]] has a feature known as an Auto-Indexed Tunnel. It is the very small orange box on the lower left of the for loop.<br/>{{VI solution|LabVIEW_Loops_Foreach.png}}
 
=={{header|Lang5}}==
<lang lang5>: >>say.(*) . ;
5 iota >>say.</lang>
 
=={{header|LIL}}==
Line 1,750 ⟶ 1,745:
dolor
</pre>
 
=={{header|Objeck}}==
<lang objeck>fruits := ["Apple", "Banana", "Coconut"];
each(i : fruits) {
fruits[i]->PrintLine();
};</lang>
 
=={{header|Objective-C}}==
Line 1,771 ⟶ 1,772:
// do something with object i
}</lang>
 
=={{header|Objeck}}==
<lang objeck>fruits := ["Apple", "Banana", "Coconut"];
each(i : fruits) {
fruits[i]->PrintLine();
};</lang>
 
=={{header|OCaml}}==
Line 1,787 ⟶ 1,782:
(fun i -> Printf.printf "%d\n" i)
collect_array</lang>
 
=={{header|Oforth}}==
 
<lang Oforth>: printMonths | m | Date.Months forEach: m [ m . ] ;</lang>
 
But, apply can be used instead of a loop :
<lang Oforth>#. Date.Months apply</lang>
 
=={{header|Octave}}==
Line 1,811 ⟶ 1,799:
disp(val);
endfor</lang>
 
=={{header|Oforth}}==
 
<lang Oforth>: printMonths | m | Date.Months forEach: m [ m . ] ;</lang>
 
But, apply can be used instead of a loop :
<lang Oforth>#. Date.Months apply</lang>
 
=={{header|Ol}}==
Line 1,889 ⟶ 1,884:
print "I like $l\n";
}</lang>
 
=={{header|Perl 6}}==
{{works with|Rakudo|2015.10-40}}
<lang perl6>say $_ for @collection;</lang>
Perl 6 leaves off the <tt>each</tt> from <tt>foreach</tt>, leaving us with <tt>for</tt> instead. The variable <tt>$_</tt> refers to the current element, unless you assign a name to it using <tt>-></tt>.
<lang perl6>for @collection -> $currentElement { say $currentElement; }</lang>
Perl 6 will do it's best to put the topic at the right spot.
<lang perl6>.say for @collection;
for @collection { .say };</lang>
Iteration can also be done with hyperoperators. In this case it's a candidate for autothreading and as such, execution order may vary. The resulting list will be in order.
<lang per6>@collection>>.say;
@collection>>.=&infix:<+>(2); # increment each element by 2</lang>
 
=={{header|Phix}}==
Line 2,008 ⟶ 1,991:
true.
</lang>
 
 
=={{header|Python}}==
Line 2,062 ⟶ 2,044:
(displayln i))
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2015.10-40}}
<lang perl6>say $_ for @collection;</lang>
Perl 6 leaves off the <tt>each</tt> from <tt>foreach</tt>, leaving us with <tt>for</tt> instead. The variable <tt>$_</tt> refers to the current element, unless you assign a name to it using <tt>-></tt>.
<lang perl6>for @collection -> $currentElement { say $currentElement; }</lang>
Perl 6 will do it's best to put the topic at the right spot.
<lang perl6>.say for @collection;
for @collection { .say };</lang>
Iteration can also be done with hyperoperators. In this case it's a candidate for autothreading and as such, execution order may vary. The resulting list will be in order.
<lang per6>@collection>>.say;
@collection>>.=&infix:<+>(2); # increment each element by 2</lang>
 
=={{header|REBOL}}==
Line 2,161 ⟶ 2,156:
end</lang>
There are various flavours of <code>each</code> that may be class-dependent: [http://www.ruby-doc.org/core/classes/String.html#M000862 String#each_char], [http://www.ruby-doc.org/core/classes/Array.html#M002174 Array#each_index], [http://www.ruby-doc.org/core/classes/Hash.html#M002863 Hash#each_key], etc
 
 
 
=={{header|Rust}}==
Line 2,324 ⟶ 2,317:
display "`i'"
}</lang>
 
=={{header|Suneido}}==
<lang Suneido>for i in #(1, 2, 3)
Line 2,368 ⟶ 2,362:
puts "$i,$x,$y"
}</lang>
 
 
=={{header|Trith}}==
Line 2,443 ⟶ 2,436:
echo i
endfor</lang>
 
 
=={{header|Wart}}==
10,343

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.