tutorial on using nested loops in LUA programming

Source: Internet
Author: User
Tags lua min

This article mainly introduces the use of nested loops in LUA programming, is the basic knowledge in Lua's introductory learning, and needs friends to refer to the following

The LUA programming language allows the use of one loop to embed another loop. A few examples are presented below to illustrate this concept.

Grammar

Nested looping syntax statements in LUA are as follows:

The code is as follows:

For init,max/min value, increment

Todo

For init,max/min value, increment

Todo

Statement (s)

End

Statement (s)

End

A syntax statement for a nested while loop in the LUA programming language, as follows:

The code is as follows:

while (condition)

Todo

while (condition)

Todo

Statement (s)

End

Statement (s)

End

The syntax statements for the LUA programming language nesting repeat...until loops are as follows:

The code is as follows:

Repeat

Statement (s)

Repeat

Statement (s)

Until (condition)

Until (condition)

At the end of loop nesting, you can put any type of loop into any other type of loop. For example, a for loop can be within another while loop, and vice versa.

For example:

The following program uses a nested loop:

The code is as follows:

J =2

For i=2,10 do

For j=2, (i/j), 2 do

if (not (I%J))

Then

Break

End

if (J > (i/j)) Then

Print ("Value of I is", i)

End

End

End

When the above code is built and run, it produces the following results.

The code is as follows:

Value of I is 8

Value of I is 9

Value of I is 10

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.