I am trying to figure out how to use the AND operator in Lua but looking through various Lua websites and wikis isn't really helping me understand how to properly use them. I am particularly trying to use the AND operator in a IF statement. If anybody can help me get a better grasp of the concept that would be great!
1
[Lua] [Question] How to use the AND operator in Lua?
Started by stylish, Feb 05 2013 03:57 PM
6 replies to this topic
#1
Posted 05 February 2013 - 03:57 PM
#2
Posted 05 February 2013 - 04:02 PM
Here's a nice example involving ham.
Here we'll want to check if it's delicious, and if it's meat.
However ham isn't a vegetable, so if we check if it's delicious and a vegetable, that won't check out.
This is basically how and statements work.
Both of the parts of the and statement must be true in order for it to become true.
ham = {} ham.isDelicious = true ham.isMeat = true ham.isVegetable = false
Here we'll want to check if it's delicious, and if it's meat.
if ham.isDelicious and ham.isMeat then print 'Yay!' end
However ham isn't a vegetable, so if we check if it's delicious and a vegetable, that won't check out.
if ham.isDelicious and ham.isVegetable then print 'What the f-' end
This is basically how and statements work.
local boolean boolean = true and true --> true boolean = true and false --> false boolean = false and true --> false boolean = false and false --> false
Both of the parts of the and statement must be true in order for it to become true.
#3
Posted 05 February 2013 - 04:28 PM
Kingdaro, on 05 February 2013 - 04:02 PM, said:
Here's a nice example involving ham.
ham = {} ham.isDelicious = true ham.isMeat = true ham.isVegetable = false
Edit: I came across this nice picture on Google that gives the so called truth tables for and, or and not:
Spoiler
A truth table basically lists all possible combinations between the inputs and there outputs according to the chosen operation. In lua you would use them as 'a and b', 'a or b' and 'not a'. Where a and b are variables or just constants (like 5 or true).I thought this could be useful to grasp the other operators once you understand Kingdaro's explanation.
#4
Posted 05 February 2013 - 04:53 PM
Or, to think of it a little more in terms of natural language, we use conditionals like so in Lua:
Now, if we want to get deeper, we can say that the and keyword is like an operator that returns the second value if (and only if) the first value is true while the or keyword returns the second value if and only if the first is false. Thus:
if valueA then...end if valueA and valueB then...end if valueA or valueB then...end if not valueA then...endThat is to say, each of these produces exactly what a typical English speaker would expect.
Now, if we want to get deeper, we can say that the and keyword is like an operator that returns the second value if (and only if) the first value is true while the or keyword returns the second value if and only if the first is false. Thus:
valueA = false and valueB --valueA is assigned false valueA = true and valueB --valueA is assigned valueB valueA = false or valueB --valueA is assigned valueB valueA = true or valueB --valueA is assigned trueUnderstanding this can lead to more efficient code in some cases, since the reason that and/or return the second or first value rather than an evaluated boolean is to speed their operation.
#5
Posted 05 February 2013 - 06:10 PM
it is also useful to use bit.band( number, number) to get the name of the number(s) that passed the test in the same run.
http://computercraft...ki/Colors_(API)
dont bother with this method unless your specifically working with colors/cables, the "if ... and ... then" statements is a lot better way of going about this.
http://computercraft...ki/Colors_(API)
dont bother with this method unless your specifically working with colors/cables, the "if ... and ... then" statements is a lot better way of going about this.
#6
Posted 05 February 2013 - 11:57 PM
ChunLing, on 05 February 2013 - 04:53 PM, said:
Or, to think of it a little more in terms of natural language, we use conditionals like so in Lua:
if valueA then...end if valueA and valueB then...end if valueA or valueB then...end if not valueA then...endThat is to say, each of these produces exactly what a typical English speaker would expect.
#7
Posted 06 February 2013 - 10:50 PM
That's why I gave the second, more precise description. You will eventually encounter code that uses logical operators in this way, so it is important to a more in-depth understanding.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users