Skip to Content
avatar image
Former Member

Which is better CASE... ENDCASE or IF ELSEIF ENDIF.

Hi All,

I have a generic query.Which is better performance wise,

use of CASE ... ENDCASE or use of IF ELSEIF ENDIF.

If i have multiple else is it better to use Case statement.

Please suggest.

Thanks in Advance,

Saket.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

14 Answers

  • Oct 17, 2008 at 06:45 AM

    CASI is slightly better performance wise. However there are cases when you cannot substitute IF..ELSEIF..ENDIF with CASE, because of the conditions (for example when you have to compare a value with a range)

    Add comment
    10|10000 characters needed characters exceeded

    • hi Thomas,

      I don't know if it can be measured at all, definetly it is only microseconds (or even less). If there are performance problems in a program, it is not because of unproper use of IF.... 😊

      However it is stated in SE38 ==> Environment ==> Examples ==> Performance examples : "CASE statements are clearer and a little faster than IF-constructions. "

      Of course I fully agree on your statement about "functionality and readability", I personally prefer CASE, whenever possible 😊

  • Oct 17, 2008 at 08:20 AM

    Hi,

    CASE..ENDCASE will be having Higher Performance. You can use that.

    Regards,

    Neenu

    Add comment
    10|10000 characters needed characters exceeded

  • Oct 17, 2008 at 09:09 AM

    CASE is slightly faster

    IF is more general.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 17, 2008 at 11:30 AM

    Hi,

    if you are checking multiple values in single field then use CASE statment.

    if you are checking multiple values in multiple fields then use IF ELSEIF statment.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 17, 2008 at 11:47 AM

    Hi,

    In case you are putting more than 5 IF statements within a Processing Block...it is advisable to use CASE Statement....

    Case is easy to understand and better in performance....use Exit statement in the end of every WHEN code block....so as to save prog from unnecessary processing......

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 17, 2008 at 03:22 PM

    Hi,

    performance wise its better to go with CASE....ENDCASE than with IF ELSEIF ENDIF.

    Thanks,

    abaper007

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 17, 2008 at 03:43 PM

    Hmmmm...

    Rather than giving opinions, let's find out:

    REPORT ztest LINE-SIZE 80 MESSAGE-ID 00.
    
    DATA: f(1)    VALUE 'E',
          start   TYPE i,
          end     TYPE i,
          dif     TYPE i.
    
    DO 5 TIMES.
      GET RUN TIME FIELD start.
      IF f = 'A'.
      ELSEIF f = 'B'.
      ELSEIF f = 'C'.
      ELSEIF f = 'D'.
      ELSEIF f = 'E'.
      ELSEIF f = 'F'.
      ELSEIF f = 'G'.
      ELSEIF f = 'H'.
      ELSEIF f = 'I'.
      ELSEIF f = 'J'.
      ENDIF.
      GET RUN TIME FIELD end.
      dif = end - start.
      WRITE: /001 'Time for IF/ELSEIF', ':', dif, 'microseconds'.
    
      GET RUN TIME FIELD start.
      CASE f.
        WHEN 'A'.
        WHEN 'B'.
        WHEN 'C'.
        WHEN 'D'.
        WHEN 'E'.
        WHEN 'F'.
        WHEN 'G'.
        WHEN 'H'.
        WHEN 'I'.
        WHEN 'J'.
      ENDCASE.
      GET RUN TIME FIELD end.
      dif = end - start.
      WRITE: /001 'Time for CASE     ', ':', dif, 'microseconds'.
      SKIP 1.
    ENDDO.

    Rob

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 18, 2008 at 08:51 AM

    Hello,

    If you are using 1 or 3 conditions i prefer berrer using IF and ENDIF.

    If you have more then 3 conditions better go for CASE......... ENDCASE.

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      hi ,

      case is always much more efficent ....because while you are using if else condtion ...it will check all the condition ...untill it will get get a true condtion ...

      while you are using case...it will only go to the true condtion .

      Thanks and regards

      Priyank dixit

  • avatar image
    Former Member
    Oct 19, 2008 at 10:47 AM

    Hi Saket,

    Great discussions and great answers for your post.

    I want to tell you my view too..there will not be much difference on the performance wise if you use case or if statements, its been of syntax it will be very much convenient if we use case statement when there is more searches rather if statement becomes big job validating say 20 statements.

    Even when we upload records using BDC we use CASE since we can check each column and row in a sequence rather if.

    Cheers!!

    Balu

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 22, 2008 at 06:16 AM

    its always better to use case end case rather than if endif unless there are only 2 or 3 conditions(for which if end if is better)..

    regards

    palak

    Add comment
    10|10000 characters needed characters exceeded