[Home] [Downloads] [Search] [Help/forum]

Register forum user name Search FAQ

for (numeric)

The numeric for lets you loop a specific number of times, eg.

for var = start, finish, step do
  some_action ()

The three expressions are evaluated once before the loop starts. "start" is the initial value, "finish" is the limit of the loop, and "step" is what to add after each iteration. For example:

for i = 1, 10, 2 do
  print (i) --> 1, 3, 5, 7, 9

for i = 10, 1, -3 do
  print (i) --> 10, 7, 4, 1

for i = 1, 5 do
  print (i) --> 1, 2, 3, 4, 5

The "loop control" variable (i in the examples above) is only visible inside the loop. It is a mistake to try to access it outside the loop. If you must remember what the loop control variable was when exiting a loop, save it in another variable.

You should not attempt to change the loop control variable. Doing so will lead to undefined results.

If you want to leave the loop from inside you can use a break statement.

More specifically what happens in the numeric "for" is:

  • The three control expressions (start, finish, step) are evaluated once and stored into local variables (start variable, finish variable, step variable). These evaluations must result in numbers or an error is raised. If omitted, the step variable is assumed to be one.

  • The loop control variable (var) is set to the start variable.

  • If the step variable is positive, then the loop control variable is tested to see if it is less than, or equal to, the finish variable. If not, the loop exits.

  • If the step variable is negative or zero, then the loop control variable is tested to see if it is greater than, or equal to, the finish variable. If not, the loop exits.

  • The loop body is now executed.

  • The step variable is added to the loop control variable.

  • Control passes back to the start of the loop to test the loop control variable again.

See Also ...

Lua keywords/topics

data types
for (generic)
if / then / else
logical operators
relational operators
string literals


Lua base functions
Lua bc (big number) functions
Lua bit manipulation functions
Lua coroutine functions
Lua debug functions
Lua io functions
Lua LPEG library
Lua math functions
Lua os functions
Lua package functions
Lua PCRE regular expression functions
Lua script extensions
Lua SQLite (database) interface
Lua string functions
Lua syntax
Lua table functions
Lua utilities
Scripting callbacks - plugins

(Help topic: lua=for (numeric))

Documentation contents page

Search ...

Enter a search string to find matching documentation.

Search for:   

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]