Loops/Foreach: Difference between revisions

→‎{{header|Objective-C}}: conventional syntax
imported>Arakov
(→‎{{header|Objective-C}}: conventional syntax)
 
(3 intermediate revisions by 2 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,795 ⟶ 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,140 ⟶ 2,187:
{{works with|Cocoa}}
<syntaxhighlight lang="objc">NSArray *collect;
// ...
for (Type i in collect) {
NSLog(@"%@", i);
}</syntaxhighlight>
Line 2,149 ⟶ 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>
3

edits