What do you want to achieve? Keep it simple and clear!
learn exactly how and/or operate
What is the issue? Include screenshots / videos if possible!
I don’t know how they fully work yet
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
Experimenting
So I have had some questions in the past about and/or but have just found ways to avoid them. I want to know on a more complicated topic how this works. So my question is:
if number = 5 or number = 6 and number = 7 or number = 4 then
Is this saying that if the number is 5 or 6 AND the number is 7 or 4, or is it saying if the number is 5 or the number is 6 and 7 or the number is 4? If more information is needed to be provided just reply to this post on how it can be improved. Sorry, it’s kinda confusing.
The if statement will be true if the number is 5 or 6 AND ALSO 7 or 4.
So if your numbers were 5 and 4. The statement would be true.
Both sides of the and must be fulfilled.
If the number is not 5 or 6 but the other side of the and is fulfilled, the if statement is false.
Tell me if you need more elaboration.
It might help to write it with parenthesis to be clearer to the reader what the intended boolean logic is. A look at your example:
if number==5 or number==6 and number==7 or number==4
May be rewritten as follows: if(number==5 or number==6) and (number==7 or number==4)
Now, say your number is 5. This expression becomes: if(true or false) and (false or false)
Simplifies to: if(true) and (false)
true and false will evaluate to false since both conditionals are not true.
Your example might also be inferred as: if(number==5) or (number==6 and number==7) or (number==4) then...
In which case, a number 5 would be: if(true) or (false and false) or (false)
In which equal conditionals with the “or” operator can be simplified, so this becomes: if(true) or (false)
which is true, since “or” only needs one of the conditionals to be true.
Yeah, singular conditionals don’t need to be parenthesized if you don’t need them, I’d still recommend it though just to be a little more explicit about the behavior you want. I find it to be easier to debug too. In this form (number == 6 and number == 7) will evaluate first, then your statement becomes (again for number of 5): if true or false or false
which will evaluate true.
All logical operators consider false and nil as false and anything else as true.
Both and and or use short-cut evaluation, that is, they evaluate their second operand only when necessary. ( though I’m not sure how that would work with ‘and’)
The operator and returns its first argument if it is false;
otherwise, it returns its second argument.
The operator or returns its first argument if it is not false;
otherwise, it returns its second argument.
The operators are processed left to right, so in your example: if number == 5 or number == 6 and number == 7 or number == 4 then
Since the first operator is an ‘or’ it will check to see if n==5 and return true straight away if n==5 is true without ever looking at the rest (rule 3 and the short circuit).
If n isn’t 5, then (by rule 4) it returns the other side of the ‘or’ (true if n==6 and false if n~=6), which is also the left side of the ‘and’ operator, so that operator is evaluated too. If n is 6, then the ‘and’ returns the right side, n==7 (by rule 2), which equates to false. if n is not 6, then it returns the left side, n==6 (rule 1), or false. So, this example will return false if n is 6 or 7, which seems correct since n can’t be both 6 and 7.
Either way, the ‘and’ will feed a false value into the left side of the final ‘or’, and (by rule 4) this will always cause the final n==4 to be evaluated. So, you can get a true out of the example if n is 4 or 5, but everything else will be false.
It also helps to just plug true/false or even numbers between the operators and then print the results to get a feel for this stuff. print(5 and 4 or 6) -- example