Loops/Foreach: Difference between revisions

added map/dictionary and range iteration
(Add Ecstasy example)
(added map/dictionary and range iteration)
Line 1,089:
{
val vals = [10, 20, 30, 40];
console.println("Array of values:");
Loop: for (val val : vals)
{
console.println($" value #{Loop.count + 1}: {val}");
}
 
Map<String, Int> pairs = ["x"=42, "y"=69];
console.println("\nKeys and values:");
for ((String key, Int val) : pairs)
{
console.println($" {key}={val}");
}
console.println("\nJust the keys:");
Loop: for (String key : pairs)
{
console.println($" key #{Loop.count + 1}: {key}");
}
 
console.println("\nValues from a range:");
for (Int n : 1..5)
{
console.println($" {n}");
}
}
Line 1,099 ⟶ 1,118:
Output:
<syntaxhighlight>
Array of values:
value #1: 10
value #21: 2010
value #32: 3020
value #43: 4030
value #14: 1040
 
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
</syntaxhighlight>
 
162

edits