Next: 4.5 Expressions Up: 4 The Language Previous: 4.3 Adjustment

4.4 Statements

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.

4.4.1 Blocks

A block is a list of statements, executed sequentially. Any statement can be optionally followed by a semicolon.

   block ::= { stat 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.

4.4.2 Assignment

The language allows multiple assignment. Therefore, the syntax defines a list of variables on the left side, and a list of expressions on the right side. Both lists have their elements separated by commas.
   stat ::= varlist1 '=' explist1
varlist1 ::= var { ',' var }
This 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, x
Before the assignment, the list of values is adjusted to the length of the list of variables; see Section 4.3.

   var ::= name
A single name can denote a global or a local variable, or a formal parameter.
   var ::= var '[' 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.
   var ::= var '.' name
The syntax var.NAME is just syntactic sugar for var["NAME"].

4.4.3 Control Structures

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.

   stat ::= while exp1 do block end 
          | repeat block until exp1 
          | if exp1 then block { elseif } [ else block ] end
elseif ::= elseif 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:

   ret ::= return explist

4.4.4 Expressions as Statements

All expressions with possible side-effects can be executed as statements. These include function calls and table constructors:
   stat ::= functioncall
stat ::= tableconstructor
Eventual returned values are thrown away. Function calls are explained in Section 4.5.8; constructors are the subject of Section 4.5.7.

4.4.5 Local Declarations

Local variables can be declared anywhere inside a block. Their scope begins after the declaration and lasts until the end of the block. The declaration may include an initial assignment:
   stat ::= local declist [ init ]
declist ::= name { ',' name }
init ::= '=' explist1
If there is an initial assignment, it has the same semantics of a multiple assignment. Otherwise, all variables are initialized with nil.


Next: 4.5 Expressions Up: 4 The Language Previous: 4.3 Adjustment