How to use a numeric for statement in Lua
In the Lua programming language, we can use various control statements. This includes the numeric for statement.
Syntax
The syntax is as follows.
for Name ‘=’ initial_value ‘,’ limit [‘,’ step]
do
-- code
end
Parameters
The initial_value is used to specify the value on which our counter will be initialized.
The limit is used to specify the final number of our iteration.
The step (not mandatory) is used to specify the number that will be added to our counter each iteration. By default, it is 1.
Code
In this example, we will see how to enumerate numbers from to .
for i = 0, 10, 1 -- '1' is not required heredoprint(i)end
Output
0
1
2
3
4
5
6
7
8
9
10
Explanation
The for statement in line causes line to be executed repeatedly until the value of i becomes equal to .
Consequently, all the values of i in the range to are printed.