tutorial on the use of for-loop statements in Lua

Source: Internet
Author: User
Tags lua min

This article introduces the use of a for-loop statement in Lua, which is the basic knowledge of LUA's introductory learning, and needs friends to refer to the

A For loop is a circular control structure that effectively writes a specific number of loops that need to be performed.

Grammar

The syntax for the for loop of the LUA programming language is:

The code is as follows:

For init,max/min value, increment

Todo

Statement (s)

End

The following is the process of controlling a loop:

The initialization step is executed first and only once. This step allows you to declare and initialize any of the loop control variables.

Next is Max/min, which is the maximum or minimum value until the loop continues to execute. It creates a comparison between the initial and maximum/minimum values of a condition check internally.

After the For loop body executes, the control flow jumps back to the increment/decrement declaration. This statement can update any loop control variable.

Conditions are now recalculated for evaluation. If this is true, the loop executes and repeats the process (the loop body, then adds one step, then the condition). If the condition is false, the loop terminates.

Flow chart:

The code is as follows:

For i=10,1,-1

Todo

Print (i)

End

When the above code is built and executed, it produces the following results:

The code is as follows:

10

9

8

7

6

5

4

3

2

1

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.