Lua expressions and control structures study notes_node.js
Arithmetic operators
Lua’s arithmetic operators are:
" "(addition):
print(1 2)
"-" (subtraction):
print(2 - 1)
"*" (multiplication):
print(1 * 2)
"/" (division):
print(1 / 2)
"^" (index):
print(27^(-1/3))
"%" (modulo):
print(5 % 3)
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
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 and everything else as true.
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.
local a, b = 1, 10
if a < b then
Print(a)
Local a
Print(a)
end
print(a, b)
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:
function fibonacci(n)
If n < 2 then
return n
End
Return fibonacci(n - 2) fibonacci(n - 1)
end
io.write(fibonacci(50), "n")
Use local variables local
local function fibonacci(n)
If n < 2 then
return n
End
Return fibonacci(n - 2) fibonacci(n - 1)
end
io.write(fibonacci(50), "n")
Control structure
if then elseif else end
if num == 1 then
Print(1)
elseif num == 2 then
Print(2)
else
Print("other")
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
local i = 1
while a[i] do
Print(a[i])
i = i 1
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
local a = 1
repeat
a = a 1
b = a
Print(b)
until b < 10
for loop
The for loop statement has two forms: numeric for (numeric for), generic for (generic for)
Number type for syntax:
for start, end, step do
doing something
end
start is the start value, end is the end value, and step is the step size (optional, default is 1)
for a = 10, 0, -2 do
Print(a)
end
The generic for loop iterates through all values through an iterator function:
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 break out of the currently executing block.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Nginx installation and configuration Lua support By default, Nginx does not support Lua modules. You need to install the LuaJIT interpreter and recompile Nginx, or you can use the modules required by openrestry developed by Chinese people: LuaJIT, Ngx_devel and lua-nginx-module1. Environment preparation [root @nginx_lua~]#yuminstall-ygccgcc-c++makepcre-develzlib-developenssl-devel2. Download the latest luajit and ngx_devel_kit and lua-nginx-module decompression [r

The implementation idea is to directly configure the blacklist in nginx and implement it by writing logical blocks; write the filter in the server (Java) and unify the interceptors in the filter; write the interceptor in the server (Java) and unify the interceptors Interception; here are 3 implementation ideas. As for implementation solutions, there may be more, but if we think about it, writing logical blocks in nginx does not seem to be what many people are good at; it is not impossible to do it at the code level, but this is First, during peak business periods involving high concurrency, this will inevitably put greater pressure on back-end services. So are there any other better ways to deal with it? This is what lua is about, that is, nginx acts as a gateway and still acts as a proxy server, because nginx can integrate lua

The integration of Vue.js and Lua language to write lightweight embedded applications. In modern development, the front-end framework Vue.js and the scripting language Lua each have a wide range of applications. Vue.js is a progressive framework for building user interfaces, while Lua is a lightweight scripting language often used for embedded application and game development. This article will introduce how to integrate Vue.js with Lua language to write lightweight embedded applications, and provide code examples. First, we need to install Vue.j

The integration of Vue.js and Lua language, best practices and experience sharing for building a front-end engine for game development Introduction: With the continuous development of game development, the choice of game front-end engine has become an important decision. Among these choices, the Vue.js framework and Lua language have become the focus of many developers. As a popular front-end framework, Vue.js has a rich ecosystem and convenient development methods, while the Lua language is widely used in game development because of its lightweight and efficient performance. This article will explore how to

1. Install LUA Installing LUA on Mac is very simple. Just use brew related commands directly; brewinstalllua uses the lua-v command to see that lua has been installed. 1) Simple use to create a test.lua file, the content is: Execute command: luatest.lua output is: 2. Introduction to lua syntax Lua provides interactive programming and scripted programming: Interactive programming: directly enter the syntax on the command line , you can execute it immediately and see the execution effect. Scripting is programming: write a script file and then execute it. 1. Comment Lua provides two comment methods: single-line comments and multi-line comments 1) Single-line comments use two minus signs;--2) Multi-line comments--[[Multi-line comments multi-line

Through the previous idaPro debugging or hook, we can obtain the xxtea decryption key. For sign, we can directly open the original file: we can see the sign value: byds. Therefore, we can try to decrypt it with the xxtea decryption tool (which can be compiled from the source code on GitHub): Taking index.luac as an example, we see the changes before and after decryption of index.luac: We see that the lua script after decryption by xxtea is still not Plain text! We previously determined that the xxtea encryption was used based on the cocos2d framework source code and the master apk decoding result, and the Lua script of the app also has a signature value, which also confirms that it is the xxtea encryption method, but we

1. Background In our daily maintenance of the website, we often encounter such a requirement. In order to block certain crawlers or malicious users from making requests to the server, we need to establish a dynamic IP blacklist. For IPs in the blacklist, services will be denied. 2. There are many ways to implement the IP blacklist function in the architecture: 1. At the operating system level, configure iptables to reject network requests for specified IPs; 2. At the webserver level, configure the IP blacklist through the deny option of nginx itself or the Lua plug-in; 3. At the application level, check whether the client IP is in the blacklist before requesting the service. In order to facilitate management and sharing, we implement IP through the architecture of nginx+lua+redis

[Implementation process] 1. Problem analysis If the set command is set, but when setting the expiration time, the setting is not successful due to network jitter and other reasons, then a dead counter (similar to a deadlock) will appear; 2. Solution Redis+Lua It is a good solution. Use a script to make the set command and the expire command work together so that Redis is executed without being interfered with. This guarantees atomic operations to a large extent. Why is it said that it guarantees atomic operations to a large extent rather than completely? ensure? Because problems may occur when Redis is executed internally, but the probability is very small; even for small-probability events, there are corresponding solutions, such as solving deadlocks. An idea worth referring to: To prevent deadlocks, the value of the lock will be stored in
