# Control flow syntax

You can use two minus signs to indicate that the current line behind is a comment, which is not treated as code

# if/else

Conditional judgment syntax, followed by true and false values, such as nil/false are false values, other values are true values, else indicates that the code to be executed when the condition is not met is an optional statement, for example

var a = {}
if false then       
    print('false')
elseif nil then
    print('ni')
elseif 2 > 1 then
    print('2>1')
elseif a then
    print('a')
else
    print('else')
end

或者
if a then
    print('a')
end

# for

Loop through the statement,

There are two kinds of for syntax, the first one is for v = e1, e2, e3 do block end, where e1 is the initial value of v, e2 is the end value of v (the end of the loop when v exceeds e2), and e3 is every time Traversing the increasing value of v, e3 can be a negative number, e3 is optional, the default is 1

such as

for v=1,10,2 do
    print(v)
end

There is also a for syntax that is for var_1, •••, var_n in f, step do block end. var_1 to var_n are several variable names used to loop. Each step traverses step and var_1 to var_n The value is passed to the function f as a parameter, and the result is assigned to var_1,..., var_n. It loops until the return value of f(step, var_1, ..., var_n) is nil

for example

var a

let f = function(s: number, v: number)
    if not v then
        return 1
    elseif v > 10 then
        return nil
    else
        return v + s
    end
end

for a in f, 2 do
    print(a)
end

let t1 = [1,2,3]
for k: int, v: int in pairs(t1) do   -- 这里的pairs的用来遍历Map<T>, Array<T>, table的全局函数,按key排序遍历
    pprint(k, v)
end

# while/break

Syntax structure while exp do block end, you can continue to execute the block code block if the exp condition is met, and you can also use the break statement to jump out of the loop

such as

var a = 1
while a < 10 do
    a = a + 1
    print(a)
    if a > 8 then
        break
    end
end

# repeat

The grammatical structure repeat block until exp, which repeatedly executes the code block of the block until exp is true, and you can also use the break statement to jump out of the loop

such as

local a = 1
repeat
    a = a + 1
    print(a)
until a >= 10

# goto

The label can be defined by the syntax of labelName, and then jumped by goto labelName elsewhere in the function to achieve unconditional transfer of control flow.

such as

var i = 0 
s1 
do
    print(i)
    i = i+1
end
if i>3 then
    goto end_of_file
end

goto s1

end_of_file
print("this is end")

# and

Logical operators, if the left and right expressions are both true values, the result is the true value, such as true and false results are false, true and true results are true

# or

Logical operator, one of the two left and right expressions is the true value, the result is the true value, such as true of false results are true values, false or false results are false

# not

Logical operator, opposite to the Boolean value of the expression on the right, such as not false is true