In Lua, all control structure blocks are ended with end tags.
The result of an expression of the control structure can be any value, and in Lua only false and nil are false and the other values are true.
1. If
Copy Code code as follows:
If condition Then
...
End
If condition Then
...
Else
...
End
If condition Then
...
ElseIf conditions Then
...
Else
...
End
The then keyword is used to mark the beginning of a conditional code block.
2. Repeat
Copy Code code as follows:
Repeat
...
Until conditions
The Repeat keyword is used to mark the beginning of a block of code, until to mark the end of a block of code. The conditional expression of the control structure is located after the until keyword.
3. While
Copy Code code as follows:
While condition
Todo
...
End
The repeat and while control structures are similar in that they can iterate through a piece of code until a certain condition is met.
Repeat control structure in the final judgment condition, the code block will be executed at least once.
While the control structure first judges the condition, if true, then the code block is executed, or it may never be executed.
The while control structure uses the DO keyword to mark the beginning of the program block.
4. For
Copy Code code as follows:
For variable = initial, end value, step
Todo
...
End
For variable 1, variable 2, ... Variable n in table or enum function
Todo
...
End
The number of cycles is determined only at the first execution time. The initial value, the end point, and the step is computed only once, and before the loop executes.
The variables in the loop structure are local variables that are cleared once the loop body is finished.
5. Break
The break statement is used to exit the current loop. Cannot be used outside the circulation body.
6. Return
Returns are used to return results from a function. A function naturally ends with a default return.