COBOL - 循环语句
-
简述
有些任务需要一遍又一遍地完成,比如读取文件的每条记录直到结束。COBOL 中使用的循环语句是 -- Perform Thru
- Perform Until
- Perform Times
- Perform Varying
-
Perform Thru
Perform Thru 用于通过给出序列中的第一个和最后一个段落名称来执行一系列段落。执行完最后一段后,控制权返回。在线执行
PERFORM 中的语句将一直执行,直到到达 END-PERFORM。句法
以下是内联执行的语法 -PERFORM DISPLAY 'HELLO WORLD' END-PERFORM.
脱机演示
在这里,一个语句在一个段落中执行,然后控制转移到其他段落或部分。句法
以下是 Out-of-line 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
-
Perform Until
在“Perform Until”中,执行一个段落,直到给定条件变为真。'With test before' 是默认条件,它表示在执行段落中的语句之前检查条件。句法
以下是Perform Until的语法 -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 Times
在“Perform Times”中,段落将执行指定的次数。句法
以下是Perform Times的语法 -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 Varying
在Perform Varying时,将执行一个段落,直到直到短语中的条件变为真。句法
以下是Perform Varying的语法 -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 语句如下 -无条件转到
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