for Name = exp , exp [ , exp ] do block end
<
The `block` is repeated for `name` starting at the value of the first `exp`, until
it passes the second `exp` by steps of the third `exp`. More precisely,
a `for` statement like
`for var = e1, e2, e3 do block end`
is equivalent to the code: >lua
do
local var, limit, step = tonumber(e1), tonumber(e2), tonumber(e3)
if not ( var and limit and step ) then error() end
while ( step >0 and var <= limit )
or ( step <=0 and var >= limit ) do
block
var = var + step
end
end
<
Note the following:
- All three control expressions are evaluated only once, before the loop
starts. They must all result in numbers.
- `var`, `limit` and `step` are invisible variables. The names are here for
explanatory purposes only.
- If the third expression (the step) is absent, then a step of 1 is used.
- You can use `break` to exit a `for` loop.
- The loop variable `var` is local to the loop; you cannot use its value
after the `for` ends or is broken. If you need this value, assign it to
another variable before breaking or exiting the loop.
*for-in*
The generic `for` statement works over functions, called |iterator|s. On each
iteration, the iterator function is called to produce a new value, stopping
when this new value is `nil`. The generic `for` loop has the following syntax:
>
stat ::= for namelist in explist1 do block end
namelist ::= Name { , Name }
<
A `for` statement like
`for` `var1, ..., varn` `in` `explist` `do` `block` `end`
is equivalent to the code: >lua
do
local f, s, var = explist
while true do
local var1, ..., varn = f(s, var)
var = var1
if var == nil then break end
block
end
end
<
Note the following:
- `explist` is evaluated only once. Its results are an iterator function,
a `state`, and an initial value for the first iterator variable.
- `f`, `s`, and `var` are invisible variables. The names are here for
explanatory purposes only.
- You can use `break` to exit a `for` loop.
- The loop variables `var1, ..., varn` are local to the loop; you cannot use
their values after the `for` ends. If you need these values, then assign
them to other variables before breaking or exiting the loop.
------------------------------------------------------------------------------
2.4.6 Function Calls as Statements *lua-funcstatement*
To allow possible side-effects, function calls can be executed as statements:
>
stat ::= functioncall
<
In this case, all returned values are thrown away. Function calls are
explained in |lua-function|.
------------------------------------------------------------------------------
2.4.7 Local Declarations *lua-local*
Local variables may be declared anywhere inside a block. The declaration may
include an initial assignment:
>
stat ::= local namelist [ = explist1 ]
namelist ::= Name { , Name }
<
If present, an initial assignment has the same semantics of a multiple
assignment (see |lua-assign|). Otherwise, all variables are initialized
with `nil`.
A chunk is also a block (see |lua-chunk|), and so local variables can be
declared in a chunk outside any explicit block. The scope of such local
variables extends until the end of the chunk.
The visibility rules for local variables are explained in |lua-visibility|.
==============================================================================
2.5 Expressions *lua-expressions*
The basic expressions in Lua are the following:
>
exp ::= prefixexp
exp ::= nil | false | true
exp ::= Number
exp ::= String
exp ::= function
exp ::= tableconstructor