Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Lua
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Control flow === Lua has one type of [[Conditional (computer programming)|conditional]] test: <code>[[Conditional_(computer_programming)#If–then(–else)|if then end]]</code> with optional <code>else</code> and <code>elseif then</code> execution control constructs. The generic <code>if then end</code> statement requires all three keywords: <syntaxhighlight lang="lua"> if condition then --statement body end </syntaxhighlight> An example of an <code>if</code> statement <syntaxhighlight lang="lua"> if x ~= 10 then print(x) end </syntaxhighlight> The <code>else</code> keyword may be added with an accompanying statement block to control execution when the <code>if</code> condition evaluates to <code>false</code>: <syntaxhighlight lang="lua"> if condition then --statement body else --statement body end </syntaxhighlight> An example of an <code>if else</code> statement <syntaxhighlight lang="lua"> if x == 10 then print(10) else print(x) end </syntaxhighlight> Execution may also be controlled according to multiple conditions using the <code>elseif then</code> keywords: <syntaxhighlight lang="lua"> if condition then --statement body elseif condition then --statement body else -- optional --optional default statement body end </syntaxhighlight> An example of an <code>if elseif else</code> statement <syntaxhighlight lang="lua"> if x == y then print("x = y") elseif x == z then print("x = z") else -- optional print("x does not equal any other variable") end </syntaxhighlight> Lua has four types of conditional loops: the [[while loop|<code>while</code> loop]], the <code>repeat</code> loop (similar to a [[do while loop|<code>do while</code> loop]]), the numeric [[for loop|<code>for</code> loop]] and the generic <code>for</code> loop. <syntaxhighlight lang="lua"> --condition = true while condition do --statements end repeat --statements until condition for i = first, last, delta do --delta may be negative, allowing the for loop to count down or up --statements --example: print(i) end </syntaxhighlight> This generic <code>for</code> loop would iterate over the table <code>_G</code> using the standard iterator function <code>pairs</code>, until it returns <code>nil</code>: <syntaxhighlight lang="lua"> for key, value in pairs(_G) do print(key, value) end </syntaxhighlight> Loops can also be [[Nesting (programming)|nested]] (put inside of another loop). <syntaxhighlight lang="lua"> local grid = { { 11, 12, 13 }, { 21, 22, 23 }, { 31, 32, 33 } } for y, row in pairs(grid) do for x, value in pairs(row) do print(x, y, value) end end </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)