Home Explore Blog CI



neovim

7th chunk of `runtime/doc/luaref.txt`
26df929b039cdef918f9bf1f43914e33b32a8694e90edd920000000100000fa2
 assignment to an indexed variable `t[i] = val` is
equivalent to `settable_event(t,i,val)`. (See |lua-metatable| for a complete
description of the `settable_event` function. This function is not defined or
callable in Lua. We use it here only for explanatory purposes.)

An assignment to a global variable `x = val` is equivalent to the
assignment `_env.x = val`, which in turn is equivalent to
>lua
       settable_event(_env, "x", val)
<
where `_env` is the environment of the running function. (The `_env` variable is
not defined in Lua. We use it here only for explanatory purposes.)

------------------------------------------------------------------------------
2.4.4  Control Structures                               *lua-control*

                                   *lua-if* *lua-then* *lua-else* *lua-elseif*
                                   *lua-while* *lua-repeat* *lua-until*
The control structures `if`, `while`, and `repeat` have the usual meaning and
familiar syntax:
>
       stat ::=  while  exp do block end
       stat ::=  repeat  block until exp
       stat ::=  if  exp then block { elseif exp then block }
                 [ else block ] end
<
Lua also has a `for` statement, in two flavors (see |lua-for|).

The condition expression of a control structure may return any value.
Both `false` and `nil` are considered false. All values different
from `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 at the `until` keyword,
but only after the condition. So, the condition can refer to local variables
declared inside the loop block.

                                                                 *lua-return*
The `return` statement is used to return values from a function or a chunk
(which is just a function). Functions and chunks may return more than one
value, so the syntax for the `return` statement is

       `stat ::=`  `return`  `[explist1]`

                                                                  *lua-break*
The `break` statement is used to terminate the execution of a `while`, `repeat`,
or `for` loop, skipping to the next statement after the loop:

       `stat ::=`  `break`

A `break` ends the innermost enclosing loop.

The `return` and `break` statements 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 the idioms
`do return end` and `do break end`, because now `return` and `break` are
the last statements in their (inner) blocks.

------------------------------------------------------------------------------
2.4.5  For Statement                             *for* *lua-for*

The `for` statement has two forms: one numeric and one generic.

The numeric `for` loop repeats a block of code while a control variable runs
through an arithmetic progression. It has the following syntax:
>
       stat ::=  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

Title: Lua Control Structures: `if`, `while`, `repeat`, `return`, `break`, and `for` Statements
Summary
This section details Lua's control structures, including `if`, `while`, and `repeat`, which function with typical syntax. Any value other than `false` or `nil` is considered true in condition expressions. The `return` statement exits a function or chunk, potentially returning multiple values, while `break` terminates loops. Both must be the last statement in a block, but can be used within nested `do...end` blocks. The `for` statement has numeric and generic forms. The numeric `for` repeats a block while a variable progresses through an arithmetic sequence, with expressions evaluated only once at the start. Variables like `var`, `limit`, and `step` are invisible within the loop.