User:JulienDethurens/Essays/Pseudo Ternaries: Difference between revisions
>JulienDethurens No edit summary |
>JulienDethurens No edit summary |
||
Line 9: | Line 9: | ||
*and returns the first value if it is {{false}} or {{nil}}. Otherwise, it will return the second value. | *and returns the first value if it is {{false}} or {{nil}}. Otherwise, it will return the second value. | ||
*or returns the first | *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: | These definitions might sound weird to you, but think about it: |
Revision as of 01:35, 26 March 2012
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?
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, neither nil.
- or returns true if any of the value is neither false, neither nil.
However, that's not exactly 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:
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:
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".
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.