 |
 | | |
"if-then" "if-then-else" | | |
|
The "if-then" statement checks the result of a condition and executes a block of code when that condition evaluates to "true".
The syntax for this type of statement begins with the "if" keyword followed by the specific condition (in the form of a Boolean expression).
After the condition, there should be a block of code enclosed by the keywords "then" and "end".
C programmers should know that Lua does not have "switch" statements.
Instead, we use the "elseif" keyword to branch the flow of our program into multiple cases, each with its own condition.
The final, "else" case occurs when all of the previous conditions have failed.
b = 2
if ( b == 1 ) then
-- b equals 1
elseif ( b == 2 ) then
-- b equals 2
else
-- b is neither 1 or 2
end
|
 |
a = 2
if ( a > 1 ) then
-- a > 1 is true
end
|
 | | |
"while" loop | | |
|
Loops are language statements that allow you to execute of a chuck of code any given number of times.
Let's say that we want to create a script that counts or iterates from the number 1 to 100.
The next code example shows how it would look using the "while" loop.
|
 |
a = 1
while ( a < 100 ) do
-- repeated 100 times
a = a + 1
end
|
 | | |
| | |
|
We begin by assigning 1 to some variable, in this case called a.
Then comes the "while" keyword followed by the loop's condition (a < 100).
Lastly, there is a block of code between the keywords "do" and "end".
When this script is executed, the computer will iterate over the block of code for as long as the condition in parentheses is true.
Each time the block of code is iterated, the value of variable a will be incremented or increased by 1.
It begins at 1 and will continue up to 100 at which point a < 100 will no longer be true.
From then on, the loop will exit and the program will continue running sequentially.
|
 |
|
 | | |
Numeric "for" loop | | |
|
The numeric "for" loop is somewhat different in syntax from "while".
First, we have the "for" keyword and then a three-part statement.
The first of these three parts must initialize what we call the control variable of the loop.
The second part is the final value of the control variable and the third part is the "step".
The step is optional and if not specified, a default step value of one (1) will be used.
On every iteration of the loop, the control variable is incremented by the given step.
The three parts of the loop statement are separated by commas and are evaluated once, upon entering the loop.
Loops will repeat until their control variable reaches the predefined final value.
|
 |
for a = 0, 100, 1 do
-- repeated 100 times
end
|
 | | |
"break" | | |
|
Interrupting loops can be done at any time using the "break" keyword.
Here is an example of a "while" loop whose condition is the constant value true.
In Boolean logic, true always equals true so there is absolutely nothing we can do to affect this condition.
We can, however, stop the loop exactly when we want by using the "break" keyword and a simple "if-then" statement check.
|
 |
a = 1
while ( true ) do
if ( a > 50 ) then
-- loop is interrupted
-- after 50 iterations
break
end
a = a + 1
end
|
 | | |
|
Next: Understanding operators
|
 | | |
|
|