While loop

Syntax

while <cond> <body>
  • cond is a expression

  • body is a statement

Examples

One-line loop

var i = 0
while i < 10 { i++ }

puts i    // 10

Single statement, no block

var i = 0
while i < 10: i++

20th fibonacci number

var n = 20
var a = 1, b = 0, c

while n-- > 0 {
    c = a
    a = a + b
    b = c
}

puts b

Infinite loop

while true {
    // Do sonething
}

Avoid to use infinite loop, which will make your program stuck.

Last updated