Related Tags

lua
numeric
communitycreator

How to use a numeric for statement in Lua

Dario Vincenzo Tarantini

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 $0$ to $10$.

for i = 0, 10, 1 -- '1' is not required here
do
print(i)
end

Output

0
1
2
3
4
5
6
7
8
9
10


Explanation

The for statement in line $1$ causes line $3$ to be executed repeatedly until the value of i becomes equal to $10$.

Consequently, all the values of i in the range $0$ to $10$ are printed.

RELATED TAGS

lua
numeric
communitycreator

CONTRIBUTOR

Dario Vincenzo Tarantini
RELATED COURSES

View all Courses

Keep Exploring

Learn in-demand tech skills in half the time