07_ABAP - Common Control Statements

Embed Size (px)

Citation preview

  • 7/30/2019 07_ABAP - Common Control Statements

    1/36

    ABAP - Common Control

    Statements

  • 7/30/2019 07_ABAP - Common Control Statements

    2/36

    Contents

    Objectives

    Control Statements

    Using the IF Statement

    Using the CASE Statement

    Using the EXIT Statement

    Using the DO Statement

    Using the WHILE Statement

    Using the CONTINUE Statement

    Using the CHECK Statement

    Comparing the EXIT, CONTINUE, and CHECK Statements

    Simple Position and Length Specifications for the WRITE Statement

    Summary

    Q&A

  • 7/30/2019 07_ABAP - Common Control Statements

    3/36

    Contents

    Objectives

  • 7/30/2019 07_ABAP - Common Control Statements

    4/36

    Objectives

    Code the common control statements IF,

    CASE, DO, and WHILE

    Control program sequence using EXIT,CONTINUE, and CHECK

    Code simple position and length specificationson the WRITE statement

  • 7/30/2019 07_ABAP - Common Control Statements

    5/36

    Contents

    Using the IF Statement

  • 7/30/2019 07_ABAP - Common Control Statements

    6/36

    IF Statement

    Syntax: if [not] exp [ and [not] exp ] [ or [not] exp ].

    ---

    [elseif exp.

    ---][else.

    ---]

    endif.

    where: exp is a logical expression that evaluates to a true or false

    condition.

    --- represents any number of lines of code. Even zero lines areallowed.

  • 7/30/2019 07_ABAP - Common Control Statements

    7/36

    IF Statement

  • 7/30/2019 07_ABAP - Common Control Statements

    8/36

    IF Statement

    Data Conversion of Literals During Comparisons

  • 7/30/2019 07_ABAP - Common Control Statements

    9/36

    IF Statement

    REPORT y_vu_1001.

    DATA: BEGIN OF s1,x VALUE 'X',

    y VALUE 'Y',

    z VALUE 'Z',

    END OF s1,

    BEGIN OF s2,

    x VALUE 'X',

    z VALUE 'Z',

    END OF s2.

    IF s1-x = s2-x.

    WRITE: / s1-x, '=', s2-x.

    ELSE.

    WRITE: / s1-x, '', s2-x.

    ENDIF.

    IF s1-x BETWEEN s2-x AND s2-z.

    WRITE: / s1-x, 'is between', s2-x, 'and', s2-z.

    ELSE.

    WRITE: / s1-x, 'is not between', s2-x, 'and', s2-z.

    ENDIF.

    IF s1 = s2. "comparing field strings byte by byte

    WRITE: / 's1 = s2'.

    ELSE.

    WRITE: / 's1 s2'.

    ENDIF.

    IF 0 = ' '. "Watch out for this one

    WRITE: / '0 = '' '''.

    ELSE.

    WRITE: / '0 '' '''.

    ENDIF.

  • 7/30/2019 07_ABAP - Common Control Statements

    10/36

    Using ELSEIF

    to avoid nesting IFs

    IF f1 = f2.

    WRITE: / f1, '=', f2.

    ELSEIF f1 = f3.

    WRITE: / f1, '=', f3.ELSEIF f2 = f3.

    WRITE: / f2, '=', f3.

    ELSE.

    WRITE: / 'all fields are different'.

    ENDIF.

    IF f1 = f2.

    WRITE: / f1, '=', f2.

    ELSE.

    IF f1 = f3.WRITE: / f1, '=', f3.

    ELSE.

    IF f2 = f3.

    WRITE: / f2, '=', f3.

    ELSE.

    WRITE: / 'all fields are different'.

    ENDIF.ENDIF.

    ENDIF.

  • 7/30/2019 07_ABAP - Common Control Statements

    11/36

    Character String Operators

  • 7/30/2019 07_ABAP - Common Control Statements

    12/36

    Character String Operators

    'AABB' co 'AB'

    'ABCD' co 'ABC'

    'AABB' cn 'AB'

    'ABCD' cn 'ABC'

    'AXCZ' ca 'AB'

    'ABCD' ca 'XYZ'

    'AXCZ' na 'ABC'

    'ABCD' na 'XYZ'

    True

    False

    False

    True

    True

    False

    False

    True

  • 7/30/2019 07_ABAP - Common Control Statements

    13/36

    CP contains pattern

  • 7/30/2019 07_ABAP - Common Control Statements

    14/36

    Contents

    Using the CASE Statement

  • 7/30/2019 07_ABAP - Common Control Statements

    15/36

    CASE Statement

    Syntax: case v1.

    when v2 [ or vn ... ].

    ---

    when v3 [ or vn ... ].---

    [ when others.

    --- ]

    endcase.

    where: v1 or v2 can be a variable, literal, constant, or field string.

    --- represents any number of lines of code. Even zero lines areallowed.

  • 7/30/2019 07_ABAP - Common Control Statements

    16/36

    CASE Statement

    REPORT y_vu_1005.

    PARAMETERS f1 TYPE i DEFAULT 2.

    CASE f1.

    WHEN 1. WRITE / 'f1 = 1'.

    WHEN 2. WRITE / 'f1 = 2'.WHEN 3. WRITE / 'f1 = 3'.

    WHEN OTHERS. WRITE / 'f1 is not 1, 2, or 3'.

    ENDCASE.

  • 7/30/2019 07_ABAP - Common Control Statements

    17/36

    Contents

    Using the EXIT Statement

  • 7/30/2019 07_ABAP - Common Control Statements

    18/36

    EXIT Statement

    Syntax: exit.

    prevents further processing from occurring

    in main program: exits program

    inside LOOP, SELECT, DO, WHILE: leaves loop processing

    inside subroutine: leaves subroutine

    REPORT y_vu_1006.

    WRITE: / 'This line is showed.'.EXIT.

    WRITE: / 'This line is not showed.'.

  • 7/30/2019 07_ABAP - Common Control Statements

    19/36

    Contents

    Using the DO Statement

  • 7/30/2019 07_ABAP - Common Control Statements

    20/36

    DO Statement

    Syntax:

    do [ v1 times ]

    ---

    [exit.]---

    enddo.

    where: v1 is a variable, literal, or constant.

    SY-INDEX: the current iteration number

  • 7/30/2019 07_ABAP - Common Control Statements

    21/36

    DO Statement

    sy-index = 99.

    WRITE: / 'before loop, sy-index =', sy-index, / ''.DO 5 TIMES.

    WRITE sy-index.

    ENDDO.

    WRITE: / 'after loop, sy-index =', sy-index, / ''.

    DO 4 TIMES.

    WRITE: / 'outer loop, sy-index =', sy-index.DO 3 TIMES.

    WRITE: / ' inner loop, sy-index =', sy-index.

    ENDDO.

    ENDDO.

    WRITE: / ''. "new line

    DO 10 TIMES.WRITE sy-index.

    IF sy-index = 3.

    EXIT.

    ENDIF.

    ENDDO.

  • 7/30/2019 07_ABAP - Common Control Statements

    22/36

    Contents

    Using the WHILE Statement

  • 7/30/2019 07_ABAP - Common Control Statements

    23/36

    WHILE Statement

    similar to DO statement

    Syntax:

    while exp.

    ---

    [ exit. ]

    ---

    endwhile. where:

    exp is a logical expression.

  • 7/30/2019 07_ABAP - Common Control Statements

    24/36

    Contents

    Using the CONTINUE

    Statement

  • 7/30/2019 07_ABAP - Common Control Statements

    25/36

    CONTINUE Statement

    only within a loop

    terminates the current loop pass

    jumps to the beginning of the next loop pass

    DO 10 TIMES.

    IF sy-index BETWEEN 3 AND 8.CONTINUE.

    ENDIF.

    WRITE sy-index.

    ENDDO.

  • 7/30/2019 07_ABAP - Common Control Statements

    26/36

    Contents

    Using the CHECK Statement

  • 7/30/2019 07_ABAP - Common Control Statements

    27/36

    CHECK Statement

    Syntax: CHECK exp.

    within a loop

    If exp is TRUE: does nothing

    If exp is FALSE: same as CONTINUE

    terminates the current loop pass

    jumps to the beginning of the next loop pass

    DO 10 TIMES.

    CHECK NOT sy-index BETWEEN 3 AND 8.

    WRITE sy-index.

    ENDDO.

  • 7/30/2019 07_ABAP - Common Control Statements

    28/36

    Contents

    Comparing the EXIT,

    CONTINUE, and CHECK

    Statements

  • 7/30/2019 07_ABAP - Common Control Statements

    29/36

    Comparing EXIT, CONTINUE, and

    CHECK

  • 7/30/2019 07_ABAP - Common Control Statements

    30/36

    Contents

    Simple Position and Length

    Specifications for the WRITE

    Statement

  • 7/30/2019 07_ABAP - Common Control Statements

    31/36

    WRITE Statement

    Syntax:

    write [/][P][(L)] v1.

    where:

    v1 is a variable, literal, or constant.

    P is a number indicating a position on the current

    line of output.

    L is a number indicating the number of bytesallocated in the list for outputting v1.

  • 7/30/2019 07_ABAP - Common Control Statements

    32/36

    WRITE Statement

  • 7/30/2019 07_ABAP - Common Control Statements

    33/36

    WRITE Statement

    WRITE: / '....+....1....+....2....+....3....+....4',

    /1 'Hi', 4 'There',

    /1 'Hi', 3 'There',

    /1 'Hi', 2 'There',

    /1 'Hi', 1 'There',/2 'Hi', 'There',

    /2(1) 'Hi', 'There',

    /2(3) 'Hi', 'There',

    /2 'Hi', 10(3) 'There'.

    ....+....1....+....2....+....3....+....4

    Hi There

    HiThere

    HThere

    There

    Hi There

    H There

    Hi There

    Hi The

  • 7/30/2019 07_ABAP - Common Control Statements

    34/36

    Contents

    Summary

  • 7/30/2019 07_ABAP - Common Control Statements

    35/36

    Summary

    Comparison statements: IF and CASE CASE: compares 2 values only and only for equality Conversions occur when comparing differing data types string comparisons

    CP and NP to match strings against patterns

    SY-FDPOS is set after each comparison Loop statements: DO and WHILE

    SY-INDEX: contains the counter for the current loop pass

    EXIT, CONTINUE, and CHECK statements: to modify loopprocessing.

    EXIT: leaves the current loop. CONTINUE: unconditional jump to the end of the loop CHECK exp: jumps to the end of the loop ifexp is false

    WRITE statement: position and length

  • 7/30/2019 07_ABAP - Common Control Statements

    36/36

    Summary