In Lua, the
while keyword is used to repeat a piece of code given a certain condition.
The syntax is as follows.
while(condition) do -- code end
The illustration below demonstrates how the
while keyword works.
The code below uses the
while statement to print the numbers from to .
i = 1 while(i <= 10) do print("number ", i) i = i + 1 end
number 1 number 2 number 3 number 4 number 5 number 6 number 7 number 8 number 9 number 10
while statement in line causes the lines after it to be executed repeatedly until the value of
i becomes greater than . Consequently, all the values of
i in the range to are printed.
View all Courses