5 Replies Latest reply on Apr 6, 2010 6:21 AM by comment_1

    Whats better Case or If

    grinch

      Title

      Whats better Case or If

      Post

      Although they can be used interchangeably what is better to use with a long list of conditionals If statement of Case statements?

       

      For Example:

       

      If x = 1 or x = 3 or x = 5 or x = 7  Do thing a

      If x = 2 or x = 4 or x = 6 or x = 8 Do thing b

       

      Or

      Case

      x = 1 do thing a

      x = 2 do thing b

      x = 3 do thing a

      x = 4 do thing b

      x = 5 do thing a

      x = 6 do thing b

      x = 7 do thing a

      x = 8 do thing b

       

      Both achieve the same result, but other then the case statement being more readable, is there other advantages or disadvantages of either method?

       

      Thanks

      Chris

       

        • 1. Re: Whats better Case or If
          Steve Wright

          You  could also write

           

          Case (

          x = 1 or x = 3 or x = 5 or x = 7 ; Do thing a ;

          x = 2 or x = 4 or x = 6 or x = 8 ; Do thing b

          )

           

          Again, the difference is no need to nest if's which could get rather ugly and un-readable.

          Case, makes it easier to add to at a later date too.

           

          But as for any other advantages such as performance, there's none I know about,  they both short-circuit when a valid match is found.

           

          • 2. Re: Whats better Case or If
            LaRetta_1

             

            If ( x = 1 or x = 3 or x = 5 or x = 7  ; Do thing a

            Do thing b )

             

            Or

            Case (

            x = 1 or x = 3 or x = 5 or x = 7 ; Do thing a ;

            x = 2 or x = 4 or x = 6 or x = 8 ; Do thing b ;

            x = 9 or x = 10 ; Do thing c ;

            Do thing d

            )

             

            ... there is no difference except If() will only allow two possible results and Case() allows many. :smileyvery-happy:

             

            UPDATE:  HI SWS, you must have responded just when I started typing!

            • 3. Re: Whats better Case or If
              DLW-BPEX

              As an added comment, your example suggests that in this particular instance you may be simply wanting to distinguish between odd and even conditions. And is "x" always a positive integer?

              If so, you may want to consider using the MOD function. Mod(x;2) will return 0 if x is even.

              Regardless of whether you go with IF or CASE, this could be a simpler approach, and one that would not need to be modified if more values of "x" had to be included later.

              If my presumption is wrong and you were only giving a simple example that happened to have a pattern, then please disregard.

              David

              • 4. Re: Whats better Case or If
                grinch

                Yes, just giving a supper simple example.  In reality I am evaluating an extremely long list of conditionals, (over 100 items) over a large data set.  Just trying to figure out what is going to be the most efficient way for the program to run.  It could be done either way, and the more I think about it, It is not going to matter.  I need to use case statements for the readability.

                 

                Chris

                 

                • 5. Re: Whats better Case or If
                  comment_1

                   


                  grinch wrote:

                  am evaluating an extremely long list of conditionals, (over 100 items) over a large data set.  Just trying to figure out what is going to be the most efficient way for the program to run.


                   

                  You didn't say what it's really about, but my guess would be a lookup.