Lua: Loops

Often when creating Lua scripts it's useful to be able to loop a certain piece of code a certain number of times. There are several 'loop' structures available to us in Lua, and we'll be iterating through some of these in this tutorial.

The While Loop

The while loop is probably the most common type of loop - it simply loops a piece of code while a certain condition is true. It is created by writing the while keyword, followed by a condition (as you'd write in an if-statement), followed by the do keyword, and then the code you want to loop, followed by the end keyword to end the structure. So conceptually, something like the following:

1
2
3
while condition do
	--Code to loop through
end

The simplest kind of while loop is one which simply loops forever (with something like the condition 1==1, or even better, just true), and then breaks out of the loop in a certain situation. This could be accomplished by using an 'if' statement inside the loop which uses the break keyword to break out of the loop in a certain situation. Note that the break keyword can be used in any of the loops which we cover in this tutorial to break out. Take for example the following:

1
2
3
4
5
6
7
8
9
10
11
name = ""

while true do
	io.write("Enter your name: ")
	name = io.read()

	if name ~= "" then
		break
	end
end
print ("Hello, " .. name)

The above would work brilliantly, however an infinite loop like this is generally considered bad practice. It would be much better to incorporate the condition we're checking for in the if-statement into the loop itself, for example the following:

1
2
3
4
5
6
7
name = ""

while name == "" do
	io.write("Enter your name: ")
	name = io.read()
end
print ("Hello, " .. name)

The For Loop

The 'for' loop is much like the 'while' loop, however focuses much more closely on iterative loops. Take the following 'while' loop for example:

1
2
3
4
5
6
i = 1 --Set 'i' to 1

while i <= 5 do --While 'i' is less than 5, do the following:
	print(i) --Output 'i' using the print function
	i = i + 1 --Add one to 'i'
end --End the whole 'while' loop

The above would output the numbers one through five, this works via adding one to the variable 'i' which we use to iterate through the loop - this process of adding one is called incrementation. The 'for' loop simplifies this iterative process by taking three different sections in its declaration, separated by commas: the first should be an iterator variable set to a beginning value, the second should be the value at which the iterator variable should indicate the end of the loop, and the third (which is optional and will default to 1 if not set) is the step value that should be added to the iterator variable each loop. The 'for' loop is created via the for keyword, followed by the three sections we just talked about, followed by the do keyword, the code you want to loop, and then the end keyword. So the loop we created using 'while' above could be compacted into the following 'for' loop:

1
2
3
for i = 1, 5, 1 do --for beginning, finish, step do
	print(i) --Output 'i' using the print function
end --End the whole 'for' loop

'For' loops are particularly useful for looping through a number of different things, as you will discover when learning about arrays.

Repeat Until

The repeat-until loop acts sort of like a 'while' loop, but with inverted functionality. So instead of looping while a condition is true, it loops while a condition is not true. Technically, this functionality could be achieved by simply putting the not operator before the same condition in a 'while' loop, however the repeat-until loop works well to achieve this type of functionality while maintaining easy-to-read code.

This loop really doesn't require much explanation, but as you can probably imagine, can be useful in a bunch of different circumstances. The loop is accomplished via writing the repeat keyword, followed by the code you want to loop, and then ended with the until keyword followed by the condition at which we want the loop to break. We could, for example, re-construct our simple functionality of counting from 1 to 5 like so:

1
2
3
4
5
6
i = 1

repeat
	print(i)
	i = i + 1
until i > 5

If you want some kind of challenge to tie off the techniques in this tutorial, try and create a basic program which takes the names of 5 people and then outputs them all in a kind of list. Think about how this task could be accomplished using each of the different types of loops we've talked about.