IF, ELSEIF, and When To Use ENDIF

I see rule 4.0 rules in the forum that have an IF statement, a THEN and then a ENDIF. I also see an IF statement that has a THEN but no ENDIF. This goes for the ELSEIF too.

So my question is when do you use the ENDIF and when do you not? What determines this? Thanks

Please don't take this the wrong way, but try reading the documentation first. Even if you don't get it, at least it'll help lay some concepts for explaining and understanding how RM 4.0 works from a structure point. This stuff can get kind of complex.

The most simple is a Simple Conditional Action. This is the a IF THIS (single condition) then Single Action. There are no ENDIFs with this. it's a single use item. This is the only time you wouldn't use an ENDIF.

The next are IF (conditions) THEN statements where you can have multiple conditions. When you are done with your structure, you need to put an ENDIF on it. Since it's not a "single use item" it needs a bookmark at the end.

Most basic:
IF (condition) <-Starting place and statement to be tested
Actions
END-IF <- Ending place

IF (condition) <-Starting place and statement to be tested
Actions
ELSE IF <-A variation of your starting place.
Actions
END-IF <- Ending Place

I hope this helps.

1 Like

IMO, there should be an ENDIF for every IF.

4 Likes

No offense taken. I read about it but there are just contradictions in the ENDIF use. I see rules posted that don't have an ENDIF in a IF THEN statement and they say they work and I also see rules with ELSEIF statements that don't have an ENDIF and they say the rule works as well.
So I agree with the complexity but there is still a question of why do some rules work with it and why do some rules work without it whether it be a IF THEN or IF or ELSEIF?

If the END-IF is the last action you'd have in the rule, it's technically optional, but I'd consider that RM just being nice and I'd say it's good form to put it in anyway. Does that explain any examples you've seen?

2 Likes

If the rule has run it's course there there is nothing left for it to do. So in reality you can leave them off at time. There are potential times where you may have repeating items that could cause unintended consequences but they may be few and far between.

I have done it by accident or laziness on several occasions (especially when I'm really tired). It is in the best coding practices to always use an ENDIF. That said, I go back and fix them when I see them. It helps with the reading and troubleshooting.

I would agree that it should be included in all IF's because technically you are telling it that the ENDIF is the end of that statement and to move on to the next one or end the rule. I hope this is explained correctly.

I agree as I mentioned above that it is just correct to include it so there is no misunderstanding as to when a statement starts and ends.

These are simply sloppy rule authors, leaving off the closing END-IF. No one enforces that every sentence is supposed to end with a period, and parenthetical remarks are supposed to have a right paren, but you still understand the sentence when it doesn't (like this one

7 Likes

Touche. I will use them always. :smiley: Thanks

1 Like

My question is, if an ENDIF is needed after every if statement, why is it not added automatically behind the scenes? Having it as an action you can add leads people to believe it is optional or some additional thing such as a way to start an entirely new if block.

Presumably because then you'd be stuck having to use "Insert Action Before" every time until you're finished with that block, whereas the most intuitive step (and the easiest since you don't have to wade through a list of actions to choose) is "Insert Action" to put it next in the list.

1 Like

I am traying to understand

IF (Condition1 is false).
Action 1
ELSE IF (coditon2 is false)
ACTION 2
ELSE IF (condition3 is truth)
ACTION3
ELSE (condition4 is truth)
ACTION 4
END IF

what actions will be executed?
I gues only action3. But I am not sure.

Can someone please help. Thanks

correct, only action three will execute

Note, there is no condition on an ELSE by itself, the way you showed it. ELSE is what would happen if the preceding IF or ELSE-IF is false.

IF (1 is true)
   do A
ELSE-IF (2 is true)
   do B
ELSE
   do C
ENDIF

So, if 1 is true, only A happens (irrespective of whether 2 is true or not). If 1 is false and 2 is true, only B happens. If 1 and 2 are both false, C happens.

6 Likes

OK. perfectly understud.

But how will this work for next example:
IF (1 is FALSE)
do A
ELSE-IF (2 is true)
do B
ELSE-IF (3 is true)
do C
ELSE
do D
ENDIF

Will it execute B and C? Or Just B

Just B.

:ok_hand: thanks

This was helpful for me to understand the proper use of ELSE-IF. thank you!

1 Like