Understanding Loops In Lua
In this article, we will learn different types of Lua loops, their syntax, and how they can be used in various scenarios with examples.
In computer programming, a loop involves repeatedly executing a piece of code until a specific condition is met.
Loops automate repetitive tasks and iterate over a sequence of values or elements without having to write the same code over and over again.
In Lua, there are several types of loops that provide different ways to iterate over a collection of values or execute a block of code for a specific number of times.
- While Loop
- Repeat-Until Loop
- Numeric for loop
- Generic for loop
We will examine each loop in detail.
While Loop In Lua
This type of loop executes a block of code repeatedly while a certain condition is true.
The syntax of a while loop in Lua is:
while condition do -- block of code end
The following example prints the numbers from 1 to 10 using a while loop in Lua:
This type of Lua loop is similar to a while loop, but the condition is checked at the end of the loop, so the block of code will execute at least once.
repeat -- block of code until condition
In the below example, numbers from 1 to 10 are printed using a repeat until loop in Lua:
This type of loop executes a block of code a fixed number of times.
There are two ways to use the for loop in Lua:
- The numeric for loop.
- The generic for loop.
Numeric for loop
This type of Lua loop is a variant of the for loop, used specifically for iterating over a sequence of numbers.
for var = start_value, end_value, step_value do -- block of code end
Following example showcases the implementation of the for loop in Lua:
Generic for loop
This type of Lua loop is used to iterate over a sequence of values, such as elements in a table.
for key, value in ipairs(table_name) do -- block of code end
This example implements the generic for loop in Lua:
Loop Control Statement
A programming language provides built-in loop control statements that allow you to control the execution of loops.
The purpose of these statements is to modify the behavior of loops in some way, for example, to exit a loop prematurely or jump to a certain point in the loop’s code.
In Lua, the two built-in loop control statements are break and goto which are explained below:
Break Statement In Lua
Break statement is used to exit loops immediately.
The loop is terminated early when it is executed, skipping the remaining iterations.
Below example implements the break statement in a for loop:
A goto statement transfers program control to a specific point in the code.
It allows us to create loops, implement complex flow control, or jump out of loops prematurely.
The goto statement is demonstrated in the example below:
Infinite loops In Lua
In computer programming, an infinite loop can be described as a loop that continues to execute indefinitely without ever terminating.
In other words, the loop will repeat the same code block indefinitely.
Programmers often use infinite loops when they need to continuously perform a task until some condition is met.
A break statement can be used to exit an infinite loop.
The following example illustrates the use of infinite loop in Lua: