Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Lua
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
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>
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Lua
(section)
Add topic