Control structures enable us to manage the flow of execution in our program. They are the basic elements of algorithms and code structure. When you think about how something should be done in your code, you must consider, among other things, loops, if
statements, data structures, and functions. We can choose from three types of loops: while
, repeat
, and for
. Additionally, we can make use of if
statements.
Each control structure accepts an expression or value, which results either in false
or true
. If the value is not false
or nil
, then it evaluates as true
. Luau considers zero and an empty string as true
.
while
loop:
The while
loop looks as follows:
local counter = 0 while counter < 3 do print(`counter = {counter}`) counter += 1 end
Outputs:
counter = 0 counter = 1 counter = 2
In line 2, counter < 3
is an expression evaluated each time the loop executes. When counter
is equal to 3 or higher, the loop will stop executing. If you use an expression that always results in true
, then such a while
loop will never end on its own, leading to an infinite loop. It is important to include a mechanism within the loop to eventually break out of it.
repeat
loop:
The repeat
loop is similar to the while
loop, but it evaluates the expression at the end of each iteration. This means that the loop will always execute at least once, even if the expression is initially false
. Here’s an example:
local counter = 0 repeat print(`counter = {counter}`) counter += 1 until counter >= 3
Outputs:
counter = 0 counter = 1 counter = 2
In this example, the code block within the repeat
loop is executed first, and then the expression counter >= 3
is evaluated. If the expression is false
, the loop will continue to execute until the expression becomes true
.
for
loop:
The for
loop provides a convenient way to iterate over a range of values. It consists of an initialization, a condition, and an update. Here’s an example that prints numbers from 1 to 3 using a for
loop:
for i = 1, 3 do print(`i = {i}`) end
Outputs:
i = 1 i = 2 i = 3
In this example, the loop starts with i
initialized to 1. The loop will continue executing as long as i
is less than or equal to 3. After each iteration, the value of i
is incremented by 1. The loop terminates when the condition i <= 3
becomes false
.
if
statement:
The if
statement allows us to execute a block of code conditionally based on a specified expression. Here’s an example:
local x = 5 if x > 0 then print("x is positive") elseif x == 0 then print("x is zero") else print("x is negative") end
Outputs:
x is positive
In this example, the code checks the value of x
and executes the corresponding block of code based on the condition. If the condition x > 0
is true, the first block of code is executed. If it is false, the condition x == 0
is checked, and if true, the second block of code is executed. If none of the conditions are true, the else
block is executed.
These are some of the basic control structures in Luau that allow you to control the flow of your program based on conditions and iterations. Understanding and using these structures effectively will help you write more powerful and flexible code.