For Loop
A for loop is a refactored while loop with a different syntax:
for ([variable]; [condition]; [increment]) { [code]}
Essentially, it is just a while loop worded differently for readibility. The while loop below does the same as the for loop:
[variable]while ([condition]) { [code] [increment]}
The variable can be a variable declaration, or an existing variable. The loop will execute while the condition is true. The increment is executed at the end of each loop. An example of a for loop is:
for (var i = 0; i < 5; i += 1) print(i)
This loop will output 0
, 1
, 2
, 3
, 4
. You can also remove the variable clause if you have a pre existing variable, for example:
var a = 0for (; a < 5; a += 1) print(a)