COBOL 循環(huán)語(yǔ)句

2018-10-14 18:31 更新

有一些任務(wù)需要一遍一遍地完成,就像讀一個(gè)文件的每一個(gè)記錄,直到它結(jié)束。COBOL中使用的循環(huán)語(yǔ)句是:

  • Perform Thru
  • Perform Until
  • Perform Times
  • Perform Varying

Perform Thru

Perform Thru用于執(zhí)行一系列段落,通過(guò)給序列中的第一和最后一段命名。執(zhí)行最后一段后,控制返回。

內(nèi)聯(lián)Perform

PERFORM內(nèi)的語(yǔ)句將被執(zhí)行,直到到達(dá)END-PERFORM。

語(yǔ)法

下面是在內(nèi)聯(lián)perform的語(yǔ)法:

PERFORM 
   DISPLAY 'HELLO WORLD'
END-PERFORM.

外聯(lián)Perform

在這里,一個(gè)語(yǔ)句在段落中執(zhí)行,然后將控制轉(zhuǎn)移到其他段落或節(jié)。

語(yǔ)法

以下是外聯(lián)Perform的語(yǔ)法:

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執(zhí)行上述COBOL程序。

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

當(dāng)你編譯和執(zhí)行上面的程序,會(huì)產(chǎn)生以下結(jié)果:

IN A-PARA
IN C-PARA
IN D-PARA
IN E-PARA
IN B-PARA

Perform Until

Perform Until中,一個(gè)段落執(zhí)行直到給定的條件為true?!癢ITH TEST BEFORE”是默認(rèn)條件,表示條件在執(zhí)行段落中的語(yǔ)句之前檢查。

語(yǔ)法

以下是perform until的語(yǔ)法:

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執(zhí)行上述COBOL程序。

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

當(dāng)你編譯和執(zhí)行上面的程序,會(huì)產(chǎn)生以下結(jié)果:

WS-CNT : 0
WS-CNT : 1
WS-CNT : 2
WS-CNT : 3

Perform Times

在“perform times”中,一個(gè)段落將被執(zhí)行指定的次數(shù)。

語(yǔ)法

以下是perform times的語(yǔ)法:

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執(zhí)行上述COBOL程序:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

當(dāng)你編譯和執(zhí)行上面的程序,會(huì)產(chǎn)生以下結(jié)果:

IN B-PARA
IN B-PARA
IN B-PARA

Perform Varying

在perform varying中,一個(gè)段落將被執(zhí)行直到UNTIL短語(yǔ)中的條件為true。

語(yǔ)法

以下是perform varying的語(yǔ)法:

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執(zhí)行上述COBOL程序:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

當(dāng)你編譯和執(zhí)行上面的程序,會(huì)產(chǎn)生以下結(jié)果:

IN B-PARA 1
IN B-PARA 2
IN B-PARA 3
IN B-PARA 4

GO TO語(yǔ)句

GO TO語(yǔ)句用于更改程序中的執(zhí)行流。在GO TO語(yǔ)句中,傳輸方向僅限為向前。它用于退出一個(gè)段落。常用的GO TO語(yǔ)句的不同類型如下:

無(wú)條件GO TO

GO TO para-name.

有條件GO TO

GO TO para-1 para-2 para-3 DEPENDING ON x.

如果x是等于1,那么控制將被傳輸?shù)降谝欢?。如果x是等于2,則控制將傳輸?shù)降诙?,以此類推?

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執(zhí)行上述COBOL程序:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

當(dāng)你編譯和執(zhí)行上面的程序,會(huì)產(chǎn)生以下結(jié)果:

IN A-PARA
IN B-PARA 
IN D-PARA 
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)