Home Web Front-end JS Tutorial Lua expressions and control structures study notes_node.js

Lua expressions and control structures study notes_node.js

May 16, 2016 pm 04:26 PM
lua control structure expression

Arithmetic operators

Lua’s arithmetic operators are:

" "(addition):

Copy code The code is as follows:

print(1 2)

"-" (subtraction):

Copy code The code is as follows:

print(2 - 1)

"*" (multiplication):

Copy code The code is as follows:

print(1 * 2)

"/" (division):

Copy code The code is as follows:

print(1 / 2)

"^" (index):

Copy code The code is as follows:

print(27^(-1/3))

"%" (modulo):

Copy code The code is as follows:

print(5 % 3)

Relational operators

Lua provides the following relational operators:

Copy code The code is as follows:

< > <= >= == ~=

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

Copy code 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 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.

Copy code The code is as follows:

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:

Copy code The code is as follows:

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

Copy code The code is as follows:

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

Copy code 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

Check the while condition first, if the condition is true, continue to execute the loop body, otherwise end

Copy code The code is as follows:

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

Copy code The code is as follows:

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:

Copy code The code is as follows:

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)

Copy code The code is as follows:

for a = 10, 0, -2 do
Print(a)
end

The generic for loop iterates through all values ​​through an iterator function:

Copy code 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 break out of the currently executing block.
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1270
29
C# Tutorial
1250
24
How to install and configure Lua support for Nginx How to install and configure Lua support for Nginx Jun 02, 2023 pm 10:01 PM

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

How to integrate nginx with lua to operate mysql How to integrate nginx with lua to operate mysql May 16, 2023 pm 10:43 PM

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

Integration of Vue.js and Lua language to write lightweight embedded applications Integration of Vue.js and Lua language to write lightweight embedded applications Jul 31, 2023 pm 02:23 PM

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

Integration of Vue.js and Lua language, best practices and experience sharing in building front-end engines for game development Integration of Vue.js and Lua language, best practices and experience sharing in building front-end engines for game development Aug 01, 2023 pm 08:14 PM

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

How to use Lua script in Java ecosystem/Redis How to use Lua script in Java ecosystem/Redis Jun 02, 2023 pm 10:41 PM

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

How idaPro analyzes app decryption lua script How idaPro analyzes app decryption lua script May 18, 2023 am 08:34 AM

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

How Nginx uses Lua+Redis to dynamically ban IPs How Nginx uses Lua+Redis to dynamically ban IPs May 26, 2023 am 10:50 AM

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

How to use Redis+Lua script to implement the anti-swipe function of the counter interface How to use Redis+Lua script to implement the anti-swipe function of the counter interface May 28, 2023 pm 11:32 PM

[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

See all articles