Internet Tutorials How to Create Assignments and Control Structures in Lua Lua is similar to Pascal or C. This set includes assignments, …

Lua is similar to Pascal or C. This set includes assignments, control structures, function calls, and variable declarations. ¿How to Create Assignments and Control Structures in Lua?

Lua is a powerful, efficient, lightweight and integrable language. It supports the programming of procedures, oriented to objects among others.

How to Create Assignments and Control Structures in Lua?

Programmers often need to be able to store values ​​in memory so that they can be used later. This is done using variables.

Variables are references to a value that is stored in the computer’s memory. They can be used to access a number later after storing it in memory.

Assignment is the statement that is used to assign a value to a variable. It consists of the name of the variable in which the value should be stored, an equal sign, and the value that should be stored in the variable:

variable = 43
print(variable) – > 43

As demonstrated in the above code, the value of a variable can be accessed by placing the variable name where the value should be accessed.

Lua Allows Multiple Assignments

Therefore, the syntax for assignment defines a list of variables on the left side and a list of expressions on the right side. Items in both lists are separated by commas.

stat :: = varlist `= ´ explist
varlist :: = var {`, ´ var}
explist :: = exp {`, ´ exp}

Before assignment, the list of values ​​is adjusted to the length of the list of variables. If there are more values ​​than necessary, the excess values ​​are discarded.

If there are fewer values ​​than necessary, the list is expanded with as many nulls as necessary. If the list of expressions ends with a function call.

All values ​​returned by that call go into the list of values ​​before wrapping (except when the call is in parentheses).

i = 3
i, a[i] = i+1, 20
Establece un [3] a 20, sin afectar a un [4] porque la i en a [i] se evalúa (a 3) antes de que se le asigne 4. Del mismo modo, la línea
x, y = y, x
Intercambia los valores de x y y, y
x, y, z = y, z, x
Permuta cíclicamente los valores de x, y, y z.

The meaning of the assignments to global variables and table fields can be changed through metatables. An assignment to an indexed variable t [i] = val is equivalent to settable_event (t, i, val).

An assignment to a global variable x = val is equivalent to the assignment _env.x = val, which in turn is equivalent.

settable_event (_env, "x", val)

Where _env is the environment of the running function.

An assignment to a global variable x = val is equivalent to the assignment _env.x = val, which in turn is equivalent.

settable_event (_env, "x", val)

Where _env is the environment of the running function.

Control Structures

Control structures if, while and repeat they have the usual meaning.

stat ::= while exp do block end
stat ::= repeat block until exp
stat ::= if exp then block {elseif exp then block} [else block] en

The condition expression of a control structure can return any value. Both false and null are considered false.

All different values ​​of nil and false are considered true (in particular the number 0 and the empty string are also true).

In the repeat until loop, the inner block does not end on the until keyword, but only after the condition. Therefore, the condition can refer to local variables declared within the loop block.

The return statement is used to return values ​​from a function or a fragment (which is just a function). Functions and fragments can return more than one value.

stat ::= return [explist]

The break statement is used to terminate the execution of a beat, repeat or to loop, skipping to the next.

stat ::= break

A break ends the innermost enveloping loop

Return statements and break can only be written as the last statement of a block. If it is really necessary to return or break in the middle of a block.

Then an explicit inner block can be used, as in idioms the terms return final and they break final, because now return and break are the last statements in their (inner)) blocks.

For Declaration

The for statement has two forms: a numeric and a generic. The numeric for loop repeats a block of code while a control variable runs through an arithmetic progression.

stat ::= for Name `=´ exp `,´ exp [`,´ exp] do block end

The block is repeated for the name that starts at the value of the first exp, until the second exp passes in steps of the third exp.

for v = e1, e2, e3 do block end