*&---------------------------------------------------------------------**&ReportZDEMO_LIMING01*&---------------------------------------------------------------------**&作者:黎明
*&---------------------------------------------------------------------*REPORT zdemo_liming01.*1、while循环实现输出0-7DATA lv_a TYPE i.
lv_a =0.WHILE lv_a <8. "条件
WRITE:/ 'This is line:',lv_a. "执行的语句
lv_a = lv_a +1.ENDWHILE.*2、do循环实现输出0-7
lv_a =0.DO8TIMES. "指定循环的次数
WRITE:/ 'Dothis is line:',lv_a.
lv_a = lv_a +1.ENDDO.*3、嵌套循环
DATA: lv_a1 TYPE i VALUE0,
lv_a2 TYPE i VALUE0.DO2TIMES.
lv_a1 = lv_a1 +1.WRITE:/'Outer',lv_a1.DO5TIMES.
lv_a2 = lv_a2 +1.WRITE:/'Inner',lv_a2.ENDDO.ENDDO.*跳出循环三个条件
DO15TIMES.IF sy-index =5.CONTINUE."终止当前循环,跳到下次循环
EXIT."终止所有循环
ENDIF.CHECK sy-index =4."有条件的执行后面的语句
WRITE:/'Hello',sy-index.ENDDO.
二、条件判断
*1、条件判断
DATA lv_title1 TYPE c LENGTH20.
lv_title1 ='abap'.IF lv_title1 ='abap'.WRITE:/'true'.ELSE.WRITE:/'false'.ENDIF.*2、复杂的条件判断(方式一)
DATA: lv_c1 TYPE c LENGTH20VALUE'2023',
lv_c2 TYPE c LENGTH20VALUE'ABAP',
lv_c3 TYPE c LENGTH20VALUE'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.*3、if elseif else*4、case when
**如果b1 =36 今天上班
**如果b1 =35 今天不上班
**如果都不满足,今天肯定不上班
DATA lv_b1 TYPE i VALUE36.CASE lv_b1.WHEN36.WRITE:/'今天上班'.WHEN35.WRITE:/'今天不上班'.WHENOTHERS.WRITE:/ '今天肯定不上班'.ENDCASE.
三、异常处理(cx-root)
*异常处理(cx-root)
DATA: lv_i1 TYPE i VALUE2,
lv_i2 TYPE i VALUE0,
lv_i3 TYPE i,
lv_msg TYPE string.TRY.
lv_i3 = lv_i1 / lv_i2.WRITE:/ lv_i3.CATCH cx_root INTODATA(lo_exref).
lv_msg = lo_exref->get_text().WRITE:/ lv_msg.ENDTRY.
四、结构体,工作区
*结构体,工作区
**方法一 定义一个变量
DATA:BEGINOF ls_student1,
name TYPE c LENGTH10,
age TYPE i,ENDOF ls_student1.DATA: ls_student2 LIKE ls_student1.
ls_student2-name ='学生1'.
ls_student2-age =18.WRITE:/ ls_student2-name,ls_student2-age.**方法二 定义一个类型
TYPES:BEGINOF lty_student,
name TYPE c LENGTH10,
age TYPE i,ENDOF 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 toMOVE ls_stu1 TO ls_stu3.**用MOVE-CORRESPONDING...to(建议使用)MOVE-CORRESPONDING ls_stu1 TO ls_stu4.*结构体继承
TYPES:BEGINOF lty_xxs,
name TYPE c LENGTH10,
age TYPE i,ENDOF lty_xxs.DATA: ls_xxs TYPE lty_xxs.
ls_xxs-name ='学生1'.
ls_xxs-age =6.WRITE:/'小学生信息:', ls_xxs-name, ls_xxs-age.TYPES:BEGINOF lty_dxs,
zy TYPE string.INCLUDESTRUCTURE ls_xxs.TYPES:ENDOF lty_dxs.DATA: ls_dxs TYPE lty_dxs.
ls_dxs-zy ='ABAP'.WRITE:/'大学生信息:', ls_dxs-name,ls_dxs-age,ls_dxs-zy.
五、内表
*内表
**结构-》一个学生的信息
**内表-》一群学生的信息
**1、内表的定义
TYPES:BEGINOF lty_xxs,
name TYPE c LENGTH10,
age TYPE i,ENDOF lty_xxs.DATA: ls_xxs TYPE lty_xxs,
lt_xxs TYPESTANDARDTABLEOF 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.DO3TIMES.
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 INDEX3. "在内表中间插入一条数据,在3前面插入
cl_demo_output=>display( lt_xxs )."打印输出
*读取内表数据
**根据索引读取数据
READTABLE lt_xxs INTO ls_xxs INDEX1.WRITE:/ '根据索引读取index 1 的学生',ls_xxs-name,ls_xxs-age.**根据一些关键字读取数据
READTABLE lt_xxs INTO ls_xxs WITHKEY name ='插班生'.WRITE:/ '根据关键字读取学生信息',ls_xxs-name,ls_xxs-age.**二分查找法
**排序
SORT lt_xxs BY name ASCENDING.READTABLE lt_xxs INTO ls_xxs WITHKEY name ='插班生'BINARYSEARCH.WRITE:/ '根据二分查找读取学生信息',ls_xxs-name,ls_xxs-age.