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. Loops are code structures used to repeat statements in scripts. This lesson will show you how to use loops in your scripts.

Prerequisites
This lesson assumes you have already completed the Conditions lesson.

Create a Lua Script with Loops
To create a Lua script with loops: ""
 * 1) Navigate to Module:Sandbox.
 * 2) 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.
 * 1) Add the following code and save the page:

Test Your Lua Script
To test your Lua script:
 * 1) Navigate to either the Module_talk:Sandbox page, the Sandbox page, or your own user or sandbox page.
 * 2) Add the following code and save the page:

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
To understand your Lua script for loop:
 * 1)   and the following code defines the variables   and  .  Both are.
 * 2)   assigns a string literal value to the variable.
 * This could also have been written with the initial variable declaration as .  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.
 * 1)   creates a loop code block that will vary the value of the variable   from   to   by.
 * 2) * 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.
 * 3) * For loops can be sequenced either in a positive or a negative direction.   would count down from   to   by.
 * 4) * If the increment value is not provided, the default increment is 1.   would count from   to   by.
 * 5)   concatenates a string literal and the current value of the variable   to.
 * 6)   ends the loop.
 * The value of the loop variable,  in this case, will be one increment greater than the stop value when the loop ends.  In this example,   will be equal to   after the loop ends.
 * 1)   returns the current value of   as the result of the function.

To understand your Lua script while loop:
 * 1)   and the following code defines the variables   and  .  Both are.
 * 2)   assigns a string literal value to the variable.
 * 3)   assigns the value   to the variable.
 * This could also have been written with the initial variable declaration as.
 * 1)   creates a loop code block that will loop while the value of   is less than.
 * 2) * 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.
 * 3) * If the while loop condition is not met, the loop code block will be skipped.
 * 4) * If the while loop condition is met, the while loop condition must be altered inside the loop, or the loop will repeat forever.
 * 5)   concatenates a string literal and the current value of the variable   to.
 * 6)   takes the current value of , adds  , and stores the result in.
 * 7)   ends the loop.
 * 8)   returns the current value of   as the result of the function.

To understand your Lua script repeat loop:
 * 1)   and the following code defines the variables   and  .  Both are.
 * 2)   assigns a string literal value to the variable.
 * 3)   assigns the value   to the variable.
 * 4)   creates a loop code block that will loop until the ending condition is true.
 * 5) * 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.
 * 6) * 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.
 * 7) * If the repeat loop condition is met, the repeat loop condition must be altered inside the loop, or the loop will repeat forever.
 * 8)   concatenates a string literal and the current value of the variable   to.
 * 9)   takes the current value of , adds  , and stores the result in.
 * 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.
 * 11)   returns the current value of   as the result of the function.

Conclusion
Congratulations! You've now created, tested, and understood a Lua script with loops. Continue on to the Functions lesson.