Or operator: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Flurite
>JulienDethurens
No edit summary
 
(15 intermediate revisions by 4 users not shown)
Line 1: Line 1:
__TOC__
__TOC__


The or statement comes in handy when you want to check if one of the listed values is a certain value.
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 ==
<pre>
The 'or' operator comes in handy when you want to check if one of the listed values is a certain value.
{{code and output
|code=
soul = true
soul = true
food = false
food = false
try = false
try = false


if soul == true or food == true or try == true then -- If any of the three comparisons are met, then continue with the code block
if soul == true or food == true or try == true then -- If any of the three comparisons are met, then continue
     print 'soul is always true'
     print("The value of either soul, food or try is true.")
end
end
|output=The value of either soul, food or try 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:
{{code and output
|code=
local y = x or 1
print(y)
|output=1
}}


Output:
This printed '1' because <var>x</var> doesn't exist and is therefore nil. So the or operator allowed us to choose 1 over nil.
> soul is always true


</pre>
{{code and output
 
|code=
== Choice of Value ==
local x = false
The or statement can also be used to choose an existent value of a nil value. Here is an example:
<pre>
local y = x or 1
local y = x or 1
print(y)
print(y)
> 1
|output=1
</pre>
}}
 
This printed '1' because variable 'x' doesn't exist and is therefore nil. So the or operator allowed us to choose 1 over nil.


This also printed '1' because although <var>x</var> exists, it is false. If <var>x</var> had been true, then <var>y</var> would be true, because the 'or' operator would choose <var>x</var>, as it is neither false, neither nil.


== See Also ==
== See Also ==
[http://wiki.roblox.com/index.php/And_operator And operator]
* [[and operator]]
 
* [[not operator]]
[http://wiki.roblox.com/index.php/Conditional_statements Conditional statements]
* [[Conditional statements]]
* [[Operators]]

Latest revision as of 22:51, 29 January 2012

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("The value of either soul, food or try is true.")
end
The value of either soul, food or try 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 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 had been true, then y would be true, because the 'or' operator would choose x, as it is neither false, neither nil.

See Also