Programming in Lua Translation --Statements

xiaoxiao2021-03-06  38

4.Statementslua Like C and Pascal supports traditional statements: assigning statements, control structural statements, function calls, etc., but also supports non-traditional multivariable assignments, local variable declarations .4.1 Assignment statement assignment is the value and change of a variable The most basic method of the field. A = "hello" .. "world" TN = TN 1 Lua can assign a number of variables, the variable list, and value lists are separated by commas, assigning the value of the right side of the statement. The variables are assigned to the left. A, B = 10, 2 * x indicate, A = 10 and b = 2 * X encountered assignment statement Lua first calculate all values ​​on the right and then perform assignment, so we can do this Value of swap variables: x, y = y, x - swap 'x' for 'y' a [i], a [j] = a [j], a [i] - swap 'a [i]' For 'a [i]' When the number of variables and values ​​is inconsistent, Lua will take the following policies based on the number of variables,: a. Variable number> The number of values ​​is completed by variables NIL B The number of variables is a value of the number of values ​​that are ignored by A, B, C = 0, 1 Print (A, B, C) -> 0 1 nil A, B = A 1, B 1, B 2 - Value Of B 2 IGNORED Print (A, B) -> 1 2 A, B, C = 0 Print (A, B, C) -> 0 NIL NIL above the last example is one Common error, Note: If you want to assign a value for multiple variables to assign each variable. A, B, C = 0, 0, 0 Print (A, B, C) -> 0 0 0 0 Multi-value assignment Regularly used to exchange variables or return function calls to variables: A, b = f () f () returns two values, the first assignment to A, the second assignment to B.4.2 local variables and code blocks (B LOCK) Creating a local variable using local variables, local variables are only valid within the code block being declared. Code block finger: a control structure, a function body, or a chunk (variable is declared or Text string). X = 10 local i = 1 - local to the chunk While i <= x do local x = i * 2 - local to the while body print (x) -> 2, 4, 6, 8 , ... i = i 1 end if i> 20 Then local x - local to the "the" body x = 20 Print (x 2) Else Print (x) -> 10 (the global one) end Print (x) -> 10 (the global one) Note that if the example above in interactive mode may not output the desired result, because the second sentence Local i =

1 is a complete Chunk. After the interactive mode is executed, Lua will start a new Chunk, so the second sentence I has exceeded his effective range. This code can be placed in do..nd (Equivalent to the C / C {}) block. Use local variables as much as possible, there are two benefits: 1. Avoid naming conflicts 2. Access the local variable faster than the global variable. We delineate one for Block Clear bound: DO..nd part. This is useful when you want to better control the role of local variables. Do Local A2 = 2 * a local d = SQRT (B ^ 2 - 4 * A * c) x1 = (-b d) / a2 x2 = (-b - d) / a2 end - scope of 'a2' AND 'D' Ends Here Print (x1, x2) 4.3 Control Structure Statement Control Structure The conditional expression result can be any value, Lua believes that false and nil are false, other values ​​are true. If statement, there are three forms: if conditions dam, and ration; i conditions dam f; IF conditions kilseif-part .. ---> Multiple elseif else else-part end; while statement: while condition do statements; end; repeat-us, ket; There are two categories: first, value for loop: for var = eXP1, EXP2, Exp3 do loop-part end end will perform loop-part; EXP3, using exp3 as STEP from EXP1 (initial value) It can be omitted, and the default STEP = 1 requires attention: 1. Three expressions will only be calculated once and is before the cycle start. For i = 1, f (x) do print (i) end for i = 10, 1, -1 do print (i The first example f (x) will only be called once before the cycle 2. The control variable var is the local variable automatically declared, and only is valid in the cycle. For i = 1,10 do print (i) end max = i - probably wrong! 'i' Here is Global If you need to keep the value of the control variable, you need to save it in the loop - Find a value in a list localfact = nil for i = 1, An do if a [ I] == Value the found = i - save value of 'i' Break End "Do not change the value of the control variable during the loop process, which is unpredictable. If you want to exit the cycle, Using the Break statement. Second, Vale for loop: I have seen an example: - Print All Values ​​of Array 'A' for i, v in iPairs (a) do print (v) end model for traversal iteration Each value returned. An example of a traversal table key: - Print All Keys of Table 'T'

转载请注明原文地址:https://www.9cbs.com/read-69185.html

New Post(0)