# Needing Multiple "ENDIF" after "ELSEIF" statements

**URL:** <https://tech-community.robotics.abb.com/t/needing-multiple-endif-after-elseif-statements/15147>\
**Category:** RobotStudio\
**Tags:** rapid\
**Created:** [September 18, 2026, 2:16pm UTC](https://tech-community.robotics.abb.com/t/needing-multiple-endif-after-elseif-statements/15147 "2026-09-18T14:16:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![EMInvio](https://avatars.discourse-cdn.com/v4/letter/e/c57346/32.png) [@EMInvio](https://tech-community.robotics.abb.com/u/EMInvio)\
**Post date:** [September 18, 2026, 2:16pm UTC](https://tech-community.robotics.abb.com/t/needing-multiple-endif-after-elseif-statements/15147/1 "2026-09-18T14:16:57Z")

</div>

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  
!\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

```auto
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  
!\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

```auto
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

```

---

<div class="post-metadata">

**Author:** ![mike\_10](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@mike\_10](https://tech-community.robotics.abb.com/u/mike_10)\
**Post date:** [September 18, 2026, 2:33pm UTC](https://tech-community.robotics.abb.com/t/needing-multiple-endif-after-elseif-statements/15147/2 "2026-09-18T14:33:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lemster68](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/lemster68/32/1945_2.png) [@lemster68](https://tech-community.robotics.abb.com/u/lemster68)\
**Post date:** [September 18, 2026, 2:35pm UTC](https://tech-community.robotics.abb.com/t/needing-multiple-endif-after-elseif-statements/15147/3 "2026-09-18T14:35:22Z")

</div>

> [@EMInvio](#):
>
> `ELSE IF`

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.

---

<div class="post-metadata">

**Author:** ![mike\_10](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@mike\_10](https://tech-community.robotics.abb.com/u/mike_10)\
**Post date:** [September 18, 2026, 2:39pm UTC](https://tech-community.robotics.abb.com/t/needing-multiple-endif-after-elseif-statements/15147/4 "2026-09-18T14:39:20Z")

</div>

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

```

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

        nBufferNestPosition := 2;

    ELSE 

```

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

        nBufferNestPosition := 3;

        ELSE 

```

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

                 nBufferNestPosition := 4;

            ELSE 

```

```auto
                  IF (nBufferNest = 2 AND nGripper = 0) THEN

                      nBufferNestPosition := 5;
     

                  ENDIF
            ENDIF
        ENDIF
     ENDIF
   ENDIF
ENDIF

```

---

<div class="post-metadata">

**Author:** ![EMInvio](https://avatars.discourse-cdn.com/v4/letter/e/c57346/32.png) [@EMInvio](https://tech-community.robotics.abb.com/u/EMInvio)\
**Post date:** [September 18, 2026, 3:51pm UTC](https://tech-community.robotics.abb.com/t/needing-multiple-endif-after-elseif-statements/15147/5 "2026-09-18T15:51:26Z")

</div>

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. 😅

```auto
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

```
