Cannot Use Parentheses and Math Logic in ELIF Statements
Hello,
I'm trying to figure out why I'm unable to use parentheses and math logic within ELIF statements.
Code such as this will not work:
IF X
THEN Y
ELIF (A + B) / C = D
THEN Z
ENDIF
While this will:
IF X
THEN Y
ELIF A + B / C = D
THEN Z
ENDIF
Notice how the only difference is the parentheses. Values don't really matter, neither do the math operators or comparison operators. In fact, a comparison operator isn't even needed.
Naturally, this is mathematically different and will have different results.
For this specific case, we know that the following statements are equivalent and thus interchangeable...
(A + B) / C
A / C + B / C
...so I was able to clumsily maneuver around this issue, but it still begs the question of why this is prohibited in the first place.

Hi Jozef B 
A couple thoughts:
 Fwiw, (A + B) / C is not mathematically the same as A + B / C, because multiplication and division operators are resolved prior to resolving addition and subtraction.
 The best practice would be to use parentheses to enclose everything that you want to be resolved, in the order you want them resolved. For your first example:
ELIF (((A + B) / C) = D)
You can see this method in the examples here: Nesting multiple IF THEN ELSE functions

Hello Dave,
1. I did specify that those are mathematically different and will have different results. This is why I mention that the below is a mathematically equivalent workaround.
A / C + B / C
2. The example you provided does work for me, and with more testing I found the following combinations to work / not work.
ELIF (((A + B) / C) = D)  WORKING
ELIF ((A + B) / C = D)  WORKING
ELIF ((A + B) / C) = D  NOT WORKING
ELIF (A + B) / C = D  NOT WORKING
I guess it's even weirder because this is not demonstrably required in the initial IF statement  only in the subsequent ELIFs.
IF (((A + B) / C) = D)  WORKING
IF ((A + B) / C = D)  WORKING
IF ((A + B) / C) = D  WORKING
IF (A + B) / C = D  WORKING
Regardless, thank you for clearing this up  I must have been stuck hopping between 3 & 4 without trying another set of parentheses around the entire statement. What constitutes as valid and invalid doesn't seem very predictable here, and I'm glad there's at least a post now showcasing this.
The link you provided was also helpful as it does specifically state best practices, although in one case it's a best practice and in another it's a requirement.

Glad I could help, Jozef!
Please sign in to leave a comment.
3 Comments