Simple "if - else if - else if" combination activating on wrong and/or multiple cases
dtadams
Function choices(angleChoice)
Variable angleChoice
if(angleChoice == 1)
print "ONE"
else if(angleChoice == 2)
print "TWO"
else if(angleChoice == 3)
print "THREE"
endif
End
Variable angleChoice
if(angleChoice == 1)
print "ONE"
else if(angleChoice == 2)
print "TWO"
else if(angleChoice == 3)
print "THREE"
endif
End
This results in:
•choices(1)
ONE
THREE
•choices(2)
TWO
•choices(3)
TWO
ONE
THREE
•choices(2)
TWO
•choices(3)
TWO
Here's my hypothesis:
- If an "if/else if" condition is TRUE, the following "else if" condition is treated as FALSE.
- If an "if/else if" condition is FALSE, the following "else if" condition is treated as TRUE.
So, apparently, the very first if condition is the only one that matters. After that, if-conditions seem to be ignored completely and instead are evaluated on the basis of "run this if and only if the last condition did not run".
This is when I started having fun.
Function choices2(angleChoice)
Variable angleChoice
if(angleChoice == 1)
print "ONE"
else if(angleChoice == 2)
print "TWO"
else if(angleChoice == 3)
print "THREE"
else if(angleChoice == 4)
print "FOUR"
else if(angleChoice == 5)
print "FIVE"
else if(angleChoice == 6)
print "SIX"
else if(angleChoice == 7)
print "SEVEN"
else if(angleChoice == 8)
print "EIGHT"
endif
End
Variable angleChoice
if(angleChoice == 1)
print "ONE"
else if(angleChoice == 2)
print "TWO"
else if(angleChoice == 3)
print "THREE"
else if(angleChoice == 4)
print "FOUR"
else if(angleChoice == 5)
print "FIVE"
else if(angleChoice == 6)
print "SIX"
else if(angleChoice == 7)
print "SEVEN"
else if(angleChoice == 8)
print "EIGHT"
endif
End
•choices2(0)
TWO
FOUR
SIX
EIGHT
•choices2(1)
ONE
THREE
FIVE
SEVEN
•choices2(2)
TWO
FOUR
SIX
EIGHT
•choices2(5)
TWO
FOUR
SIX
EIGHT
TWO
FOUR
SIX
EIGHT
•choices2(1)
ONE
THREE
FIVE
SEVEN
•choices2(2)
TWO
FOUR
SIX
EIGHT
•choices2(5)
TWO
FOUR
SIX
EIGHT
I'm not really sure what to say about this.
How can I get an if-statement to act like it does in a mainstream language? I'm asking because I honestly can't think of a go-around.
if-elseif-endif
if ()
// Execute if condition 1 is TRUE
elseif ()
// Execute if condition 2 is TRUE and condition 1 is FALSE
[...]
[else
] // Optionally execute if all conditions are FALSE
endif
Variable angleChoice
if(angleChoice == 1)
print "ONE"
elseif(angleChoice == 2)
print "TWO"
elseif(angleChoice == 3)
print "THREE"
endif
End
--Jim Prouty
Software Engineer, WaveMetrics, Inc.
July 23, 2014 at 03:36 pm - Permalink
case 1:
print "ONE"
break;
case 2:
print "TWO"
break;
case 3:
print "THREE"
break;
endswitch
John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
July 23, 2014 at 03:50 pm - Permalink
Thanks for responding so fast!
July 23, 2014 at 04:05 pm - Permalink
Variable angleChoice
if(angleChoice == 1)
print "ONE"
else // if(angleChoice == 2)
print "TWO"
else // if(angleChoice == 3)
print "THREE"
endif
End
Which gives the results that were reported.
The above code can be typed in and it will compile and run, but I don't think it makes sense. Does It? Maybe this should be treated as an error.
July 23, 2014 at 05:29 pm - Permalink
The warning includes a question allowing you to make it an error, as shown by the attached screen shot. That was triggered by this code:
if(1)
else if (2)
endif
end
...and the phantom-like Igor 7 treats it as an error without asking first.
John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
July 24, 2014 at 09:16 am - Permalink
Yep, I saw that. I meant the following might be considered an error
Variable angleChoice
if(angleChoice == 1)
print "ONE"
else
print "TWO"
else
print "THREE"
endif
End
Multiple else blocks seem acceptable to the compiler, but they don't mean anything to my sense of logic... particularly the way they are executed (see OP).
July 24, 2014 at 10:37 am - Permalink
Nice!
July 24, 2014 at 11:21 am - Permalink