While Loop in Lua

In Lua, unlike the for loop that sets the given number of steps, the while loop is executed until the specified logical condition is true. In General, the cycle is:

Initially, the condition is checked, if it is true, the loop body is executed. Before the end condition is checked again and if it is still true, then the cycle repeats. Otherwise, the cycle ends.

You can easily get an infinite loop if you do not change the values of variables that used in the condition:

In the example above, i will always be less than 10 because its value will not change within the loop, the loop never ends. Note that this is a logical fallacy, but not an error in terms of the compiler. The correct example is shown below.

Here inside the loop, the value of variable i is increased by 0.5, and the condition is checking that i is less than or equal to 10. This will display the inclusive number 5, 5.5, 6, 6.5 and up to 10.