Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

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

Former Member
0 Kudos

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.

22 REPLIES 22

JozsefSzikszai
Active Contributor
0 Kudos

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)

0 Kudos

Hi Eric,

is there really a measurable effect compared to the overall runtime of a typical program (some DB access, some internal table handling)?

I think this decision should be made purely in respect to functionality and readability of the program code.

Cheers

Thomas

0 Kudos

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

Former Member
0 Kudos

Hi,

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

Regards,

Neenu

former_member194613
Active Contributor
0 Kudos

CASE is slightly faster

IF is more general.

Former Member
0 Kudos

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.

Former Member
0 Kudos

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......

0 Kudos

>

use Exit statement in the end of every WHEN code block....so as to save prog from unnecessary processing......

???

My ABAP jumps to the next statement after ENDCASE whenever a WHEN was true (and the code part in the WHEN finished).

0 Kudos

Hi,

I believe you are talking about a CASE Statement inside a Loop..Endloop.......whereas I am talking about a different situation....

Like

At-Selection Screen.

Case Sy-lsind.

When '1'.

0 Kudos

OK, I see what you mean, but in your example the EXIT will cause that the program continues with the next event (will jump out from AT SELECTION-SCREEN completely), which is most of the time is not what we want...

0 Kudos

Hi Eric,

I got it.....

I will try to be more specific next time.....

Thanks for this

Former Member
0 Kudos

Hi,

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

Thanks,

abaper007

Former Member
0 Kudos

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

Former Member
0 Kudos

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.

0 Kudos

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

Former Member
0 Kudos

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

Former Member
0 Kudos

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

kesavadas_thekkillath
Active Contributor
0 Kudos

go to tcode se30-> tips & tricks -> If,Case ->If vs Case->Measure run time..

check it urself and conclude

0 Kudos

This will be the docu u will get there

"Documentation

CASE statements are clearer and a little faster than

IF-constructions."

Got it...now just close this thread

former_member194613
Active Contributor
0 Kudos

the useless threads are the longest!

> CASE is slightly faster

> IF is more general.

The IF allows you conditions, for example < > initial etc. , which are simply not possible with the case ... endcase.

Use CASE if the conditions are simple and there are many conditions.

And learn the most important fact:

The difference are microseconds or less, the number of conditions is rarely in the hundrets, so performance-wise they ARE THE SAME! No difference which you will ever notice.

The difference between good and bad selects are seconds or even minutes, these are the points which have an performance impact.

Siegfried

Former Member
0 Kudos

Dear friend,

Please goto Tcode- SE30. In that click on Tips&Tricks. You will be finding entire techniues with example and measuring time.

From the examples u can understand the better one.

regards,

Prashanth maturu.

Former Member
0 Kudos

Thanks for all suggestions.