Answered Sucession of if else if with some weird behavior

Hello all,

In one on my program I witness a weird behavior.
I had a list of if else if statement like this:
Code:
IF condition1 THEN
    error1.
ELSE IF condition2 THEN
    error2.
ELSE IF condition3 THEN DO:
    something3.
    error3.
END.
ELSE IF condition4 THEN
   error4.
For more precision this list of IF statement is here to stop the validation action if any error like a empty field occured. Maybe this is not the best way of doing it.

If condition 1 2 & 3 where not met and condition 4 was true the code neither goes on condition4.


Do you have any idea why?
 

Osborne

Active Member
The rough example you posted should reach condition4 as this does:
Code:
DEFINE VARIABLE condition1 AS LOGICAL NO-UNDO.
DEFINE VARIABLE condition2 AS LOGICAL NO-UNDO.
DEFINE VARIABLE condition3 AS LOGICAL NO-UNDO.
DEFINE VARIABLE condition4 AS LOGICAL INITIAL YES NO-UNDO.

IF condition1 THEN
    MESSAGE "error1" VIEW-AS ALERT-BOX.
ELSE IF condition2 THEN
    MESSAGE "error2" VIEW-AS ALERT-BOX.
ELSE IF condition3 THEN DO:
    MESSAGE "error3" VIEW-AS ALERT-BOX.
END.
ELSE IF condition4 THEN
   MESSAGE "error4" VIEW-AS ALERT-BOX.

It suggests that your main code is missing something.

A neater solution for using ELSE IF is something like this:

Code:
CASE TRUE:
   WHEN condition1 THEN
      error1.
   WHEN condition2 THEN
      error2.
   WHEN condition3 THEN DO:
      something3.
      error3.
   END.
   WHEN condition4 THEN
      error4.
END CASE.
 
Thank you Osborne. I never thought that I could use the case statement this way.
It's much neater in fact.
I will try this way and see what goes on :)
 
Top