‎2007 Sep 05 1:06 PM
Hi,
Can anyone can send me a simple example using the case statment.
‎2007 Sep 05 1:10 PM
Hi,
FORM user_command USING l_ucomm LIKE sy-ucomm
l_selfield TYPE slis_selfield.
DATA: ld_fcode LIKE rsmpe-func.
CASE l_ucomm.
WHEN 'EXIT'.
LEAVE PROGRAM.
WHEN 'XXL'.
*-- Call Excel
PERFORM xxl_ausfuehren.
WHEN 'PFLG'.
PERFORM memory_id_aufbauen.
ENDCASE.
ENDFORM. " user_command
regards
Nicole
‎2007 Sep 05 1:09 PM
Hi,
When you branch conditionally, a processing block is executed or not based on the result of one or more logical conditions. ABAP contains two control structures for conditional branching.
The IF Control Structure
This control structure is introduced with the IF statement. The IF statement allows you to divert the program flow to a particular statement block, depending on a condition. The statement block concludes either with ENDIF, ELSEIF, or ELSE.
IF <condition1>.
<statement block>
ELSEIF <condition2>
<statement block>.
ELSEIF <condition3>.
<statement block>
.....
ELSE.
<statement block>
ENDIF.
To formulate conditions in IF or ELSEIF statements, you can use any logical expression.
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. If the first condition is not true, the program jumps to the next ELSEIF statement and executes it like an IF statement. ELSE begins a statement block which is processed if none of the IF or ELSEIF conditions is true. The end of the last statement block must always be concluded by ENDIF.
You can nest IF control structures. However, the statement blocks must all end within the current processing block. So, for example, an IF - ENDIF block may not contain an event keyword.
DATA: TEXT1(30) VALUE 'This is the first text',
TEXT2(30) VALUE 'This is the second text',
TEXT3(30) VALUE 'This is the third text',
STRING(5) VALUE 'eco'.
IF TEXT1 CS STRING.
WRITE / 'Condition 1 is fulfilled'.
ELSEIF TEXT2 CS STRING.
WRITE / 'Condition 2 is fulfilled'.
ELSEIF TEXT3 CS STRING.
WRITE / 'Condition 3 is fulfilled'.
ELSE.
WRITE / 'No condition is fulfilled'.
ENDIF.
The output is:
Condition 2 is fulfilled.
Here, the second logical expression TEXT2 CS STRING is true because the string "eco" occurs in TEXT2.
The CASE Control Structure
This control structure is introduced with the CASE statement. The CASE control structure allows you to control which statement blocks are processed based on the contents of a data object.
CASE <f>.
WHEN <f11> [OR <f 12> OR ...].
<Statement block>
WHEN <f21>.[OR <f 22> OR ...]
<Statement block>
WHEN <f31> [OR <f 32> OR ...].
<statement block>
WHEN ...
......
WHEN OTHERS.
<statement block>
ENDCASE.
The statement block following a WHEN statement is executed if the contents of <f> are the same as those of one of the fields <f ij >. Afterwards, the program carries on processing after the ENDCASE statement. The statement block after the optional WHEN OTHERS statement is executed if the contents of <f> does not equal any of the <f ij > contents. The last statement block must be concluded with ENDCASE.
The CASE control structure is a shortened form of the following IF structure:
IF <f> = <f11> OR <f> = <f 12> OR <f> = ...
<Statement block>
ELSEIF <f> = <f21> OR <f> = <f 22> OR <f> =...
<Statement block>
ELSEIF <f> = <f21> OR <f> = <f 22> OR <f> =...
<statement block>
ELSEIF <f> = ...
...
ELSE.
<statement block>
ENDIF.
You can nest CASE control structures and also combine them with IF structures. However, they must always end with an ENDCASE statement within the current processing block.
DATA: TEXT1 VALUE 'X',
TEXT2 VALUE 'Y',
TEXT3 VALUE 'Z',
STRING VALUE 'A'.
CASE STRING.
WHEN TEXT1 OR TEXT2.
WRITE: / 'String is', TEXT1, 'OR', TEXT2.
WHEN TEXT3.
WRITE: / 'String is', TEXT3.
WHEN OTHERS.
WRITE: / 'String is not', TEXT1, TEXT2, TEXT3.
ENDCASE.
The output is:
String is not X Y Z
Here, the last statement block after WHEN OTHERS is processed because the contents of STRING, A, does not equal X, Y, or Z.
Regards
Sudheer
‎2007 Sep 05 1:09 PM
Please check ABAPDOCU tcode where you will get all example report for ABAP Programming.
Of Case statement :
Case <field-name>.
when <value1>
executable or condition statement/s
when <value2>
executable or condition statement/s
when <value3>
executable or condition statement/s
.......
others.
Endcase.
‎2007 Sep 05 1:10 PM
Hi,
Check this from help
Example
DATA: ONE TYPE I VALUE 1,
THREE TYPE P VALUE 3.
FOUR TYPE P VALUE 4.
DO 4 TIMES.
CASE SY-INDEX.
WHEN ONE.
WRITE / 'That is'.
WHEN 2.
WRITE 'a'.
WHEN THREE.
WRITE 'good'.
WRITE 'example'.
WHEN OTHERS.
WRITE '!'.
ENDCASE.
ENDDO.
Regards,
Suruchi
‎2007 Sep 05 1:10 PM
Hi,
FORM user_command USING l_ucomm LIKE sy-ucomm
l_selfield TYPE slis_selfield.
DATA: ld_fcode LIKE rsmpe-func.
CASE l_ucomm.
WHEN 'EXIT'.
LEAVE PROGRAM.
WHEN 'XXL'.
*-- Call Excel
PERFORM xxl_ausfuehren.
WHEN 'PFLG'.
PERFORM memory_id_aufbauen.
ENDCASE.
ENDFORM. " user_command
regards
Nicole
‎2007 Sep 05 1:11 PM
Hi,
parameters:num value '1'.
case num.
when '1'.
write:/ 'one'.
when '2'.
write:/ 'two'.
when others.
write:/ 'other than one and two'.
endcase.
rgds,
bharat.
‎2007 Sep 05 1:11 PM
Hi,
You can refer code:
FORM user_command USING p_ucomm TYPE sy-ucomm
p_selfield TYPE slis_selfield.
CASE p_ucomm.
WHEN 'MAIL'.
PERFORM email.
WHEN 'LIST'.
PERFORM display_list.
ENDCASE.
ENDFORM. " Subroutine to handle buttons on ALV toolbar
Reward points for useful answers.
‎2007 Sep 05 1:16 PM
Hi,
<u><b>The CASE Control Structure:</b></u>
This control structure is introduced with the CASE statement. The CASE control structure allows you to control which statement blocks are processed based on the contents of a data object.
CASE <f>.
WHEN <f11> [OR <f12> OR ...].
<Statement block>
WHEN <f21>.[OR <f22> OR ...]
<Statement block>
WHEN <f31> [OR <f32> OR ...].
<statement block>
WHEN ...
......
WHEN OTHERS.
<statement block>
ENDCASE.
The statement block following a WHEN statement is executed if the contents of <f> are the same as those of one of the fields <fij>. Afterwards, the program carries on processing after the ENDCASE statement. The statement block after the optional WHEN OTHERS statement is executed if the contents of <f> does not equal any of the <fij> contents. The last statement block must be concluded with ENDCASE.
The CASE control structure is a shortened form of the following IF structure:
IF <f> = <f11> OR <f> = <f12> OR <f> = ...
<Statement block>
ELSEIF <f> = <f21> OR <f> = <f22> OR <f> =...
<Statement block>
ELSEIF <f> = <f21> OR <f> = <f22> OR <f> =...
<statement block>
ELSEIF <f> = ...
...
ELSE.
<statement block>
ENDIF.
You can nest CASE control structures and also combine them with IF structures. However, they must always end with an ENDCASE statement within the current processing block.
Ex:
DATA: TEXT1 VALUE 'X',
TEXT2 VALUE 'Y',
TEXT3 VALUE 'Z',
STRING VALUE 'A'.
CASE STRING.
WHEN TEXT1 OR TEXT2.
WRITE: / 'String is', TEXT1, 'OR', TEXT2.
WHEN TEXT3.
WRITE: / 'String is', TEXT3.
WHEN OTHERS.
WRITE: / 'String is not', TEXT1, TEXT2, TEXT3.
ENDCASE.
The output is:
String is not X Y Z
Here, the last statement block after WHEN OTHERS is processed because the
contents of STRING, A, does not equal X, Y, or Z.
Regards,
Bhaskar
‎2007 Sep 05 1:31 PM
Hi Vighnesh,
The CASE Control Structure
This control structure is introduced with the CASE statement. The CASE control structure allows you to control which statement blocks are processed based on the contents of a data object.
CASE <f>.
WHEN <f11> [OR <f 12> OR ...].
<Statement block>
WHEN <f21>.[OR <f 22> OR ...]
<Statement block>
WHEN <f31> [OR <f 32> OR ...].
<statement block>
WHEN ...
......
WHEN OTHERS.
<statement block>
ENDCASE.
The statement block following a WHEN statement is executed if the contents of <f> are the same as those of one of the fields <f ij >. Afterwards, the program carries on processing after the ENDCASE statement. The statement block after the optional WHEN OTHERS statement is executed if the contents of <f> does not equal any of the <f ij > contents. The last statement block must be concluded with ENDCASE.
The CASE control structure is a shortened form of the following IF structure:
IF <f> = <f11> OR <f> = <f 12> OR <f> = ...
<Statement block>
ELSEIF <f> = <f21> OR <f> = <f 22> OR <f> =...
<Statement block>
ELSEIF <f> = <f21> OR <f> = <f 22> OR <f> =...
<statement block>
ELSEIF <f> = ...
...
ELSE.
<statement block>
ENDIF.
You can nest CASE control structures and also combine them with IF structures. However, they must always end with an ENDCASE statement within the current processing block.
DATA: TEXT1 VALUE 'X',
TEXT2 VALUE 'Y',
TEXT3 VALUE 'Z',
STRING VALUE 'A'.
CASE STRING.
WHEN TEXT1 OR TEXT2.
WRITE: / 'String is', TEXT1, 'OR', TEXT2.
WHEN TEXT3.
WRITE: / 'String is', TEXT3.
WHEN OTHERS.
WRITE: / 'String is not', TEXT1, TEXT2, TEXT3.
ENDCASE.
The output is:
String is not X Y Z
Here, the last statement block after WHEN OTHERS is processed because the contents of STRING, A, does not equal X, Y, or Z.
Regards,
Kumar.
‎2007 Sep 06 9:53 AM
Hi,
CASE statement example.....
CASE Name.
When 'a'.
Write 'a'.
When 'b'.
Write 'b'.
When 'c'.
Write 'c'.
ENDCASE.
Depending on the value of Name (It could be a or b or c ) the corresponding write statement is executed.(Works similar to IF statement).
Reward for helpful answers**********
Regards,
Vijay
‎2007 Sep 06 11:21 AM
hi,
go throught it, a simple ex: for case u can understand it easily.
TABLES: mara,makt.
SELECT-OPTIONS: s_matnr FOR mara-matnr.
DATA: BEGIN OF t_mara OCCURS 0,
matnr TYPE mara-matnr, "material number.
ersda TYPE mara-ersda, "creation date.
END OF t_mara.
DATA: BEGIN OF t_makt OCCURS 0,
matnr TYPE makt-matnr, "material number.
maktx TYPE makt-maktx, "material description.
END OF t_makt.
START-OF-SELECTION.
SELECT matnr
ersda
FROM mara INTO TABLE t_mara
WHERE matnr IN s_matnr.
LOOP AT t_mara.
WRITE: /10 t_mara-matnr HOTSPOT ON,
30 t_mara-ersda.
HIDE t_mara-matnr.
ENDLOOP.
END-OF-SELECTION.
AT LINE-SELECTION.
CASE sy-lsind. "list index
WHEN 1.
SELECT matnr maktx FROM makt INTO TABLE t_makt
WHERE matnr = t_mara-matnr AND spras = sy-langu.
LOOP AT t_makt.
WRITE: / t_makt-matnr,
t_mara-ersda,
t_makt-maktx.
ENDLOOP.
ENDCASE.
<b>please reward points if helpfull.</b>
with regards,
radhika kolluru.