Skip to main content

While Loop

To write a while loop, you can use the while keyword with this syntax:

while ([condition]) {    [code]}

As the name implies, while the condition is truthy, the code will execute. Be careful with this, as sometimes this can cause an infinite loop.

Here's an example using a while loop:

var a = 0
while (a < 5) {    print(a)    a += 1}

In this example, the loop will print 0, 1, 2, 3, and 4. It doesn't print anything after this because a is not less than 5.