📅  最后修改于: 2020-11-22 16:50:50             🧑  作者: Mango
有些任务需要一遍又一遍地完成,例如读取文件的每个记录直到结束。 COBOL中使用的循环语句为-
Perform Thru用于通过提供序列中的第一个和最后一个段落名称来执行一系列段落。执行最后一段后,控件将返回。
PERFORM中的语句将一直执行到到达END-PERFORM为止。
以下是在线执行的语法-
PERFORM
DISPLAY 'HELLO WORLD'
END-PERFORM.
在这里,在一个段落中执行一条语句,然后将控件转移到另一段落或节中。
以下是离线执行的语法-
PERFORM PARAGRAPH1 THRU PARAGRAPH2
例
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
PROCEDURE DIVISION.
A-PARA.
PERFORM DISPLAY 'IN A-PARA'
END-PERFORM.
PERFORM C-PARA THRU E-PARA.
B-PARA.
DISPLAY 'IN B-PARA'.
STOP RUN.
C-PARA.
DISPLAY 'IN C-PARA'.
D-PARA.
DISPLAY 'IN D-PARA'.
E-PARA.
DISPLAY 'IN E-PARA'.
JCL执行上述COBOL程序。
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
//STEP1 EXEC PGM = HELLO
当您编译并执行上述程序时,它将产生以下结果-
IN A-PARA
IN C-PARA
IN D-PARA
IN E-PARA
IN B-PARA
在“执行直到”中,执行一段直到给定条件变为真为止。默认情况下为“ with test before”,它表示在执行段落中的语句之前检查了条件。
以下是Perform直到的语法-
PERFORM A-PARA UNTIL COUNT=5
PERFORM A-PARA WITH TEST BEFORE UNTIL COUNT=5
PERFORM A-PARA WITH TEST AFTER UNTIL COUNT=5
例
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-CNT PIC 9(1) VALUE 0.
PROCEDURE DIVISION.
A-PARA.
PERFORM B-PARA WITH TEST AFTER UNTIL WS-CNT>3.
STOP RUN.
B-PARA.
DISPLAY 'WS-CNT : 'WS-CNT.
ADD 1 TO WS-CNT.
JCL执行上述COBOL程序-
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
//STEP1 EXEC PGM = HELLO
当您编译并执行上述程序时,它将产生以下结果-
WS-CNT : 0
WS-CNT : 1
WS-CNT : 2
WS-CNT : 3
在“执行次数”中,一段将执行指定的次数。
以下是执行时间的语法-
PERFORM A-PARA 5 TIMES.
例
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
PROCEDURE DIVISION.
A-PARA.
PERFORM B-PARA 3 TIMES.
STOP RUN.
B-PARA.
DISPLAY 'IN B-PARA'.
JCL执行上述COBOL程序-
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
//STEP1 EXEC PGM = HELLO
当您编译并执行上述程序时,它将产生以下结果-
IN B-PARA
IN B-PARA
IN B-PARA
在执行变化时,将执行一个段落,直到直到短语中的条件变为真为止。
以下是执行变化的语法-
PERFORM A-PARA VARYING A FROM 1 BY 1 UNTIL A = 5.
例
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-A PIC 9 VALUE 0.
PROCEDURE DIVISION.
A-PARA.
PERFORM B-PARA VARYING WS-A FROM 1 BY 1 UNTIL WS-A=5
STOP RUN.
B-PARA.
DISPLAY 'IN B-PARA ' WS-A.
JCL执行上述COBOL程序-
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
//STEP1 EXEC PGM = HELLO
当您编译并执行上述程序时,它将产生以下结果-
IN B-PARA 1
IN B-PARA 2
IN B-PARA 3
IN B-PARA 4
GO TO语句用于更改程序中的执行流程。在GO TO语句中,传输仅向前进行。用于退出段落。所使用的GO TO语句的不同类型如下-
GO TO para-name.
GO TO para-1 para-2 para-3 DEPENDING ON x.
如果’x’等于1,则控件将转移到第一段;如果’x’等于2,则控件将转移到第二段,依此类推。
例
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-A PIC 9 VALUE 2.
PROCEDURE DIVISION.
A-PARA.
DISPLAY 'IN A-PARA'
GO TO B-PARA.
B-PARA.
DISPLAY 'IN B-PARA '.
GO TO C-PARA D-PARA DEPENDING ON WS-A.
C-PARA.
DISPLAY 'IN C-PARA '.
D-PARA.
DISPLAY 'IN D-PARA '.
STOP RUN.
JCL执行上述COBOL程序:
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
//STEP1 EXEC PGM = HELLO
当您编译并执行上述程序时,它将产生以下结果:
IN A-PARA
IN B-PARA
IN D-PARA