Register forum user name Search FAQ

logical operators

The logical operators are and, or, and not. Like control structures, all logical operators consider false and nil as false and anything else (including zero) as true. The operator and returns its first argument if it is false; otherwise, it returns its second argument. The operator or returns its first argument if it is not false, otherwise, it returns its second argument.

Both and and or use short-cut evaluation, that is, they evaluate their second operand only when necessary.

A useful technique is to use the ability of or to return its first argument if it is not false to quickly test if a variable exists. For example:

t = t or {} -- create table if it does not exist


The operator not always returns true or false.


See Also ...

Lua keywords/topics

assignment
break
comments
data types
do
for (generic)
for (numeric)
function
identifiers
if / then / else
keywords
local
precedence
relational operators
repeat
return
string literals
tables
while

Topics

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
Scripting callbacks - plugins

(Help topic: lua=logical operators)

Documentation contents page


Search ...

Enter a search string to find matching documentation.

Search for:   

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