Loops/Foreach: Difference between revisions

→‎{{header|Objective-C}}: conventional syntax
imported>Brie
(Add Nu)
(→‎{{header|Objective-C}}: conventional syntax)
 
(5 intermediate revisions by 4 users not shown)
Line 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 1,220 ⟶ 1,267:
 
=={{header|Elena}}==
ELENA 56.0x :
<syntaxhighlight lang="elena">import system'routines;
import extensions;
Line 1,228 ⟶ 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,782 ⟶ 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 2,127 ⟶ 2,187:
{{works with|Cocoa}}
<syntaxhighlight lang="objc">NSArray *collect;
// ...
for (Type i in collect) {
NSLog(@"%@", i);
}</syntaxhighlight>
Line 2,136 ⟶ 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,968 ⟶ 3,028:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">for (f in ["apples", "oranges", "pears"]) System.print(f)</syntaxhighlight>
 
{{out}}
3

edits