Loops/Foreach: Difference between revisions

→‎{{header|Objective-C}}: conventional syntax
(→‎Nim: Fixed typo.)
(→‎{{header|Objective-C}}: conventional syntax)
 
(23 intermediate revisions by 16 users not shown)
Line 284:
<br>
<br>[[ALGOL 68RS]] and [[Algol68toc]] have a FORALL loop, the following is equivalent to the example above:
<syntaxhighlight lang="algol68">FORALL indexc IN collection DO
print((collection[index]c," "))
OD</syntaxhighlight>
 
=={{header|Amazing Hopper}}==
<syntaxhighlight lang="c">
#include <jambo.h>
 
Main
things = {}, len list=0
Set ' 100,200,300,"Hello world!", -3,-2,-1 ' Apndlist 'things'
Let ' len list := Length(things) '
Printnl ' "\nNormal count:\n" '
For each( n, things, len list )
Printnl ' "Thing : ", n '
Next
 
Printnl ' "\n\nReverse count:\n" '
For each reverse ( n, things, len list )
Printnl ' "Thing : ", n '
Next
End
</syntaxhighlight>
{{out}}
<pre>
Normal count:
 
Thing : 100
Thing : 200
Thing : 300
Thing : Hello world!
Thing : -3
Thing : -2
Thing : -1
 
 
Reverse count:
 
Thing : -1
Thing : -2
Thing : -3
Thing : Hello world!
Thing : 300
Thing : 200
Thing : 100
 
</pre>
 
=={{header|AmigaE}}==
Line 370 ⟶ 415:
}
}</syntaxhighlight>
 
=={{header|Bait}}==
`for-in` loops work with both builtin container types (arrays and maps).
They allow to iterate the indices/keys and values.
 
<syntaxhighlight lang="bait">
fun for_in_array() {
arr := ['1st', '2nd', '3rd']
 
// Iterate over array indices and elements
for i, val in arr {
println('${i}: ${val}')
}
 
// Using only one variable will iterate over the elements
for val in arr {
println(val)
}
 
// To only iterate over the indices, use `_` as the second variable name.
// `_` is a special variable that will ignore any assigned value
for i, _ in arr {
println(i)
}
}
 
fun for_in_map() {
nato_abc := map{
'a': 'Alpha'
'b': 'Bravo'
'c': 'Charlie'
'd': 'Delta'
}
 
// Iterate over map keys and values.
// Note that, unlike arrays, only the two-variable variant is allowed
for key, val in nato_abc {
println('${key}: ${val}')
}
}
 
fun main() {
for_in_array()
println('')
for_in_map()
}
</syntaxhighlight>
 
=={{header|BASIC}}==
Line 880 ⟶ 972:
element += 42;
}</syntaxhighlight>
 
=={{header|C3}}==
C3 has a standard built-in foreach for iterating through lists.
 
<syntaxhighlight lang="c3">String[] fruits = { "Apple", "Banana", "Strawberry" };
foreach (fruit : fruits) io::printn(fruit);</syntaxhighlight>
 
