How to Use nested loops in Lua Programming

Source: Internet
Author: User

How to Use nested loops in Lua Programming

This article describes how to use nested loops in Lua programming. It is the basic knowledge of Lua beginners. For more information, see

The Lua programming language allows one loop to embed another loop. The following describes several examples to illustrate this concept.

Syntax

The nested loop syntax statement in Lua is as follows:

The Code is as follows:

For init, max/min value, increment

Do

For init, max/min value, increment

Do

Statement (s)

End

Statement (s)

End

The nested while LOOP syntax statement in Lua programming language is as follows:

The Code is as follows:

While (condition)

Do

While (condition)

Do

Statement (s)

End

Statement (s)

End

The syntax statement of the Lua programming language nested repeat... until loop is as follows:

The Code is as follows:

Repeat

Statement (s)

Repeat

Statement (s)

Until (condition)

Until (condition)

At the end of loop nesting, it should be noted that any type of loop can be put into any other type of loop. For example, a for loop can be in 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 created and run, it will produce 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.