Home > Article > Web Front-end > Lua expressions and control structures study notes_node.js
Arithmetic operators
Lua’s arithmetic operators are:
" "(addition):
"-" (subtraction):
"*" (multiplication):
"/" (division):
"^" (index):
"%" (modulo):
Relational operators
Lua provides the following relational operators:
The operation results returned by the above operators are all true or false. Strings and numbers cannot be compared
Logical operators
Logical operators include and, or, not
Local variables and scope
Lua creates local variables through the local statement. The scope of local variables is limited to the block in which they are declared.
Using local variables local to save global variables can speed up access to global variables in the current scope. For the acceleration effect, compare the execution time of the Fibonacci sequence calculated below:
Use local variables local
Control structure
if then elseif else end
Lua does not support switch statements
while
Check the while condition first, if the condition is true, continue to execute the loop body, otherwise end
repeat-until
First execute the loop body once, 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 will be executed at least once
for loop
The for loop statement has two forms: numeric for (numeric for), generic for (generic for)
Number type for syntax:
start is the start value, end is the end value, and step is the step size (optional, default is 1)
The generic for loop iterates through all values through an iterator function: