lua

loops statements

A loop is a set of instructions that are declared to be processed, if the condition you provide satifies your action in the code, then some lines of code will be executed, otherwise it will consider another chunk of code.

Lua provides three type of loops: repeat until loops, while loops and for loops.
These loops are, in general, very similar between the various programming languages, so if you come from another language, it will be easy for you to understand these concepts.

So let's get into the explanation of these.

Repeat Until Loop

As its own name could demonstrates, this type of loop repeats a chunk of code until a condition is true.
You need to consider that once you create this loop, lua will at first execute the code that is inside the loop, and then check if the condition is true.
If it is true the loop will continue until false will be generated, otherwise it stops looping the code and exits.

Let see the syntax:

Lua
repeat
	-- chunk of code to be executed
until (condition)

Very simple syntax, now let's try with a basic loop statement by declaring first a increment value:

Lua
-- Declare an index control variable
local i = 10

-- Loop statement
repeat 
	print("Prints number " .. i)
	-- Increment the value of i
	i = i + 1
until (i > 10)

Now, supposing you have called this new file "repeat.lua", save it and if you want to see what the output should be, type inside your terminal:

$ lua repeat.lua

If you get into an error, probably you have to install lua on your machine.
I suggest to read this shortly post and then come back.

An output with no errors should be like this:

Prints number 10

As you can see, at first the code inside the loop was executed once, but since the condition is no more true it stops its execution.

What can happen if you change the sign inside the condition from ">" to "<" ?
If you save and run the file, you will see that this loop keeps running and if you don't stop it, it will loop forever.

TIP: type "Ctrl" + "c" to stop the execution

While Loop

For this loop the process is a bit different because instead of running once the code inside the loop and then control if the condition it's still true, it first checks if the condition given is true and then executes its instructions.

It's syntax:

Lua
while (condition) do
	-- Code to be executed
end

Let see some code in action with this loop:

Lua
-- Declare an index control variable
local i = 1

-- Loop statement
while (i < 10) do
	print("A value of: " .. i)
	-- Increment the value of i
	i = i + 1
end

An output will look like this:

$ lua while.lua

A value of: 1
A value of: 2
A value of: 3
A value of: 4
A value of: 5
A value of: 6
A value of: 7
A value of: 8
A value of: 9 

As you can see, at nine it stops because the condition (i < 10) is no more satisfied, since 10 is not less than 10.

For Loops

This loop is very popular between loops and it is quite much more used than others.
In Lua there are a couple of types of for loops: numeric loops and generic loops.
Each of this, obviously has a different task, so let start with the numeric loops.

Numeric for Loop

It provides some control structures settings that others couldn't provide, like an initial expression , a final expression and then a step expression .

Let see how it looks like:

Lua
for index = (initial_expression), (final_expression), (step_expression) do
	-- Chunk of code to loop
end

If we go to investigate each control setting, we'll find at first an index variable, which is referred to the initial expression from where this loop starts counting, following there is a final expression which tell you how many times this loop will be executed, at final there is the step expression which let you know by how much the index will be incremented or even decremented each time the loop finishes its process.
If this value takes a number equals to 1, it can be omitted.

Let see an example:

Lua
for i = 1, 6, 1 do
	print("Number " .. i)
end

You can notice that the increment is equal to 1, so in this case it can be omitted:

Lua
for i = 1, 5 do
	print("Number " .. i)
end

This is an incrementation of the index value, how it would be if we want a decrementation of it?

Lua
for i = 5, 1, -1 do
	print("Number " .. i)
end

Pretty simple! The index value now takes a higher value than the final expression's value and the step expression has a vaule of -1, in this way you can start to decrement the value from 5 to 1.

Generic for Loop

The generic for loop allow you to iterate over an object table, which has a key-value pair inside.

Its syntax:

Lua
for k, v in (iterator)(table) do
	-- Enter stuff
end

Don't worry, the syntax as well is very simple, now i'll explain it step by step.

At first we have two variables, which are the abbreviation of "key" and "value".
This two will iterate over the table you want to provide by using a specific iterator function, depending of what type of table you are looking for:

  • If you are dealing with a numeric table, the ipairs() function will come to get rescue;
  • Otherwise, if the table you are looking for is a generic table, you need to use the pairs() function.

Let see how this new function works!
Here below i will provide two example referred to two different tables, numeric and generic, so get ready...

IPAIRS FUNCTION

Lua
-- Declare a numeric table
local numbers = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"}

-- Loop statement
for i, v in ipairs(numbers) do
	print("index(i) is equal to " .. i, "value(v) is equal to " .. v)
end

As you can see the first variable takes a value of i, which stands for index.
This is because the numeric tables have not a pair of key-value but only values inside in.

PAIRS FUNCTION

Lua
-- Declare a generic table
local brandColor = {
	MonsterEnergy = "green",
	RedBull = "red",
	Fanta = "orange",
	Pepsi = "blue",
	Rockstar = "yellow",
	Nos = "white"
}

-- Loop statement
for k, v in pairs(brandColor) do
	print("The " .. k .. " brand has a color of " .. v)
end

By saving and running this file you'll have a preview much more explained of what happen inside this code.

Hope you enjoy this article!

lua for loop while loop repeat until loop
Expand your knowledge about this topic