Needing Multiple "ENDIF" after "ELSEIF" statements

I’m wondering why I get a syntax error some of the time when I use IF statements with multiple ELSE IF conditions unless it has an END IF for each condition, but not every time.

In my first example, I made a function that is comparing which gripper on my end of arm tool & which buffer nest are selected to pick or place a part with. When I use my IF statement with the possible ELSE IF conditions, it has an error unless I have an END IF for every condition.

However in my second example, my proc has the main IF statement with two ELSE IF conditions (as well as another nested statement), but only required a single END IF.

I’m not sure what is different about these examples that makes the first example need so many END IFs.

Example 1:

!**************************************************************************
!* Determine which position the robot is going to for the buffer nests
!* Depends on which buffer nest & which gripper is selected
!**************************************************************************

FUNC num BNPosition(num nBufferNest, num nGripper)
    
    VAR num nBufferNestPosition;
    

IF (nBufferNest = 0 AND nGripper = 3) THEN 

    nBufferNestPosition := 0;

ELSE IF ((nBufferNest = 0 AND nGripper = 2) OR (nBufferNest = 1 AND nGripper = 3)) THEN

    nBufferNestPosition := 1;

ELSE IF ((nBufferNest = 0 AND nGripper = 1) OR (nBufferNest = 1 AND nGripper = 2) OR (nBufferNest = 2 AND nGripper = 3)) THEN

    nBufferNestPosition := 2;

ELSE IF ((nBufferNest = 0 AND nGripper = 0) OR (nBufferNest = 1 AND nGripper = 1) OR (nBufferNest = 2 AND nGripper = 2)) THEN

    nBufferNestPosition := 3;

ELSE IF ((nBufferNest = 1 AND nGripper = 0) OR (nBufferNest = 2 AND nGripper = 1)) THEN

    nBufferNestPosition := 4;

ELSE IF (nBufferNest = 2 AND nGripper = 0) THEN

    nBufferNestPosition := 5;
    

ENDIF
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF




RETURN nBufferNestPosition;



ENDFUNC

Example 2:

!**************************************************************************
!* Move the robot home at the selected speed & zone
!* If the part has been inserted to interference, assembly must be moved clear
!* If the part has been partially inserted, it can be lifted straight out
!* If the part has begun glue/cure, it must be moved clear
!**************************************************************************

PROC Home(speeddata HomeSpeed,zonedata HomeZone) !Slow home
    
    VAR bool bPreInsert;
    VAR robtarget pInsert;
    
    jCurr := CJointT();
    pCurr := CRobT(\Tool:=tool1 \WObj:=wobj0);


    IF  (EIP_OUT64 = 0 AND EIP_OUT65 = 0) THEN
        !Raise Z
        pCurr.trans.z := pHome.trans.z-1;
        MoveJ pCurr, HomeSpeed, fine, tool1\WObj:=wobj0;
        
        MoveJ pHome, HomeSpeed, HomeZone, tool1\WObj:=wobj0;
        
    
    ELSEIF  (EIP_OUT64 = 1) THEN
        
        IF (bPreInsert = FALSE) THEN
            
            MoveL Offs(pInsert,0,0,+8),v300,fine,tool1\WObj:=wobj0;
            MoveL Offs(pInsert,0,+30,+8),v300,z0,tool1\WObj:=wobj0;
            MoveJ Offs(pInsert,0,+100,+50),v300,z10,tool1\WObj:=wobj0;
            MoveJ pHome, HomeSpeed, HomeZone, tool1\WObj:=wobj0;
     
        ELSEIF (bPreInsert = TRUE) THEN
            
            MoveL Offs(pInsert,0,0,+40),v100,z10,tool1\WObj:=wobj0;
            MoveJ Offs(pHome,0,0,0),v300,fine,tool1\WObj:=wobj0;   
            
            bPreInsert := FALSE;
            
        ENDIF
        
        
    ELSEIF  (EIP_OUT65 = 1) THEN
        MoveJ Offs(pInsert,0,0,0),v100,z0,tool1\WObj:=wobj0;
        MoveJ Offs(pInsert,+10,0,+50),v100,z10,tool1\WObj:=wobj0;
        MoveJ Offs(pInsert,0,+100,+50),v100,z10,tool1\WObj:=wobj0;           
        MoveJ pHome, HomeSpeed, HomeZone, tool1\WObj:=wobj0;
        
    ENDIF
    
    
    
ENDPROC

I would say the obvious difference is example 1 uses ELSE IF

Example 2 uses ELSEIF

ELSE IF would be interpreted as an else statement containing a further IF statement

That seemed to me peculiar and it stood out in your second example in contrast is “ELSEIF” like I have always seen in use. Try changing your first example to ELSEIF without the spaces and let us know, please. I really didn’t think that we could use it like that, with the space.

Example 1 is behaving as:

IF (nBufferNest = 0 AND nGripper = 3) THEN

nBufferNestPosition := 0;

ELSE
IF ((nBufferNest = 0 AND nGripper = 2) OR (nBufferNest = 1 AND nGripper = 3)) THEN

    nBufferNestPosition := 1;

ELSE
     IF ((nBufferNest = 0 AND nGripper = 1) OR (nBufferNest = 1 AND nGripper = 2) OR (nBufferNest = 2 AND nGripper = 3)) THEN

        nBufferNestPosition := 2;

    ELSE 
        IF ((nBufferNest = 0 AND nGripper = 0) OR (nBufferNest = 1 AND nGripper = 1) OR (nBufferNest = 2 AND nGripper = 2)) THEN

        nBufferNestPosition := 3;

        ELSE 
            IF ((nBufferNest = 1 AND nGripper = 0) OR (nBufferNest = 2 AND nGripper = 1)) THEN

                 nBufferNestPosition := 4;

            ELSE 
                  IF (nBufferNest = 2 AND nGripper = 0) THEN

                      nBufferNestPosition := 5;
     

                  ENDIF
            ENDIF
        ENDIF
     ENDIF
   ENDIF
ENDIF

Yes! Thank you @mike_10 & @lemster68 that does seem to be it. Once I removed the space, it only needed the single ENDIF. I’m glad it was something that simple, even if I feel a bit silly for missing it. :sweat_smile:

FUNC num BNPosition(num nBufferNest, num nGripper)
    
    

    IF (nBufferNest = 0 AND nGripper = 3) THEN 

        nBufferNestPosition := 0;

    ELSEIF ((nBufferNest = 0 AND nGripper = 2) OR (nBufferNest = 1 AND nGripper = 3)) THEN

        nBufferNestPosition := 1;

    ELSEIF ((nBufferNest = 0 AND nGripper = 1) OR (nBufferNest = 1 AND nGripper = 2) OR (nBufferNest = 2 AND nGripper = 3)) THEN

        nBufferNestPosition := 2;

    ELSEIF ((nBufferNest = 0 AND nGripper = 0) OR (nBufferNest = 1 AND nGripper = 1) OR (nBufferNest = 2 AND nGripper = 2)) THEN

        nBufferNestPosition := 3;

    ELSEIF ((nBufferNest = 1 AND nGripper = 0) OR (nBufferNest = 2 AND nGripper = 1)) THEN

        nBufferNestPosition := 4;

    ELSEIF (nBufferNest = 2 AND nGripper = 0) THEN

        nBufferNestPosition := 5;
    

    ENDIF



    RETURN nBufferNestPosition;



ENDFUNC