Simple "if - else if - else if" combination activating on wrong and/or multiple cases

I've written a few posts about Igor acting strangely lately (I'm an amateur programmer, so some of my habits may caused me to abuse the language a bit). But this is by far the strangest. I've noticed lately that basic "if-else" statements don't behave the way they should in Igor. For instance, define a function:

Function choices(angleChoice)
    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


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


•choices2(0)
  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.
From the Igor help, notice the lack of a space in the elseif keyword:

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

Function choices(angleChoice)
    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.
And we presume that your code is a stand-in for something more complex, but for what you are showing, a switch statement would be preferred:
    switch(angleChoice)
        case 1:
            print "ONE"
            break;
        case 2:
            print "TWO"
            break;
        case 3:
            print "THREE"
            break;
    endswitch


John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
Wow, that explains a lot... I read "elseif" as "else if" in the Igor manual and didn't think to try the alternative.

Thanks for responding so fast!
You also ignored Igor's helpful offer to treat extra text after a flow control statement as an error. Your first example then became

Function choices(angleChoice)
    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.
jtigor wrote:
Maybe this should be treated as an error.

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:
function test()

    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
ExtraTextWarning.png
johnweeks wrote:
jtigor wrote:
Maybe this should be treated as an error.

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:
function test()

    if(1)
    else if (2)
    endif
end


Yep, I saw that. I meant the following might be considered an error

Function choices(angleChoice)
    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).