For loops
Zap has two for forms: a C-style loop and a for ... in loop. Both use a
block and support break and continue.
C-style for
Section titled “C-style for”A C-style loop contains an initialization, a Bool condition, and an update:
for var index: Int = 0; index < 5; index = index + 1 { println(toString(index));}The order is:
- initialize
index; - check the condition;
- run the body;
- run the update;
- check the condition again.
The loop variable is scoped to the loop:
for var index: Int = 0; index < 3; index = index + 1 { println(toString(index));}
// `index` is not available here.Choose the update explicitly
Section titled “Choose the update explicitly”The update can move by more than one or count backwards:
for var even: Int = 0; even <= 10; even = even + 2 { println(toString(even));}
for var countdown: Int = 3; countdown > 0; countdown = countdown - 1 { println(toString(countdown));}The compiler requires the update target to be the same variable introduced by the loop initializer.
Iterate over an array
Section titled “Iterate over an array”Use for value in values when the index is not needed:
var ports: [3]Int = {80, 443, 8080};for port in ports { println(toString(port));}The loop visits elements in index order and binds one value for each iteration.
Iterate over a list
Section titled “Iterate over a list”The same form works with List<T>:
var jobs = new List<String>();jobs.push("compile");jobs.push("test");
for job in jobs { println(job);}Use an indexed loop when you need to replace an element or compare its
position. See List for collection operations.
break and continue
Section titled “break and continue”for value in ports { if value == 443 { continue; } if value > 500 { break; } println(toString(value));}Both statements affect the nearest loop. continue proceeds to the next
element or update step; break leaves the loop.
Choose between while and for
Section titled “Choose between while and for”Use for when the counter or collection describes the iteration. Use
while when the next iteration depends on state produced by
the body or by an external operation.