Statement#
Loop Statement While#
while (exp)
do
exp
…
end
Function Definition#
function fun(parameter list)
statement list
...
return exp
end
Example:
function fun1(a,b,c)
...
return a+b+c
end
Parameter type: The actual parameter takes effect
Return value type: The value depends on the actual returned parameter
The function can return multiple arguments, splitting columns with ","
Example:
function fun1(a,b,c)
...
return a,b,c
end
Return Statement#
return exp
A return statement can only be used inside a function to return the result of the function.
The data type returned by the return statement determines the return value of the function. If the function does not contain a return statement, then the function does not return any value.
Conditional Control Statement#
Form One:
if (exp) then
...
elseif (exp) then
...
else
...
end
Form 2:
if (exp) then
...
end
Form Three:
if (exp) then
...
else
...
end
Like most languages, the system supports the if conditional control statement, and when the conditional expression in if/elseif is determined to be true, the contents of the statement block are executed.
Note: The conditional expression result of the control structure can be any value, false and nil are false, true and non-nil are true. Note in particular that 0 is true:
if (0) then
print("0 is true")
end
>> 0 is true
Goto Statements#
goto Label
::Label:: exp
Example:
a=1
::label:: print("----goto-----")
if a<3 then
goto label
end
>> ----goto-----
Break Statement#
Use the break statement to terminate the loop.
Example:
a=10
while(a<20) do
print("a=",a)
a=a+1
if(a>15) then
break
end
end