While loops

while runs a block again and again, as long as a condition is true:

count: int = 3
while count > 0 {
    print(count)
    count = count - 1
}
print("go!")

Output:

3
2
1
go!

The condition is checked before each run of the block. When it is false, the program goes on after the block, so a block whose condition is false from the start never runs.

Like with if, the condition has no parentheses, the block starts on the same line, and the condition must be a bool: while n is an error, write while n > 0.

Use a walk to go over a list, set or map, and while for everything else: until something happens, until the input ends, until a number is small enough.

skip and stop

skip and stop work in a while the same way as in a walk:

  • skip ends this run of the block; the condition is checked again.
  • stop ends the loop; the program goes on after it.
n: int = 0
while n < 10 {
    n = n + 1
    if n % 3 != 0 {
        skip
    }
    print(n)
}

Output:

3
6
9

while true: until stop

A loop that should go on until something inside it says so is while true, with a stop (or return) inside:

b: buffer = buffer("GET / HTTP/1.1\nHost: example.com\n\nbody")
while true {
    line: str = b.until(10)
    if line == "" {
        stop
    }
    print("line: ${line}")
}

Output:

line: GET / HTTP/1.1
line: Host: example.com

Without a stop, return or error, while true goes on until the program is stopped (Ctrl+C), which is right for a server.

In a function, while true without a stop can only be left by return (or an error), so the function needs no return after it:

fn first_square_over(limit: int): int {
    k: int = 1
    while true {
        if k * k > limit {
            return k * k
        }
        k = k + 1
    }
}
print(first_square_over(50))

Output:

64

Counting

To run a block a number of times, count with a variable:

i: int = 1
while i <= 3 {
    print("round ${i}")
    i = i + 1
}

Output:

round 1
round 2
round 3

Don’t forget to change the variable: while i <= 3 without i = i + 1 goes on forever.

Rules

  • skip and stop belong to the innermost loop, a walk or a while. In a walk inside a while, stop ends the walk.
  • A skip or stop inside a try leaves the try too, as in a walk.
  • An error raised in the block ends the loop and goes to the nearest try.
  • Variables declared in the block are new on each run and end with it: they aren’t visible in the condition or after the loop. To keep a value, declare the variable before the while.
  • while is a reserved word.
while {                 // error: while needs a condition; for a loop that goes on until stop, write while true { ... }
while x = 5 {           // error: '=' assigns a value; use '==' to compare
while count {           // error: condition must be a bool, got int

For counting from one number to another, a range is shorter: (1..20).walk(n) { ... }.

Not available yet

  • do ... while (a check at the end) and leaving an outer loop from an inner one (labeled loops): use while true with stop, and a bool.

See Limitations.