User:JulienDethurens/Essays/Pseudo Ternaries: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>JulienDethurens
No edit summary
>JulienDethurens
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
If you're reading this, you almost certainly know about the and and or operators. These are useful in if statements to combine boolean expressions and manipulate them. But did you know they could also be useful even outside of if statements?
#redirect [[User:JulienDethurens/Essays/Pseudo ternaries]]
 
When you use the {{`|and}} and {{`|or}} operators, you probably think of them in the following way:
 
*and returns {{true}} if both values are neither {{false}} nor {{nil}}.
*or returns {{true}} if any of the values is neither {{false}} nor {{nil}}.
 
However, that's not <em>exactly</em> how they work. This is how they really work:
 
*and returns the first value if it is {{false}} or {{nil}}. Otherwise, it will return the second value.
*or returns the first value if it is neither {{false}} nor {{nil}}. Otherwise, it will return the second value.
 
These definitions might sound weird to you, but think about it:
 
{{`|true and true}} will not return the first value, because it is neither {{false}}, nor {{nil}}, and will therefore return the second value.
{{`|false or true}} will not return the first value, because it is {{false}}, and will therefore return the second value.
 
You can try this with any combination, and it will still correspond to the definitions you previously used. But there are some small difference...
 
What does {{`|true and 5}} return? Remember, everything other than {{nil}} and {{false}} is considered as true. Therefore, this would return {{true}}? No! It would return 5! Why? Well, because the first value is nor {{false}}, nor {{nil}}, and, therefore, the second value, 5, is returned.
 
Suppose you wanted to print "Hello" if a [[BoolValue]]'s value is {{true}}, and print "Bye" otherwise. You would probably do it this way:
 
{{code|=
if boolvalue.Value then
print("Hello")
else
print("Bye")
end
}}
 
But you can make that a lot shorter by using what you have just learned. Look at this:
 
{{code|=
print(boolvalue.Value and "Hello" or "Bye")
}}
 
That code does exactly the same thing as the previous code. But how does it work? I will explain that, don't worry:
 
The code checks if the [[BoolValue]]'s value is {{true}}. If it is, the and operator will therefore return the second value, "Hello". Then, the code left will be {{`|"Hello" or "Bye"}}. "Hello" being nor {{false}}, nor {{nil}}}, it will be returned and therefore, the print function will print "Hello". But, what would happen if the [[BoolValue]]'s value was {{false}}? Let's think about it: the code would evaluate {{`|false and "Hello" or "Bye"}}, which would be reduced to {{`|false or "Bye"}}, as the first value is indeed {{false}}. And since the first value is {{false}}, that would return the second value, "Bye".
 
The previous example used the {{`|and}} and the {{`|or}} operators, but you could also make one that only uses the {{`|or}} operator. Here is an example:
 
{{code|=
print(a or b)
}}
 
If <var>a</var> is {{nil}} or {{false}}, then the value printed will be <var>b</var>. Otherwise, it will be <var>a</var>. This allows you to do many things, like creating optional arguments for functions without having to use if statements. The same thing can be done by using the {{`|and}} operator, though it is not as useful.
 
You can do lots of things by cleverly manipulating the {{`|and}} and {{`|or}} operators. Many players call this "ternaries", but it is not really ternaries, as it is just clever use of the {{`|and}} and {{`|or}} operators.

Latest revision as of 21:13, 7 April 2012