Loops/Foreach: Difference between revisions

Add ed example
imported>Arakov
(Add ed example)
 
(6 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,191 ⟶ 1,238:
5
</pre>
 
=={{header|Ed}}==
 
Print all (newline-separated) lines in the file.
 
<syntaxhighlight lang="sed">
,p
</syntaxhighlight>
 
=={{header|Efene}}==
Line 1,795 ⟶ 1,850:
 
=={{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] {
for i in [1, 2, 3] {
writeln .i
}
 
val .abc = "abc"
 
for .i in .abc {
writeln .i
}
 
for .i of .abc {
writeln .abc[.i]
}
 
for .i in .abc {
writeln cp2s .(i)
}
}</syntaxhighlight>
</syntaxhighlight>
 
{{out}}
Line 2,140 ⟶ 2,197:
{{works with|Cocoa}}
<syntaxhighlight lang="objc">NSArray *collect;
// ...
for (Type i in collect) {
NSLog(@"%@", i);
}</syntaxhighlight>
Line 2,149 ⟶ 2,206:
{{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,252 ⟶ 2,309:
=={{header|Pascal}}==
See [[Loops/Foreach#Delphi | Delphi]]
 
=={{header|PascalABC.NET}}==
<syntaxhighlight lang="delphi">
##
foreach var s in |'Pascal','ABC','.NET'| do
Print(s);
</syntaxhighlight>
 
=={{header|Perl}}==
110

edits