SAP/ABAP(二)

一、循环结构

*&---------------------------------------------------------------------*
*& Report ZDEMO_LIMING01
*&---------------------------------------------------------------------*
*&作者:黎明
*&---------------------------------------------------------------------*
REPORT zdemo_liming01.
*1while循环实现输出0-7
DATA lv_a TYPE i.
lv_a = 0.

WHILE lv_a < 8. "条件
  WRITE: / 'This is line:',lv_a. "执行的语句
  lv_a = lv_a + 1.
ENDWHILE.

*2do循环实现输出0-7
lv_a = 0.
DO 8 TIMES. "指定循环的次数
  WRITE: / 'Do this is line:',lv_a.
  lv_a = lv_a + 1.
ENDDO.

*3、嵌套循环
DATA: lv_a1 TYPE i VALUE 0,
      lv_a2 TYPE i VALUE 0.
DO 2 TIMES.
  lv_a1 = lv_a1 + 1.
  WRITE: / 'Outer',lv_a1.
  DO 5 TIMES.
    lv_a2 = lv_a2 + 1.
    WRITE: / 'Inner',lv_a2.
  ENDDO.
ENDDO.

*跳出循环三个条件
DO 15 TIMES.
  IF sy-index = 5.
    CONTINUE."终止当前循环,跳到下次循环
    EXIT."终止所有循环
  ENDIF.
  CHECK sy-index = 4."有条件的执行后面的语句
  WRITE: / 'Hello',sy-index.
ENDDO.

二、条件判断

*1、条件判断
DATA lv_title1 TYPE c LENGTH 20.
lv_title1 = 'abap'.
IF lv_title1 = 'abap'.
  WRITE: / 'true'.
ELSE.
  WRITE: / 'false'.
ENDIF.

*2、复杂的条件判断(方式一)
DATA: lv_c1 TYPE c LENGTH 20 VALUE '2023',
      lv_c2 TYPE c LENGTH 20 VALUE 'ABAP',
      lv_c3 TYPE c LENGTH 20 VALUE '35'.

IF lv_c1 = '2023'.
  IF lv_c2 = 'ABAP'.
    IF lv_c3 = '35'.
      WRITE: / '今天不上班'.
    ENDIF.
  ENDIF.
ENDIF.

*复杂的条件判断(方式二 and or)
IF lv_c1 = '2023' AND lv_c2 = 'ABAP' AND lv_c3 = '35'.
  WRITE: / '今天不上班'.
ENDIF.

*3if elseif else

*4case when
**如果b1 = 36 今天上班
**如果b1 = 35 今天不上班
**如果都不满足,今天肯定不上班
DATA lv_b1 TYPE i VALUE 36.

CASE lv_b1.
  WHEN 36.
    WRITE: / '今天上班'.
  WHEN 35.
    WRITE: / '今天不上班'.
  WHEN OTHERS.
    WRITE: / '今天肯定不上班'.
ENDCASE.

SAP/ABAP(二)_第1张图片

三、异常处理(cx-root)

*异常处理(cx-root)
DATA: lv_i1  TYPE i VALUE 2,
      lv_i2  TYPE i VALUE 0,
      lv_i3  TYPE i,
      lv_msg TYPE string.
TRY.
    lv_i3 = lv_i1 / lv_i2.
    WRITE: / lv_i3.
  CATCH cx_root INTO DATA(lo_exref).
    lv_msg = lo_exref->get_text( ).
    WRITE: / lv_msg.
ENDTRY.

四、结构体,工作区

*结构体,工作区
**方法一 定义一个变量
DATA:
  BEGIN OF ls_student1,
    name TYPE c LENGTH 10,
    age  TYPE i,
  END OF ls_student1.

DATA: ls_student2 LIKE ls_student1.
ls_student2-name = '学生1'.
ls_student2-age = 18.
WRITE: / ls_student2-name,ls_student2-age.

**方法二 定义一个类型
TYPES: BEGIN OF lty_student,
         name TYPE c LENGTH 10,
         age  TYPE i,
       END OF lty_student.

DATA: ls_stu1 TYPE lty_student.
ls_stu1-name = '学生2'.
ls_stu1-age = 19.
WRITE: / ls_stu1-name,ls_stu1-age.

*给结构体赋值三种方式
DATA: ls_stu2 TYPE lty_student,
      ls_stu3 TYPE lty_student,
      ls_stu4 TYPE lty_student.
**=赋值
ls_stu2 = ls_stu1.
**用move to
MOVE ls_stu1 TO ls_stu3.
**MOVE-CORRESPONDING...to(建议使用)
MOVE-CORRESPONDING ls_stu1 TO ls_stu4.


*结构体继承
TYPES: BEGIN OF lty_xxs,
         name TYPE c LENGTH 10,
         age  TYPE i,
       END OF lty_xxs.
DATA: ls_xxs TYPE lty_xxs.

ls_xxs-name = '学生1'.
ls_xxs-age = 6.
WRITE: / '小学生信息:', ls_xxs-name, ls_xxs-age.

TYPES: BEGIN OF lty_dxs,
         zy TYPE string.
    INCLUDE STRUCTURE ls_xxs.
TYPES: END OF lty_dxs.

DATA: ls_dxs TYPE lty_dxs.
ls_dxs-zy = 'ABAP'.
WRITE: / '大学生信息:', ls_dxs-name,ls_dxs-age,ls_dxs-zy.

五、内表

*内表
**结构-》一个学生的信息
**内表-》一群学生的信息

**1、内表的定义
TYPES: BEGIN OF lty_xxs,
         name TYPE c LENGTH 10,
         age  TYPE i,
       END OF lty_xxs.
DATA: ls_xxs TYPE lty_xxs,
      lt_xxs TYPE STANDARD TABLE OF lty_xxs.

ls_xxs-name = '学生1'.
ls_xxs-age = 6.
APPEND ls_xxs TO lt_xxs.

ls_xxs-name = '学生2'.
ls_xxs-age = 6.
APPEND ls_xxs TO lt_xxs.

ls_xxs-name = '学生3'.
ls_xxs-age = 6.
APPEND ls_xxs TO lt_xxs.

DO 3 TIMES.
  ls_xxs-name = '循环相互来的'.
  ls_xxs-age = sy-index.
  APPEND ls_xxs TO lt_xxs. "append:追加
ENDDO.

ls_xxs-name = '插班生'.
ls_xxs-age = 7.
INSERT ls_xxs INTO lt_xxs INDEX 3. "在内表中间插入一条数据,在3前面插入

cl_demo_output=>display( lt_xxs )."打印输出

*读取内表数据
**根据索引读取数据
READ TABLE lt_xxs INTO ls_xxs INDEX 1.
WRITE: / '根据索引读取index 1 的学生',ls_xxs-name,ls_xxs-age.

**根据一些关键字读取数据
READ TABLE lt_xxs INTO ls_xxs WITH KEY name = '插班生'.
WRITE: / '根据关键字读取学生信息',ls_xxs-name,ls_xxs-age.

**二分查找法
**排序
SORT lt_xxs BY name ASCENDING.
READ TABLE lt_xxs INTO ls_xxs WITH KEY name = '插班生' BINARY SEARCH.
WRITE: / '根据二分查找读取学生信息',ls_xxs-name,ls_xxs-age.

你可能感兴趣的:(SAP,ABAP,ABAP,SAP)