case statment

ta5ta5 Member Posts: 1,164
Hi

There is a special possibility with case statement
CASE TRUE OF
  Expression1:; //do something
  Expression2:; //do something
  ELSE //do something
END;

Personally I think a construction like this is a bit easier to read than subsequent if or else if statements. What is your opinion?
Regards
Thomas

Answers

  • einsTeIn.NETeinsTeIn.NET Member Posts: 1,050
    I know this possibility for a long time. The problem I have with it is that it'll only execute the first condition that matches. If there are more conditions that are true it won't execute the code of it.
    "Money is likewise the greatest chance and the greatest scourge of mankind."
  • lvanvugtlvanvugt Member Posts: 774
    ta5 wrote:
    Personally I think a construction like this is a bit easier to read than subsequent if or else if statements. What is your opinion?
    Agree
    ... it'll only execute the first condition that matches ...
    This is also the case with if ... else (if)
    Luc van Vugt, fluxxus.nl
    Never stop learning
    Van Vugt's dynamiXs
    Dutch Dynamics Community
  • einsTeIn.NETeinsTeIn.NET Member Posts: 1,050
    If we are talking about nested IF statements that's true. But how would you replace nested IF statements with the construction above?
    "Money is likewise the greatest chance and the greatest scourge of mankind."
  • ta5ta5 Member Posts: 1,164
    problem I have with it is that it'll only execute the first condition that matches. If there are more conditions that are true it won't execute the code of it.

    That's why I like it :mrgreen:
  • einsTeIn.NETeinsTeIn.NET Member Posts: 1,050
    My personal feeling is that it would be better if you could decide if you want to leave the case statement or not. Like in other programming languages where you explicitly need to set a break statement.
    "Money is likewise the greatest chance and the greatest scourge of mankind."
  • vaprogvaprog Member Posts: 1,141
    The above CASE statement is functionally the same as (more conditions added)
    IF Expression1 THEN BEGIN
      ; //do something
    END ELSE IF Expression2 THEN BEGIN
      ; //do something
    ...
    END ELSE IF Expression_n THEN BEGIN
      ; //do something
    END ELSE BEGIN
      //do something
    END;
    
    Which is just my way of writing a series of IF statements where each else consists of exactly one IF THEN ELSE statement.
    IF Expression1 THEN BEGIN
      ; //do something
    END ELSE 
      IF Expression2 THEN BEGIN
        ; //do something
      END ELSE
    ...
                      IF Expression_n THEN BEGIN
                        ; //do something
                      END ELSE BEGIN
                        //do something
                      END;
    
    Many modern languages actually have an IF ... THEN ... ELSIF ... ELSE statement which my code layout mimics and is more readable than the nested IFs, as is the CASE TRUE OF. The latter is less familiar to many programmers, and especially those coming from C and languages with a switch statement patterned after C tend to have problems remembering:
    • The first, and only the first matching section is executed.
    • There is no fall-trough, so subsequent sections are out of reach, no break needed.
    The nested IF .. ELSE IF sequence is free from both and thus more easily readable.
  • ta5ta5 Member Posts: 1,164
    Thanks for your contributions, very welcome. I've set the topic to solved.
    Regards
    Thomas
  • rocatisrocatis Member Posts: 163
    ta5 wrote:
    Personally I think a construction like this is a bit easier to read than subsequent if or else if statements. What is your opinion?
    I use the CASE TRUE OF extensively. I find the alternative (IF THEN ELSE IF THEN...) completely unreadable.
    Brian Rocatis
    Senior NAV Developer
    Elbek & Vejrup
Sign In or Register to comment.