Updated April 20, 2023
Introduction to Lua Loop
Lua loop is statements that allow the user to execute a specific statement or a group of statements a multiple times. All the programming languages provide control structures that allow complicated execution paths. So, a Lua loop has various loop statements such as while loop, for loop, repeats…until loop, nested loops, infinite loop, which have their own looping requirements and the way they are used. Here, we will be learning few more ways of writing or executing loops in Lua programs. Users can execute a block of code specific number of times in sequential order i.e. first statement in a function is executed first, the second statement follows, and so on.
A General form of loop statement in most of the programming languages looks as below,
Loop statements, if returns true will execute the conditional code again, and if false will come out of the loop statement or break the statement or pass infinitely based on the condition.
Types of Loops in Lua Programming
for loop: It is a control statement that executes a sequence of statements multiple times.
Syntax:
for init, min/max value, increment/ decrement
do
statement(s)
end
Flow diagram of for loop in Lua
- In for loop, the init step is executed in the first place, and also only once. This initializing step allows the user to declare and initialize loop control variables.
- In the second step, we have the min/max value till which the loop continues to execute. Internally, a condition check is created between the initial and minimum/ maximum value.
- And then the body of the loop gets executed, then the flow of the loop jumps back to the increment/ decrement statement, which updates loop control variables.
- The body of the loop is executed again. If the condition returns true, the loop gets executed and the process repeats.
- After the condition returns false, the for loop gets terminated.
Examples
Let us discuss examples of Lua Loop.
Example #1: for loop statement in Lua
for student = 2,20,2
do
print('Even numbers from 2-20:', student);
end
Output:
So the condition, for loop has init value as 2, minimum/ maximum value as 20, and increment/ decrement value as 2 i.e. every time when loop runs, init value will increase by 2. If the init value wants to be decreased, a negative number is to be provided as a decrement value.
Example #2: for loop statement in Lua
for student = 21,1,-2
do
print('Odd numbers from 20-1:', student);
end
Output:
Here, we are printing the odd numbers, in decreasing order, hence a negative integer is given in for loop condition.
while loop: This while loop statement repeatedly executes the target statement as long as the given condition is true
Syntax:
while(condition)
do
Statement(s)
end
Flow diagram for while loop:
- Here, statement(s) can be a single statement or multiple statements.
- While loop is executed only when the condition is true.
- The condition may take any expression, true if non zero value
- If the condition is false, program control passes to the next immediate line following the loop.
Example #3: while loop in Lua
num1 = 25
while( num1 > 15 )
do
print("value of num1:", num1)
num1 = num1-1
end
Output:
Here, while loop will run until the condition is satisfied i.e. num1 should be greater than 15.
repeat…until loop: unlike for and while loops, repeat…until loop will check the condition at the bottom of the loop in Lua programming.
It is similar to while loop except do; as a do-while loop is executed at least once.
Syntax: repeat
statement(s)
until(condition)
Flowchart for repeat…until loop:
- Here, we can see that the condition statement appears at the end of the loop, so loop statements execute once before the condition gets tested.
- If the condition fails, flow of statements jumps back to do…and loop statements get executed again.
- This process gets repeated until the condition is true.
Example #4: repeat…until loop in Lua programming
global_id = 12
repeat
print("value of global id:", global_id)
global_id = global_id + 3
until( global_id > 21 )
Output:
Here, the global_id is taken as the first value and then the condition gets executed until it is true.
nested loops: Nested loops make use of more than one loop nested inside the other.
Syntax: Considering a for loop being nested.
for init, min/max value, increment/decrement
do
for init, min/max value, increment/decrement
do
statement(s)
end
statement(s)
end
Considering nested while loop
While(condition)
do
while(condition)
do
statement(s)
end
statement(s)
end
Considering nested repeat…until loop
Repeat
Statement(s)
Repeat
Statement(s)
Until(condition)
Until(condition)
Example #5: nested loops in Lua
j = 5
for i = 1,5 do
for j = 2,5,4 do
print("Value of i*j is",i*j)
end
end
Output:
So here we have worked on Nested for loop.
Conclusion
With this, we shall conclude the topic ‘Lua Loop’. We have seen what Lua loop is and the various types of loops present in Lua programming. We have gone through each type of loop with the flow structure, syntax for each loop and also implemented few examples above. Hybrid loop or also we can call it a nested loop will be much useful in daily programming. We also have an Infinite loop which will have no end to the output produced.
Recommended Articles
We hope that this EDUCBA information on “Lua Loop” was beneficial to you. You can view EDUCBA’s recommended articles for more information.