ITeye博客

ABAP基本语法(IF/CASE/WHILE)

vikingwei · 2018-08-14 16:53 · SAP · 行业应用 · Markdown

1.Decisions判断语法.
  a.IF语句:
  IF<condition_1>. 
    <Statements...>. 
  ENDIF.
  --simple IF example:
  Report YH_SEP_15. 
  Data Title_1(20) TYPE C.
  Title_1 = 'Tutorials'. 
  IF Title_1 = 'Tutorials'. 
    write 'This is IF statement'. 
  ENDIF.

  --IF ELSE语法:
  IF<condition_1>. 
     <statement block 1>. 
  ELSE.  
     <statement block 2>. 
  ENDIF.
  --Example:
  Report YH_SEP_15. 
  Data Title_1(20) TYPE C. 
       Title_1 = 'Tutorials'.
  IF Title_1 = 'Tutorial'. 
     write 'This is IF Statement'. 
  ELSE. 
     write 'This is ELSE Statement'.
  ENDIF.

  --复杂IF ELSEIF语法:
  IF<condition_1>.
    <statement block 1>. 
  ELSEIF<condition_2>. 
    <statement block 2>. 
  ELSEIF<condition_3>.
    <statement block 3>.
    ......
    ......
    ......
    ......
  ELSE.
    <statement block>.
  ENDIF.
  --Example:
  Report YH_SEP_15. 
  Data Result TYPE I VALUE 65. 
  IF Result < 0.
    Write / 'Result is less than zero'. 
  ELSEIF Result < 70.
    Write / 'Result is less than seventy'. 
  ELSE.
    Write / 'Result is greater than seventy'. 
  ENDIF.

  --Case语法:
  CASE <field>.
  WHEN <abc>.
     <statement block>.    
  WHEN <def>.
     <tatement block>.    
  WHEN <pqr>.
     <statement block>.
  ......
  ......
  ...... 
  WHEN <xyz>.
     <statement block>.   
  WHEN OTHERS.
     <statement block>. 
  ENDCASE.
  --Example:Yes, this is the title.
  Report YH_SEP_15. 
  Data: Title_1(10) TYPE C, 
     Title_2(15) TYPE C. 
 
  Title_1 = 'ABAP'.
  Title_2 = 'Programming'. 
 
  CASE Title_2.   
  WHEN 'ABAP'.
     Write 'This is not the title'. 
  WHEN 'Tutorials'.
     Write 'This is not the title'. 
  WHEN 'Limited'.
     Write 'This is not the title'. 
  WHEN 'Programming'.
     Write 'Yes, this is the title'. 
  WHEN OTHERS.
     Write 'Sorry, Mismatch'. 
  ENDCASE.

2.循环LOOP,简单来讲只有两种循环:da.
  a.按循环次数执行:DO
  DO [n TIMES].
<statement block>.
  ENDDO.
  --Example:循环15次:
  Report YH_SEP_15.
  Do 15 TIMES.
Write: / 'Hello'.
  ENDDO.

  b.WHILE循环:
  WHILE <logical expression>
<statement block>.
  ENDWHILE.
  --Example:
  REPORT YS_SEP_15.
  DATA: a type i.
  a = 0.
  WHILE a <> 8.
Write: / 'This is the line:', a.
a = a + 1.
  ENDWHILE.
 
  --CHECK,是在循环中使用,当Expression为true则执行之后语句,否则不执行并马继续下一个循环.
  Report YH_SEP_15. 
  DO 5 TIMES. 
    CHECK SY-INDEX BETWEEN 3 AND 4. 
    Write / SY-INDEX. 
  ENDDO.
  --CONTINUE,与其它语言一样.
  Report YH_SEP_15. 
  DO 5 TIMES. 
    IF SY-INDEX = 3.   
      CONTINUE.   
    ENDIF.
    Write / SY-INDEX. 
  ENDDO.
  --EXIT,与其它语言一样.
  Report YH_SEP_15. 
  DO 5 TIMES. 
    IF SY-INDEX = 3. 
      EXIT. 
    ENDIF. 
    Write / SY-INDEX. 
  ENDDO.

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。