Jump to content




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


6 replies to this topic

#1 stylish

  • Members
  • 7 posts

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!

#2 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

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.

#3 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 05 February 2013 - 04:28 PM

View PostKingdaro, 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
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. :)

#4 ChunLing

  • Members
  • 2,027 posts

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.

#5 uh20

  • Members
  • 11 posts

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.

#6 PhilHibbs

  • Members
  • 98 posts

Posted 05 February 2013 - 11:57 PM

View PostChunLing, 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...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".

#7 ChunLing

  • Members
  • 2,027 posts

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