While
while checks its condition, runs its block if the condition is true, and goes round again:
var value = 10
while (value != 0) {
println("${value}")
value = value - 1
}
If the condition is false the first time, the block never runs.
A loop with one statement can use the colon form, with its statement on the same line:
var count = 0
while (count < 10): count = count + 1
Leaving early
break leaves the loop at once, and continue skips the rest of this time round and checks the condition again:
var count = 0
while (true) {
count = count + 1
if (count % 2 == 0): continue
if (count > 9): break
println("${count}")
}
Looping while a value is there
while can bind a value that may be absent, the way if can. The loop runs for as long as the value is
there:
class Queue {
var remaining = 3
let next() : string? {
if (remaining == 0) {
return none
}
remaining = remaining - 1
return "item"
}
}
entrypoint main() {
let queue = Queue()
while (let item? = queue.next()) {
println(item)
}
}
do ... while, which runs its block before the first check, is not in V0 yet: write the loop with while.