6. Loops: while, for, break and continue

  • chapter 6 of 20
  • 1 min read

Besides recursion and map/fold, Dawn has ordinary loops as well: while on a condition, for x in list, and for i in a..b (a included, b excluded). break leaves the innermost loop early and continue goes to the next round; both are expressions of type Never, and neither can cross a lambda boundary.

pub fn main() -> Unit !io = {
  var sum = 0
  for i in 0..5 {
    if i == 3 { continue }
    sum = sum + i
  }
  println("${sum}")

  var n = 0
  while true {
    n = n + 1
    if n * n > 30 { break }
  }
  println("${n}")
}
7
6

See also: spec.en.md §4.7