This article mainly introduces Lua expressions and control structure learning notes. This article describes Arithmetic Operators, Relational operators, logical operators, local variables and scopes, and control structures, for more information, see
Arithmetic Operators
Lua's arithmetic operators include:
"+" (Addition ):
The Code is as follows:
Print (1 + 2)
"-" (Subtraction ):
The Code is as follows:
Print (2-1)
"*" (Multiplication ):
The Code is as follows:
Print (1*2)
"/" (Division ):
The Code is as follows:
Print (1/2)
"^" (INDEX ):
The Code is as follows:
Print (27 ^ (-1/3 ))
"%" (Modulo ):
The Code is as follows:
Print (5% 3)
Relational operators
Lua provides the following Relational operators:
The Code is as follows:
<> <= >== ~ =
The operation results returned by the preceding operators are true or false. Strings and numbers cannot be compared.
Logical operators
Logical operators include and, or, not
The Code is as follows:
Print (1 and 2)
Print (nil and 1)
Print (false and 2)
Print (1 or 2)
Print (false or 5)
Logical operators treat false and nil as false, while others are true.
Local variables and scopes
Lua uses the local statement to create local variables. The scope of local variables is limited to the block that declares them.
The Code is as follows:
Local a, B = 1, 10
If a <B then
Print ()
Local
Print ()
End
Print (a, B)
You can use local to save global variables to accelerate access to global variables in the current scope. For the acceleration effect, compare the execution time of the following computation of the Fibonacci series (Fibonacci:
The Code is as follows:
Function fibonacci (n)
If n <2 then
Return n
End
Return maid (n-2) + maid (n-1)
End
Io. write (maid (50), "\ n ")
Use local variable
The Code is as follows:
Local function fibonacci (n)
If n <2 then
Return n
End
Return maid (n-2) + maid (n-1)
End
Io. write (maid (50), "\ n ")
Control Structure
If then elseif else end
The Code is as follows:
If num = 1 then
Print (1)
Elseif num = 2 then
Print (2)
Else
Print ("other ")
End
Lua does not support switch statements.
While
First, judge the while condition. If the condition is true, execute the loop body. Otherwise, the process ends.
The Code is as follows:
Local I = 1
While a [I] do
Print (a [I])
I = I + 1
End
Repeat-
Execute a loop body first, and then judge the condition. If the condition is true, exit the loop body; otherwise, continue to execute the loop body. Similar to do-while statements in other languages, the loop body must be executed at least once.
The Code is as follows:
Local a = 1
Repeat
A = a + 1
B =
Print (B)
Until B <10
For Loop
The for loop statement can be in the numeric type for (numeric for) or generic for (generic)
Numeric for syntax:
The Code is as follows:
For start, end, step do
Doing something
End
Start indicates the start value, end indicates the end value, and step indicates the step size (optional, 1 by default)
The Code is as follows:
For a = 10, 0,-2 do
Print ()
End
A generic for loop uses an iterator function to traverse all values:
The Code is as follows:
Tab = {key1 = "val1", key2 = "val2", "val3 "}
For k, v in pairs (tab) do
If k = "key2" then
Break
End
Print (k .. "-" .. v)
End
The break and return statements are used to jump out of the currently executed block.