if / then / else |
---|
You can use if condition then ... end to test a condition and execute code conditionally, eg.
If you want to do something else if the condition is not true you can use an else clause:
For multiple tests you can use elseif which avoids having to use end multiple times:
The only conditions that are false are the values false and nil - every other value is considered true. You can of course reverse a condition by using not like this:
It is considered good programming practice to indent the contents of an if statement, to make it clear what parts are being executed conditionally. It is also a good idea to put a comment on the end indicating what it is the end of (as in the examples above). This is because the keyword end can end if statements, while statements, for statements, do statements, and function bodies. See Also ... Lua keywords/topics
assignment
Topics
Lua base functions
(Help topic: lua=if / then / else) |
Enter a search string to find matching documentation.
Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.