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

CASE statement

Former Member
0 Likes
1,028

Hi experts I have written code as like follow:

CASE COMM_STRUCTURE-X.

WHEN '22'.

If COMM_STRUCTURE-wbs_elemt NE ' '.

READ TABLE i_wbs WITH TABLE KEY

co_area = COMM_STRUCTURE-co_area

wbs = COMM_STRUCTURE-wbs_elemt

objvers = 'A'.

RESULT = i_wbs-prfctr.

Else.

RESULT = COMM_STRUCTURE-profit_ctr.

Endif.

endcase.

Now my question is can we have if statement within WHEN, inside CASE ,ENDCASE.Is the above coding is correct?

If COMM_STRUCTURE-wbs_elemt NE ' ' is correct way or

If COMM_STRUCTURE-wbs_elemt NE Initial?

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
968

Hi,

If COMM_STRUCTURE-wbs_elemt NE ' ' is correct...or u can use like If COMM_STRUCTURE-wbs_elemt NE space.. but initial can be use like IF NOT itab[] IS INITIAL..

yes, we can write IF in CASE, and ur coding is correct..

Hope it helps!!

Regards,

Pavan

8 REPLIES 8
Read only

Former Member
0 Likes
969

Hi,

If COMM_STRUCTURE-wbs_elemt NE ' ' is correct...or u can use like If COMM_STRUCTURE-wbs_elemt NE space.. but initial can be use like IF NOT itab[] IS INITIAL..

yes, we can write IF in CASE, and ur coding is correct..

Hope it helps!!

Regards,

Pavan

Read only

0 Likes
968

Hi,

and how about code is that correct:can we do coding or read table within IF....like follow

Case.

When.

IF.

Read table.

Endif.

Endcase.

Read only

0 Likes
968

Hi,

Have a look at the following Code hope will help you.

PARAMETERS: abc TYPE i.

CASE abc.
  WHEN '1' OR '2'.
    IF abc = 1.
      WRITE: 'One'.
    ELSE.
      WRITE: 'Two'.
    ENDIF.
  WHEN '3' or '4'.
    IF abc = 3.
      WRITE: 'Three'.
    ELSE.
      WRITE: 'Four'.
    ENDIF.
  WHEN OTHERS.
    WRITE: 'Number is not Between 1 to 4'.
ENDCASE.

Best Regards,

Faisal

Read only

0 Likes
968

Yes, off course we can use READ within IF...

Rgards,

Pavan

Read only

0 Likes
968

Hi, Pie

Test the following Sample Code to for more Clear idea about Case, if else and Read Table

PARAMETERS: abc TYPE i.

DATA: BEGIN OF it OCCURS 10,
  no TYPE i,
  cno(5),
  END OF it.

it-no = 1. it-cno = 'One'. APPEND it.
it-no = 2. it-cno = 'Two'. APPEND it.
it-no = 3. it-cno = 'Three'. APPEND it.
it-no = 4. it-cno = 'Four'. APPEND it.

CASE abc.
  WHEN '1' OR '2'.
    IF abc = 1.
      READ TABLE it WITH KEY no = abc.
      IF sy-subrc EQ 0.
        WRITE: it-cno.
      ENDIF.
    ELSE.
      READ TABLE it WITH KEY no = abc.
      IF sy-subrc EQ 0.
        WRITE: it-cno.
      ENDIF.
    ENDIF.
  WHEN '3' OR '4'.
    IF abc = 3.
      READ TABLE it WITH KEY no = abc.
      IF sy-subrc EQ 0.
        WRITE: it-cno.
      ENDIF.
    ELSE.
      READ TABLE it WITH KEY no = abc.
      IF sy-subrc EQ 0.
        WRITE: it-cno.
      ENDIF.
    ENDIF.
  WHEN OTHERS.
    WRITE: 'Number is not Between 1 to 4'.
ENDCASE.

Please Reply if any Issue,

Best Regards,

Faisal

Read only

0 Likes
968

Hi All,

Thank you so much for your support.

Your ans willbe rewarded.

Read only

amit_khare
Active Contributor
0 Likes
968

There are different ways to write things based on the situations.

You are doing correct if the data types declared are appropriate with the CASE/IF conditions.

Read only

Former Member
0 Likes
968

HI,

It would be better to use INITIAL instead of ' '. Because it works depending upon the datatype.

If Datatype is I initial value is 0.

Datatype is C initial value is space.

Datatype is N initial value is 0.

You can have the If statement in WHEN. What you have written is right.