Lua/Loops
Lua modules based on the Scribunto/Lua extension are stored in resource pages using the Module: namespace. Each module uses a table to hold functions and variables, and that containing table is returned at the end of the module code.[1] Loops are code structures used to repeat statements in scripts. This lesson will show you how to use loops in your scripts.
Prerequisites
[edit | edit source]This lesson assumes you have already completed the Conditions lesson.
Create a Lua Script with Loops
[edit | edit source]To create a Lua script with loops:
- Navigate to Module:Sandbox.
- Clear all existing code.
- It's a sandbox. Everyone is free to play in the sandbox. But if you find another user is actively editing the sandbox at the same time, you may also use Module:Sandbox/Username, where Username is your Wikiversity username.
- Add the following code and save the page:
local p = {} function p.forloop() local i local result result = ';for\n' for i = 2, 10, 2 do result = result .. ":i = " .. i .. '\n' end return result end function p.whileloop() local i local result result = ';while\n' i = 2 while i <= 10 do result = result .. ":i = " .. i .. '\n' i = i + 2 end return result end function p.repeatloop() local i local result result = ';repeat\n' i = 2 repeat result = result .. ":i = " .. i .. '\n' i = i + 2 until i > 10 return result end return p
Test Your Lua Script
[edit | edit source]To test your Lua script:
- Navigate to either the Module_talk:Sandbox page, the Wikiversity:Sandbox page, or your own user or sandbox page.
- Add the following code and save the page:
{{#invoke:Sandbox|forloop}} {{#invoke:Sandbox|whileloop}} {{#invoke:Sandbox|repeatloop}}
The result should be:
- for
- i = 2
- i = 4
- i = 6
- i = 8
- i = 10
- while
- i = 2
- i = 4
- i = 6
- i = 8
- i = 10
- repeat
- i = 2
- i = 4
- i = 6
- i = 8
- i = 10
Understand Your Lua Script
[edit | edit source]To understand your Lua script for loop:
local
and the following code defines the variablesi
andresult
. Both arenil
.result = ';for\n'
assigns a string literal value to the variableresult
.- This could also have been written with the initial variable declaration as
local result = ';for\n'
. There is no difference in functionality, only in the author's preference for coding style. Some programming languages require variables to be declared before they are used, resulting in some developers preferring to see variables declared before they are used.
- This could also have been written with the initial variable declaration as
for i = 2, 10, 2 do
creates a loop code block that will vary the value of the variablei
from2
to10
by2
.- For loops are used when the number of loops desired can be easily determined before the loop is entered. They are typically counting loops of some type.
- For loops can be sequenced either in a positive or a negative direction.
for i = 10, 2, -2 do
would count down from10
to2
by2
. - If the increment value is not provided, the default increment is 1.
for i = 1, 10 do
would count from1
to10
by1
.
result = result .. ":i = " .. i .. '\n'
concatenates a string literal and the current value of the variablei
toresult
.end
ends the loop.- The value of the loop variable,
i
in this case, will be one increment greater than the stop value when the loop ends. In this example,i
will be equal to12
after the loop ends.
- The value of the loop variable,
return result
returns the current value ofresult
as the result of the function.
To understand your Lua script while loop:
local
and the following code defines the variablesi
andresult
. Both arenil
.result = ';while\n'
assigns a string literal value to the variableresult
.i = 2
assigns the value2
to the variablei
.- This could also have been written with the initial variable declaration as
local i = 2
.
- This could also have been written with the initial variable declaration as
while i <= 10 do
creates a loop code block that will loop while the value ofi
is less than10
.- While loops are used when the number of loops desired is determined within the loop, such as repeating until the end of a string is reached. A while loop is used here to demonstrate the comparison in code structure between for, while, and repeat loops.
- If the while loop condition is not met, the loop code block will be skipped.
- If the while loop condition is met, the while loop condition must be altered inside the loop, or the loop will repeat forever.
result = result .. ":i = " .. i .. '\n'
concatenates a string literal and the current value of the variablei
toresult
.i = i + 2
takes the current value ofi
, adds2
, and stores the result ini
.end
ends the loop.return result
returns the current value ofresult
as the result of the function.
To understand your Lua script repeat loop:
local
and the following code defines the variablesi
andresult
. Both arenil
.result = ';repeat\n'
assigns a string literal value to the variableresult
.i = 2
assigns the value2
to the variablei
.repeat
creates a loop code block that will loop until the ending condition is true.- Repeat loops are used when the number of loops desired is determined within the loop, such as repeating until the end of a string is reached.
- The difference between a while loop and a repeat loop is that the while loop may not execute if the condition is not met. A repeat loop always executes at least once.
- If the repeat loop condition is met, the repeat loop condition must be altered inside the loop, or the loop will repeat forever.
result = result .. ":i = " .. i .. '\n'
concatenates a string literal and the current value of the variablei
toresult
.i = i + 2
takes the current value ofi
, adds2
, and stores the result ini
.until i > 10
sets the condition that is tested to end the loop. If the condition is false, the loop repeats. If the condition is true, the loop terminates.return result
returns the current value ofresult
as the result of the function.
Conclusion
[edit | edit source]Congratulations! You've now created, tested, and understood a Lua script with loops. Continue on to the Functions lesson.