A concise summary of control structures (Process Control) in Lua _lua

Source: Internet
Author: User
Tags lua

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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.