Cycling and select

Lua provides the following types of circulation loop to handle the demand.

Numbering

Circulation type

description

1

while loop

Given condition repetition statement or group of statements is true, it is executed before the loop test conditions.

2

for loop

Repeatedly execute a series of statements, and management code abbreviation for the loop variable.

3

repeat ... unti cycle

Repeat statement group until satisfied untilcondition.

4

Nested loop

Or more cycles may be used in any other cycle, such as: while, foror do..whilecyclic.

Loop control statement

Loop control statements executed sequentially change from normal. When executing out of scope, it will automatically destroy all objects created in that scope.

Lua supports the following control statements.

Numbering

Control statements

description

1

break statement

Terminates the loop and transfer execution to the statement after the loop or switch.

Infinite loop

If the condition never becomes false, the loop becomes an infinite loop. whileLoop cycles are typically used for this purpose. If given a direct condition is true, it will always be executed. You can use breakthe statement to interrupt the endless loop.

while( true )do
   print("This loop will run forever.")end

 

Boolean assumed Lua programming language trueand non-nilany combination of values is true, if it is a Boolean falseor nilwill be assumed falsevalue. Note that, in Lua, zero will be considered true.

Lua programming language provides the following types of decision-making statement -

Numbering

Decision Statement

description

1

The if statement

ifStatements by the Boolean expression followed by one or more statements.

2

if ... else statement

ifBehind the statement can be followed by an optional elsestatement, the Boolean expression for the statement falseexecution time.

3

Nested if statements

In one ifor else ifuse another statement ifor else ifstatements.

 

 

Control structure in Lua

Lua provides a traditional, small control structure, comprising means for performing the if condition for iterative while, repeat, and for. All control structures are interested in an explicit terminator: if, for while, and as the end to end, repeat as to until the end.
1. if then else

if a < 0 then a = 0 end
if a < b then return a else return b end
if line > MAXLINES then
    showpage()
    line = 0end

 

To write nested if, you can use elseif.

if op == "+" then
    r = a + b elseif op == "-" then
    r = a - b elseif op == "*" then
    r = a * b elseif op == "/" then
    r = a / b else
    error("invalid operation")end

Because Lua does not support the switch statement, so this series of if-else if the code is very common.

While 2.
Lua first test while conditions. If the condition is false, then the loop ends; otherwise, Lua loop body is executed, and the process is repeated.

local i = 1 while a[i] do
    print(a[i])
    i = i + 1 end

3. repeat
a sentence repeat-until loop which is repeatedly executed until the end condition is true. Testing is done after the loop, so the loop will execute at least once.

- The first line of input print content is not empty repeat
    line = io.read()until line ~= ""print (line)

Unlike most other languages ​​different is that in Lua, the role of a local variable in the loop body statement to include a conditional test:

local sqr = x/2repeat
    sqr = (sqr + x/sqr)/2
    local error = math.abs (sqr ^ 2 - x) until error <x / 10000 - The error still accessible

4. The digital type for ( numeric for )
for circulating in two forms: a digital type for (numeric for) and generic for (generic for).
Numeric for the following syntax:

for var=exp1,exp2,exp3 do
    <Executable> end

var exp1 to change from exp2, each change to exp3 as a step (step) var increments, and perform a "executable." The third expression exp3 is optional, if not specified, then, Lua step will default to 1. The following is a typical example of such a cycle:

for i=1,f(x) do print(i) endfor i=10,1,-1 do print(i) end

If you do not want to set the upper limit of the loop, you can use constants math.huge:

for i=1,math.huge do
    if (0.3*i^3 - 20*i^2 - 500 >= 0) then
        print(i)
        break
    End end

The generic for ( Generic for )
generic for loop through all values to an iterator (Iterator) function:

- a print array for all values ​​for i, v in ipairs (a) do print (v) end

Lua foundation library provides ipairs, then a function for which iteration through the array. In each cycle, i is an index value will be assigned, and v is given corresponds to the index of the array element value. Here is another similar example that demonstrates how to traverse a table of all the key:

- all of the print table t keyfor k in pairs (t) do print (k) end

The standard library provides several iterators, comprising:
    for each iteration file line (io.lines);
    iteration table element (pairs);
    ; iterative array elements (ipairs)
    iteration word string (string. gmatch), etc.

 

Lua programming language statements may be repeated for loop statement specifies the number of repetitions can be controlled in the for statement.

   Lua programming language for the statement two categories: value for the cycle, the generic for loop

 

Value for loop

    for var=exp1,exp2,exp3 do 

        <Executable>  

    end 

 

    var exp1 to change from exp2, as each change in increments of exp3 var, and perform a "executable." exp3 is optional, if not specified, the default is 1.

 

Examples

    for i=1,f(x) do

        print(i)

    end

    

    for i=10,1,-1 do

        print(i)

    end

 

    For three expressions disposable evaluated before the start of the cycle, it will no longer be evaluated. The above example f (x) is executed only once before the start of the cycle, which results in a later cycle.

 

   Verify the following:

    #!/usr/local/bin/lua 

    function f(x) 

        print("function") 

        return x*2  

    end 

    for i=1,f(5) do print(i) 

    end

 

   Examples of the above output is:

    function

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

 

 

Generic for loop

    Generic values ​​for loop to iterate through all the iterator a function similar to the java foreach statement. Lua programming language generic for loop syntax:

 

    - all the values ​​of a print array 

    for i,v in ipairs(a)

        to print (in)

    end 

 

   i is an array index value, v is the index of the corresponding array element value. Lua ipairs is provided an iterator function for iterative array.

 

Examples

    #!/usr/local/bin/lua 

    days = {"Suanday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"} 

    for i,v in ipairs(days) do  print(v) end 

 

    Examples of the above output is:

    Suanday

    Monday

    Tuesday

    Wednesday

    Thursday

    Friday

    Saturday

 

Guess you like

Origin www.cnblogs.com/gd-luojialin/p/10962729.html