Soybeen
|
  |
| Joined: 17 Feb 2010 |
| Total Posts: 21462 |
|
|
| 20 May 2016 10:19 AM |
I need to do something like this: x = 1 y = 2
if x == 1 and y == 2 then -- do something elseif x == 1 and y == 2 or y == 3 then -- do something end
So, I'm assuming something like this wouldn't work because 'or' wouldn't know whether to use the initial 'if' or the 'and'. I want to say something like
elseif x==1 and (y==2 or y ==3) then -- do something end
...if that makes sense. |
|
|
| Report Abuse |
|
|
|
| 20 May 2016 10:29 AM |
I think your
elseif x == 1 and y == 2 or y == 3 then -- do something end
would work, just make a new game and try it though, but I'm pretty sure that will work. |
|
|
| Report Abuse |
|
|
eLunate
|
  |
| Joined: 29 Jul 2014 |
| Total Posts: 13268 |
|
|
| 20 May 2016 10:29 AM |
and takes precedence over or
(a and b or c) is equivalent to ((a and b) or c)
What this means for you is that sticking brackets around your code does exactly what you think it does. |
|
|
| Report Abuse |
|
|
guges
|
  |
| Joined: 07 May 2016 |
| Total Posts: 1515 |
|
|
| 20 May 2016 10:30 AM |
| yeah just stick that ho in some parentheses |
|
|
| Report Abuse |
|
|
|
| 20 May 2016 10:38 AM |
| hmm.. cool, didn't know that. |
|
|
| Report Abuse |
|
|
|
| 20 May 2016 10:49 AM |
Maybe, you only wanted an explaination of precedence, but your original code was kind of silly:
if x == 1 and y == 2 then -- do something elseif x == 1 and (y == 2 or y == 3) then -- y will never be 2 here, because that was already handeled above. -- do something end
your OP was simply redundant:
if x == 1 and y == 2 then -- do something elseif (x == 1 and y == 2) or y == 3 then -- again, the stuff in brackets has already been handled, and will never be true. -- do something end
|
|
|
| Report Abuse |
|
|
Soybeen
|
  |
| Joined: 17 Feb 2010 |
| Total Posts: 21462 |
|
|
| 20 May 2016 01:07 PM |
| @BJ, This code was conceptual. In my real code, there are many factors that determine the value of y. |
|
|
| Report Abuse |
|
|