Or operator: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>SoulStealer9875
No edit summary
>Crazypotato4
The = operator does not belong in the 'See Also' part of this article. I also added a few links and made some things bold
Line 1: Line 1:
__TOC__
__TOC__


The 'or' operator operates on two values.  If the first value is neither false nor nil, the 'or' operator returns the first value.  If the first value is false or nil, then it will return the second value, regardless of what it is.
The 'or' operator operates on two values.  If the first value is <b>neither [[Bool|false]] nor [[Nil|nil]]</b>, the 'or' operator returns the first value.  If the first value <b>is</b> false or nil, then it will return the second value, regardless of what it is.
== If Statement ==
== If Statement ==
The 'or' operator comes in handy when you want to check if one of the listed values is a certain value.
The 'or' operator comes in handy when you want to check if one of the listed values is a certain value.
Line 38: Line 38:


== See Also ==
== See Also ==
* [[%3D_Operator|= Operator]]
* [http://wiki.roblox.com/index.php/And_operator And operator]
* [http://wiki.roblox.com/index.php/And_operator And operator]
* [[Not operator]]
* [[Not operator]]
* [http://wiki.roblox.com/index.php/Conditional_statements Conditional statements]
* [http://wiki.roblox.com/index.php/Conditional_statements Conditional statements]
* [[Operators]]
* [[Operators]]

Revision as of 21:35, 14 July 2011

The 'or' operator operates on two values. If the first value is neither false nor nil, the 'or' operator returns the first value. If the first value is false or nil, then it will return the second value, regardless of what it is.

If Statement

The 'or' operator comes in handy when you want to check if one of the listed values is a certain value.

soul = true
food = false
try = false

if soul == true or food == true or try == true then -- If any of the three comparisons are met, then continue
    print 'Either soul, food or trys value is true.'
end


Output:
> Either soul, food or trys value is true.

Choice of Value

The 'or' operator can also be used to choose an existent value of a value that is nil or false. Here are some examples:

local y = x or 1
print(y)
> 1

This printed '1' because variable 'x' doesn't exist and is therefore nil. So the or operator allowed us to choose 1 over nil.

local x = false
local y = x or 1
print(y)
> 1

This also printed '1' because although 'x' exists, it is false. If 'x' were true, then 'y' would be true because the 'or' operator would choose 'x' because it is not false and not nil.

See Also