=={{header|Chapel}}==
Line 1,063 ⟶ 1,161:
}</syntaxhighlight>
In E, the for ... in ... loop is also used for iterating over numeric ranges; see [[Loop/For#E]].
 
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
for i in [ 5 1 19 25 12 1 14 7 ]
print i
.
</syntaxhighlight>
 
=={{header|EchoLisp}}==
Line 1,080 ⟶ 1,185:
;; etc ... for other collections like Streams, Hashes, Graphs, ...
</syntaxhighlight>
 
=={{header|Ecstasy}}==
<syntaxhighlight lang="java">
module LoopForEach {
@Inject Console console;
void run() {
val vals = [10, 20, 30, 40];
console.print("Array of values:");
Loop: for (val val : vals) {
console.print($" value #{Loop.count + 1}: {val}");
}
 
Map<String, Int> pairs = ["x"=42, "y"=69];
console.print("\nKeys and values:");
for ((String key, Int val) : pairs) {
console.print($" {key}={val}");
}
console.print("\nJust the keys:");
Loop: for (String key : pairs) {
console.print($" key #{Loop.count + 1}: {key}");
}
 
console.print("\nValues from a range:");
for (Int n : 1..5) {
console.print($" {n}");
}
}
}
</syntaxhighlight>
 
{{out}}
<pre>
Array of values:
value #1: 10
value #2: 20
value #3: 30
value #4: 40
 
Keys and values:
x=42
y=69
 
Just the keys:
key #1: x
key #2: y
 
Values from a range:
1
2
3
4
5
</pre>
 
=={{header|Efene}}==
Line 1,109 ⟶ 1,267:
 
=={{header|Elena}}==
ELENA 56.0x :
<syntaxhighlight lang="elena">import system'routines;
import extensions;
Line 1,117 ⟶ 1,275:
var things := new string[]{"Apple", "Banana", "Coconut"};
things.forEach::(thing)
{
console.printLine(thing)
}
}</syntaxhighlight>
 
=== Using foreach statement template ===
<syntaxhighlight lang="elena">import extensions;
public program()
{
var things := new string[]{"Apple", "Banana", "Coconut"};
foreach(var thing; in things)
{
console.printLine:(thing)
}
}</syntaxhighlight>
Line 1,455 ⟶ 1,626:
 
=={{header|J}}==
<syntaxhighlight lang="j">smoutput each echo every i.10</syntaxhighlight>
0
1
2
3
4
5
6
7
8
9</syntaxhighlight>
 
That said, J's <code>for.</code> provides a "foreach" mechanism.
 
<syntaxhighlight lang="J"> {{for_i. i. y do. echo i end.}}10
0
1
2
3
4
5
6
7
8
9
</syntaxhighlight>
 
=={{header|Java}}==
Line 1,622 ⟶ 1,818:
delta
 
</syntaxhighlight>
 
=={{header|Lang}}==
<syntaxhighlight lang="lang">
$collection = fn.arrayOf(1, 2, 3)
 
# Foreach loop [Works with iterable types (text, collections [array, list])]
$ele
foreach($[ele], $collection) {
fn.println($ele)
}
 
# Foreach function
# Array: fn.arrayForEach(&arr, func)
# List: fn.listForEach(&list, func)
 
fn.arrayForEach($collection, fn.println)
</syntaxhighlight>
 
Line 1,629 ⟶ 1,842:
 
=={{header|langur}}==
A for in loop iterates over values and a for of loop iterates over indiceskeys.
<syntaxhighlight lang="langur">for .i in [1, 2, 3] {
writeln .i
Line 1,665 ⟶ 1,878:
<syntaxhighlight lang="lasso">array(1,2,3) => foreach { stdoutnl(#1) }</syntaxhighlight>
<syntaxhighlight lang="lasso">with i in array(1,2,3) do { stdoutnl(#i) }</syntaxhighlight>
 
=={{header|LDPL}}==
<syntaxhighlight lang="ldpl">data:
fruits is text list
fruit is text
 
procedure:
split "apple banana orange" by " " in fruits
for each fruit in fruits do
display fruit lf
repeat
</syntaxhighlight>
{{out}}
<pre>
apple
banana
orange
</pre>
 
=={{header|LFE}}==
Line 1,932 ⟶ 2,163:
ipsum
dolor</pre>
=={{header|Nu}}==
<syntaxhighlight lang="nu">
let l = [a b c d]
for x in $l {print $x}
</syntaxhighlight>
{{out}}
<pre>
a
b
c
d
</pre>
 
=={{header|Objeck}}==
Line 1,944 ⟶ 2,187:
{{works with|Cocoa}}
<syntaxhighlight lang="objc">NSArray *collect;
// ...
for (Type i in collect) {
NSLog(@"%@", i);
}</syntaxhighlight>
Line 1,953 ⟶ 2,196:
{{works with|Objective-C|<2.0}}
<syntaxhighlight lang="objc">NSArray *collect;
// ...
NSEnumerator *enm = [collect objectEnumerator];
id i;
while( ((i = [enm nextObject]) ) {
// do something with object i
}</syntaxhighlight>
Line 2,399 ⟶ 2,642:
next
</syntaxhighlight>
 
=={{header|RPL}}==
In RPL, « printing » a collection means sending it to the printer, if you have one. To just output the items of a list, you put them into the stack, like this:
≪ { "one" "two" "three" "four" "five" } LIST→ DROP ≫
If the idea is to do more than displaying the items, the usual way is a <code>FOR..NEXT</code> loop
≪ { "one" "two" "three" "four" "five" } → collection
≪ 1 collection SIZE '''FOR''' j
collection j GET
''(do something with the item here)''
'''NEXT'''
≫ ≫
 
=={{header|Ruby}}==
Line 2,751 ⟶ 3,005:
endfor</syntaxhighlight>
 
=={{header|V (Vlang)}}==
{{trans|go}}
 
<syntaxhighlight lang="v (vlang)">fn print_all(values []int) {
for i, x in values {
println("Item $i = $x")
Line 2,774 ⟶ 3,028:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">for (f in ["apples", "oranges", "pears"]) System.print(f)</syntaxhighlight>
 
{{out}}
Line 2,862 ⟶ 3,116:
 
{{omit from|GUISS}}
{{omit from|PL/0}}
{{omit from|Tiny BASIC}}
3

edits