For Loop in Lua

The for loop is used to execute the specified code a number of times in Lua.


The variable here is the name of a variable that will be incremented in steps of step to finish. The default step is 1, so you don’t have to specify the third parameter. For example, if we need to set the 10 steps, we will write the following.

In this example, it will display a inclusive number from 1 to 10.

The increment is not necessarily an integer, it can be fractional (0.5) or negative (-1). This allows you to make cycles in which the incremental counter is not increasing, rather decreases as shown in the example below.

In this example, the number is displayed from 10 to 1 with increments of 0.5.