Application Development and Automation 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: 
Read only

IF and Case statment

Former Member
0 Likes
9,205

Hi ,

Could you please let me know when is an ‘IF’ statement used and when a ‘CASE’ statement used. What is the difference between the way these two nesting works and when is it a mandate to use one and not the other.

Regards,

Veera

Hi ,

Could you please let me know when is an ‘IF’ statement used and when a ‘CASE’ statement used. What is the difference between the way these two nesting works and when is it a mandate to use one and not the other.

Regards,

Veera

6 REPLIES 6
Read only

Former Member
0 Likes
2,972

it is better to use case if u have more conditions to match as it will directly go to that case instead in if it will go to that after checking all above cases

while if u have less then four matching condition u can use if for beter result

so if checks one by one while case directly switch to the correct one

reward if helpful

Edited by: Ashish Paliwal on Mar 27, 2008 7:09 PM

Read only

Former Member
0 Likes
2,972

use if, if you have intervals.

in a case statement you can only validate against a certain value.

Read only

Former Member
0 Likes
2,972

Hi,

if we have conditions 1 or 2 then use if .--condition or else.

if we have number of conditions then case1 case 2 case3....

Thanks,

Read only

Former Member
0 Likes
2,972

Hi,

Branching with IF statement

The IF statement allows you to divert the program flow to a particular statement block, depending on a condition. This statement block consists of all the commands which occur between an IF statement and the next ELSEIF, ELSE, or ENDIF statement.

Syntax

IF<condition1>

<statement block>

ELSE

<statement block>

ENDIF

If the first condition is true, the system executes all the statements up to the end of the first statement block and then continues processing after the ENDIF statement.

To introduce alternative conditions, you can use ELSEIF statements. If the first condition is false, the system processes the following ELSEIF statement in the same way as the IF statement. ELSE begins a statement block which is processed if none of the IF and ELSEIF conditions is true. The end of the last statement block must always be concluded with ENDIF.

IF <condition1>.

<statement block>

ELSEIF <condition2>.

<statement block>

ELSEIF <condition3>.

<statement block>

ELSE.

<statement block>

ENDIF.

ABAP/4 allows unlimited nesting of IF – ENDIF statement blocks, but they must terminate within the same processing block. In other words, an IF – ENDIF block cannot contain an event keyword.

Branching with CASE statement

To execute different statement blocks depending on the contents of particular data fields, you can either use IF statement or the CASE statement as follows:

Syntax

CASE <f>.

WHEN <f1>.

<statement block>

WHEN <f2>.

<statement block>

WHEN <f3>.

<statement block>

WHEN OTHERS.

<statement block>

ENDCASE.

The system executes the statement block after the WHEN statement if the contents of <f> equals the contents of <fi>, and continues processing after the ENDCASE statement. The statement block after the optional WHEN OTHERS statement is executed if the contents of <f> do not equal any of the <fi> contents. The last statement block must be concluded with ENDCASE.

The conditional branching using CASE is a shorter and simpler form of similar processing with IF. When you have many conditions IF becomes more complicated in such cases CASE is used.

Regards,

Bhaskar

Read only

Former Member
0 Likes
2,972

Hi,

IF CHECK ALL THE CONDITIONS

CASE CHECKS ONLY THE SATISFIED CONDITION(TIME SAVING THAN IF)

Regards,

V.Balaji

Read only

Former Member
0 Likes
2,972

Veer..

There isn't a mandate to use either one, however both have there uses.

A very common place for using CASE is in screen programming and processing based on the OK_CODE/UCOMM field.


  l_ok = ok_0500.
  CLEAR ok_0500.

  CASE l_ok.
    WHEN 'SAVE'.
      PERFORM check_required_fields_0500.
      IF l_errors IS INITIAL.
        PERFORM add_follow_up_0500.
        LEAVE TO SCREEN 0.
      ENDIF.
    WHEN 'CNTCHG'.
      CLEAR: zlmfoll-contre.
    WHEN 'DDOWN'.
      CLEAR wk_0500_scr_rec-contre.
      PERFORM contact_result_dd USING wk_0500_scr_rec-cont_ty
                                     'WK_0500_SCR_REC-CONTRE'.

  ENDCASE.

an if statement for this could be a bit tough to read and would likely run a bit slower.


IF l_ok = 'SAVE'.
*  Do something
ELSEIF l_ok = 'CNTCHG'.
* Do something else
ELSEIF l_ok = 'DDOWN'.
* Do yet another thing.
endif.

However, you couldn't code a CASE statement to do this


IF pernr = '00001' and status = 'ACTIVE'.
* then do something
ENDIF.

Additionally, you could not do this with a CASE


IF pernr in r_pernr.
* Do Something
ENDIF.