Or operator: Difference between revisions
From Legacy Roblox Wiki
Jump to navigationJump to search
>Legend26 Changed 'statement' to 'operator' |
>Legend26 Added some information |
||
Line 1: | Line 1: | ||
__TOC__ | __TOC__ | ||
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. | ||
== If Statement == | == If Statement == | ||
<pre> | <pre> | ||
Line 19: | Line 19: | ||
== Choice of Value == | == Choice of Value == | ||
The or | The 'or' operator can also be used to choose an existent value of a value that is nil or false. Here are some examples: | ||
<pre> | <pre> | ||
local y = x or 1 | local y = x or 1 | ||
Line 28: | Line 28: | ||
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 printed '1' because variable 'x' doesn't exist and is therefore nil. So the or operator allowed us to choose 1 over nil. | ||
<pre> | |||
local x = false | |||
local y = x or 1 | |||
print(y) | |||
> 1 | |||
</pre> | |||
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 'y' because it is not false and not nil. | |||
== See Also == | == See Also == |
Revision as of 19:33, 9 July 2011
The 'or' operator comes in handy when you want to check if one of the listed values is a certain value.
If Statement
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 with the code block print 'soul is always true' end Output: > soul is always 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 'y' because it is not false and not nil.