Lua supports an almost conventional set of statements. The conventional commands include assignment, control structures and procedure calls. Non-conventional commands include table constructors, explained in Section 4.5.7, and local variable declarations.
A block is a list of statements, executed sequentially.
Any statement can be optionally followed by a semicolon.
blockstat sc ret sc
sc;
For syntactic reasons, a return statement can only be written
as the last statement of a block.
This restriction also avoids some ``statement not reached'' errors.
statvarlist1 = explist1 varlist1var , varThis statement first evaluates all values on the right side and eventual indices on the left side, and then makes the assignments. Therefore, it can be used to exchange two values, as in
x, y = y, xBefore the assignment, the list of values is adjusted to the length of the list of variables; see Section 4.3.
varname
A single name can denote a global or a local variable,
or a formal parameter.
varvar [ exp1 ]
Square brackets are used to index a table.
If var
results in a table value,
the field indexed by the expression value gets the assigned value.
Otherwise, the fallback settable is called,
with three parameters: the value of var
,
the value of expression, and the value being assigned to it;
see Section 4.7.
varvar . name
The syntax var.NAME
is just syntactic sugar for
var["NAME"]
.
The condition expression of a control structure can return any value. All values different from nil are considered true, while nil is considered false. if's, while's and repeat's have the usual meaning.
statwhile exp1 do block end
repeat block until exp1
if exp1 then block elseif
else block end
elseifelseif exp1 then block
A return is used to return values from a function.
Because a function may return more than one value,
the syntax for a return statement is:
retreturn explist
statfunctioncallEventual returned values are thrown away. Function calls are explained in Section 4.5.8.
statlocal declist init declistname , name init= explist1If there is an initial assignment, it has the same semantics of a multiple assignment. Otherwise, all variables are initialized with nil .