This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
stylish's profile picture

[Lua] [Question] How to use the AND operator in Lua?

Started by stylish, 05 February 2013 - 02:57 PM
stylish #1
Posted 05 February 2013 - 03:57 PM
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!
Kingdaro #2
Posted 05 February 2013 - 04:02 PM
Here's a nice example involving ham.


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.
Orwell #3
Posted 05 February 2013 - 04:28 PM
Here's a nice example involving ham.


ham = {}

ham.isDelicious = true
ham.isMeat = true
ham.isVegetable = false
I want to eat your example… :rolleyes:/>

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. :)/>
ChunLing #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:
if valueA then...end
if valueA and valueB then...end
if valueA or valueB then...end
if not valueA then...end
That 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 true
Understanding 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.
uh20 #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.
PhilHibbs #6
Posted 05 February 2013 - 11:57 PM
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...end
That is to say, each of these produces exactly what a typical English speaker would expect.
Mostly, but comparing it to English is dangerous. Sometimes people wil say "and" when they mean boolean "or", such as "I eat when I am hungry, and to be polite even if I'm not hungry".
ChunLing #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.