Trusted answers to developer questions
Trusted Answers to Developer Questions

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 00 to 1010.

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 11 causes line 33 to be executed repeatedly until the value of i becomes equal to 1010.

Consequently, all the values of i in the range 00 to 1010 are printed.

RELATED TAGS

lua
numeric
communitycreator

CONTRIBUTOR

Dario Vincenzo Tarantini
RELATED COURSES

View all Courses

Keep Exploring