COBOL 基本動(dòng)詞

2018-10-14 13:55 更新

COBOL動(dòng)詞用于過(guò)程部中的數(shù)據(jù)處理。每個(gè)語(yǔ)句總是以COBOL動(dòng)詞開(kāi)頭。有幾種具有不同動(dòng)作類(lèi)型的COBOL動(dòng)詞。

Input(輸入)/Output(輸出)動(dòng)詞

輸入/輸出動(dòng)詞用于從用戶(hù)獲取數(shù)據(jù),并顯示COBOL程序的輸出。下面兩個(gè)動(dòng)詞用于這一過(guò)程:

Accept動(dòng)詞

Accept動(dòng)詞用于從操作系統(tǒng)或直接從用戶(hù)獲取諸如日期,時(shí)間和天等數(shù)據(jù)。如果程序從用戶(hù)接收數(shù)據(jù),那么就需要通過(guò)JCL傳遞。在從操作系統(tǒng)獲取數(shù)據(jù)時(shí),F(xiàn)ROM選項(xiàng)則如下面例子所示:

ACCEPT WS-STUDENT-NAME.
ACCEPT WS-DATE FROM SYSTEM-DATE.

Display動(dòng)詞

Display動(dòng)詞用于顯示COBOL程序的輸出。

DISPLAY WS-STUDENT-NAME.
DISPLAY "System date is : " WS-DATE.

COBOL程序

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 WS-STUDENT-NAME PIC X(25).
   01 WS-DATE PIC X(10).

PROCEDURE DIVISION.
   ACCEPT WS-STUDENT_NAME.
   ACCEPT WS-DATE FROM DATE.
   DISPLAY "Name :  " WS-STUDENT_NAME.
   DISPLAY "Date : " WS-DATE.

STOP RUN.

JCL執(zhí)行上述COBOL程序:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
//INPUT DD DSN=PROGRAM.DIRECTORY,DISP=SHR
//SYSIN DD *
TutorialsPoint
/*

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

Name : TutorialsPoint
Date : 2014-08-30

Initialize(初始化)動(dòng)詞

Initialize動(dòng)詞被用來(lái)初始化組項(xiàng)或基本項(xiàng)。數(shù)據(jù)名為RENAME的子句不能被初始化。數(shù)字?jǐn)?shù)據(jù)項(xiàng)由ZEROES代替。字母數(shù)字或字母數(shù)據(jù)項(xiàng)由SPACES代替。如果我們包含REPLACING術(shù)語(yǔ),則數(shù)據(jù)項(xiàng)可被初始化為給定替換值,如以下示例所示:

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 WS-NAME PIC A(30) VALUE 'ABCDEF'.
   01 WS-ID PIC 9(5).
   01 WS-ADDRESS. 
   05 WS-HOUSE-NUMBER PIC 9(3).
   05 WS-COUNTRY PIC X(15).
   05 WS-PINCODE PIC 9(6) VALUE 123456.

PROCEDURE DIVISION.
   A000-FIRST-PARA.
   INITIALIZE WS-NAME, WS-ADDRESS.
   INITIALIZE WS-ID REPLACING NUMERIC DATA BY 12345.
   DISPLAY "My name is   : "WS-NAME.
   DISPLAY "My ID is     : "WS-ID.
   DISPLAY "Address      : "WS-ADDRESS.
   DISPLAY "House Number : "WS-HOUSE-NUMBER.
   DISPLAY "Country      : "WS-COUNTRY.
   DISPLAY "Pincode      : "WS-PINCODE.

STOP RUN.

JCL執(zhí)行上述COBOL程序:

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

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

My name is   :                               
My ID is     : 12345
Address      : 000               000000
House Number : 000
Country      :                
Pincode      : 000000

Move(移動(dòng))動(dòng)詞

移動(dòng)動(dòng)詞是用來(lái)將數(shù)據(jù)從源數(shù)據(jù)復(fù)制到目標(biāo)數(shù)據(jù)的。它可以用于基本和組數(shù)據(jù)項(xiàng)。對(duì)于組數(shù)據(jù)項(xiàng),使用MOVE CORRESPONDING/CORR。在嘗試其它選項(xiàng)的時(shí)候,MOVE CORR不工作;但是在大型機(jī)服務(wù)器上工作。

對(duì)于從字符串移動(dòng)數(shù)據(jù),則使用MOVE(x:l),其中x是起始位置,l是長(zhǎng)度。如果目標(biāo)數(shù)據(jù)項(xiàng)PIC子句少于源數(shù)據(jù)項(xiàng)PIC子句,則數(shù)據(jù)將被截?cái)唷H绻繕?biāo)數(shù)據(jù)項(xiàng)PIC子句多于源數(shù)據(jù)項(xiàng)PIC子句,則將添加額外的ZEROS或SAPCES字節(jié)。從下面的例子可以清楚的看到:

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 WS-NUM1 PIC 9(9).
   01 WS-NUM2 PIC 9(9).
   01 WS-NUM3 PIC 9(5).
   01 WS-NUM4 PIC 9(6).
   01 WS-ADDRESS. 
   05 WS-HOUSE-NUMBER PIC 9(3).
   05 WS-COUNTRY PIC X(5).
   05 WS-PINCODE PIC 9(6).
   01 WS-ADDRESS1. 
   05 WS-HOUSE-NUMBER1 PIC 9(3).
   05 WS-COUNTRY1 PIC X(5).
   05 WS-PINCODE1 PIC 9(6).

PROCEDURE DIVISION.
   A000-FIRST-PARA.
   MOVE 123456789 TO WS-NUM1.
   MOVE WS-NUM1 TO WS-NUM2 WS-NUM3.
   MOVE WS-NUM1(3:6) TO WS-NUM4.
   MOVE 123 TO WS-HOUSE-NUMBER.
   MOVE 'INDIA' TO WS-COUNTRY.
   MOVE 112233 TO WS-PINCODE.
   MOVE WS-ADDRESS TO WS-ADDRESS1.

   DISPLAY "WS-NUM1     : " WS-NUM1
   DISPLAY "WS-NUM2     : " WS-NUM2
   DISPLAY "WS-NUM3     : " WS-NUM3
   DISPLAY "WS-NUM4     : " WS-NUM4
   DISPLAY "WS-ADDRESS  : " WS-ADDRESS
   DISPLAY "WS-ADDRESS1 : " WS-ADDRESS1

STOP RUN.

JCL執(zhí)行上述COBOL程序。

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

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

WS-NUM1     : 123456789
WS-NUM2     : 123456789
WS-NUM3     : 56789
WS-NUM4     : 345678
WS-ADDRESS  : 123INDIA112233
WS-ADDRESS1 : 123INDIA112233

合法移動(dòng)

下表給出了有關(guān)合法移動(dòng)的信息:

 字母字母數(shù)字數(shù)字
字母可能可能不可能
字母數(shù)字可能可能可能
數(shù)字不可能可能可能

Add(加法)動(dòng)詞

加法動(dòng)詞用于兩個(gè)或多個(gè)數(shù)字相加,并將結(jié)果存儲(chǔ)在目標(biāo)運(yùn)算對(duì)象中。

語(yǔ)法

以下為兩個(gè)或多個(gè)數(shù)字相加的語(yǔ)法:

ADD A B TO C D

ADD A B C TO D GIVING E

ADD CORR WS-GROUP1 TO WS-GROUP2

在語(yǔ)法句1中,A,B,C相加,結(jié)果存儲(chǔ)在C(C=A+B+C)。A,B,D相加,結(jié)果存儲(chǔ)在D(D=A+B+D)。

在語(yǔ)法句2中,A,B,C,D相加,結(jié)果存儲(chǔ)在E(E=A+B+C+D)。

在語(yǔ)法句3中,WS-GROUP1和WS-GROUP2中的子組項(xiàng)相加,結(jié)果存儲(chǔ)在WS-GROUP2中。

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 WS-NUM1 PIC 9(9) VALUE 10 .
   01 WS-NUM2 PIC 9(9) VALUE 10.
   01 WS-NUM3 PIC 9(9) VALUE 10.
   01 WS-NUM4 PIC 9(9) VALUE 10.
   01 WS-NUMA PIC 9(9) VALUE 10.
   01 WS-NUMB PIC 9(9) VALUE 10.
   01 WS-NUMC PIC 9(9) VALUE 10.
   01 WS-NUMD PIC 9(9) VALUE 10.
   01 WS-NUME PIC 9(9) VALUE 10.

PROCEDURE DIVISION.
   ADD WS-NUM1 WS-NUM2 TO WS-NUM3 WS-NUM4.
   ADD WS-NUMA WS-NUMB WS-NUMC TO WS-NUMD GIVING WS-NUME.
   DISPLAY "WS-NUM1     : " WS-NUM1
   DISPLAY "WS-NUM2     : " WS-NUM2
   DISPLAY "WS-NUM3     : " WS-NUM3
   DISPLAY "WS-NUM4     : " WS-NUM4
   DISPLAY "WS-NUMA     : " WS-NUMA
   DISPLAY "WS-NUMB     : " WS-NUMB
   DISPLAY "WS-NUMC     : " WS-NUMC
   DISPLAY "WS-NUMD     : " WS-NUMD
   DISPLAY "WS-NUME     : " WS-NUME

STOP RUN.

JCL執(zhí)行上述COBOL程序:

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

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

WS-NUM1     : 000000010
WS-NUM2     : 000000010
WS-NUM3     : 000000030
WS-NUM4     : 000000030
WS-NUMA     : 000000010
WS-NUMB     : 000000010
WS-NUMC     : 000000010
WS-NUMD     : 000000010
WS-NUME     : 000000040

Subtract(減法)動(dòng)詞

減法動(dòng)詞用于減法運(yùn)算。

語(yǔ)法

下面給出的是減法運(yùn)算的語(yǔ)法:

SUBTRACT A B FROM C D

SUBTRACT A B C FROM D GIVING E

SUBTRACT CORR WS-GROUP1 TO WS-GROUP2

在語(yǔ)法句1中,A和B相加并從C中減去,結(jié)果存儲(chǔ)在C(C=C-(A+B))。A和B相加并從D中減去,結(jié)果存儲(chǔ)在D(D=D-(A+B))。

在語(yǔ)法句2中,A,B,C相加并從D中減去,結(jié)果存儲(chǔ)在E(E=D-(A+B+C))。

在語(yǔ)法句3中,WS-GROUP1和WS-GROUP2中的子組項(xiàng)相減,結(jié)果存儲(chǔ)在WS-GROUP2中。

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 WS-NUM1 PIC 9(9) VALUE 10 .
   01 WS-NUM2 PIC 9(9) VALUE 10.
   01 WS-NUM3 PIC 9(9) VALUE 100.
   01 WS-NUM4 PIC 9(9) VALUE 100.
   01 WS-NUMA PIC 9(9) VALUE 10.
   01 WS-NUMB PIC 9(9) VALUE 10.
   01 WS-NUMC PIC 9(9) VALUE 10.
   01 WS-NUMD PIC 9(9) VALUE 100.
   01 WS-NUME PIC 9(9) VALUE 10.

PROCEDURE DIVISION.
   SUBTRACT WS-NUM1 WS-NUM2 FROM WS-NUM3 WS-NUM4.
   SUBTRACT WS-NUMA WS-NUMB WS-NUMC FROM WS-NUMD GIVING WS-NUME.

   DISPLAY "WS-NUM1     : " WS-NUM1
   DISPLAY "WS-NUM2     : " WS-NUM2
   DISPLAY "WS-NUM3     : " WS-NUM3
   DISPLAY "WS-NUM4     : " WS-NUM4
   DISPLAY "WS-NUMA     : " WS-NUMA
   DISPLAY "WS-NUMB     : " WS-NUMB
   DISPLAY "WS-NUMC     : " WS-NUMC
   DISPLAY "WS-NUMD     : " WS-NUMD
   DISPLAY "WS-NUME     : " WS-NUME

STOP RUN.

JCL執(zhí)行上述COBOL程序:

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

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

WS-NUM1     : 000000010
WS-NUM2     : 000000010
WS-NUM3     : 000000080
WS-NUM4     : 000000080
WS-NUMA     : 000000010
WS-NUMB     : 000000010
WS-NUMC     : 000000010
WS-NUMD     : 000000100
WS-NUME     : 000000070

Multiply(乘法)動(dòng)詞

乘法動(dòng)詞用于乘法運(yùn)算。

語(yǔ)法

下面是兩個(gè)或多個(gè)數(shù)字相乘的語(yǔ)法:

MULTIPLY A BY B C

MULTIPLY A BY B GIVING E

在語(yǔ)法句1中,A和B相乘,結(jié)果存儲(chǔ)在B(B=A*B)。A和C相乘,結(jié)果存儲(chǔ)在C(C=A*C)。

在語(yǔ)法句2中,A和B相乘,結(jié)果存儲(chǔ)在E(E=A*B)。

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 WS-NUM1 PIC 9(9) VALUE 10 .
   01 WS-NUM2 PIC 9(9) VALUE 10.
   01 WS-NUM3 PIC 9(9) VALUE 10.
   01 WS-NUMA PIC 9(9) VALUE 10.
   01 WS-NUMB PIC 9(9) VALUE 10.
   01 WS-NUMC PIC 9(9) VALUE 10.

PROCEDURE DIVISION.
   MULTIPLY WS-NUM1 BY WS-NUM2 WS-NUM3.
   MULTIPLY WS-NUMA BY WS-NUMB GIVING WS-NUMC.
   
   DISPLAY "WS-NUM1     : " WS-NUM1
   DISPLAY "WS-NUM2     : " WS-NUM2
   DISPLAY "WS-NUM3     : " WS-NUM3
   DISPLAY "WS-NUMA     : " WS-NUMA
   DISPLAY "WS-NUMB     : " WS-NUMB
   DISPLAY "WS-NUMC     : " WS-NUMC
   
STOP RUN.

JCL執(zhí)行上述COBOL程序:

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

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

WS-NUM1     : 000000010
WS-NUM2     : 000000100
WS-NUM3     : 000000100
WS-NUMA     : 000000010
WS-NUMB     : 000000010
WS-NUMC     : 000000100

Divide(除法)動(dòng)詞

除法動(dòng)詞用于除法運(yùn)算。

語(yǔ)法

下面是除法運(yùn)算的語(yǔ)法:

DIVIDE A INTO B

DIVIDE A BY B GIVING C REMAINDER R

在語(yǔ)法句1中,B除以A,結(jié)果存儲(chǔ)在B(B=B/A)。

在語(yǔ)法句2中,A除以B,結(jié)果存儲(chǔ)在C(C=A/B),余數(shù)存儲(chǔ)在R。

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 WS-NUM1 PIC 9(9) VALUE 5.
   01 WS-NUM2 PIC 9(9) VALUE 250.
   01 WS-NUMA PIC 9(9) VALUE 100.
   01 WS-NUMB PIC 9(9) VALUE 15.
   01 WS-NUMC PIC 9(9).
   01 WS-REM PIC 9(9). 

PROCEDURE DIVISION.
   DIVIDE WS-NUM1 INTO WS-NUM2.
   DIVIDE WS-NUMA BY WS-NUMB GIVING WS-NUMC REMAINDER WS-REM.
   DISPLAY "WS-NUM1     : " WS-NUM1
   DISPLAY "WS-NUM2     : " WS-NUM2
   DISPLAY "WS-NUMA     : " WS-NUMA
   DISPLAY "WS-NUMB     : " WS-NUMB
   DISPLAY "WS-NUMC     : " WS-NUMC
   DISPLAY "WS-REM      : " WS-REM
   
STOP RUN.

JCL執(zhí)行上述COBOL程序:

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

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

WS-NUM1     : 000000005
WS-NUM2     : 000000050
WS-NUMA     : 000000100
WS-NUMB     : 000000015
WS-NUMC     : 000000006
WS-REM      : 000000010

Compute(計(jì)算)語(yǔ)句

計(jì)算語(yǔ)句用于在COBOL中寫(xiě)入算術(shù)表達(dá)式。這是替代加,減,乘和除法。

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 WS-NUM1 PIC 9(9) VALUE 10 .
   01 WS-NUM2 PIC 9(9) VALUE 10.
   01 WS-NUM3 PIC 9(9) VALUE 10.
   01 WS-NUMA PIC 9(9) VALUE 50.
   01 WS-NUMB PIC 9(9) VALUE 10.
   01 WS-NUMC PIC 9(9).

PROCEDURE DIVISION.
   COMPUTE WS-NUMC= (WS-NUM1 * WS-NUM2) - (WS-NUMA / WS-NUMB) + WS-NUM3.
   DISPLAY "WS-NUM1     : " WS-NUM1
   DISPLAY "WS-NUM2     : " WS-NUM2
   DISPLAY "WS-NUM3     : " WS-NUM3
   DISPLAY "WS-NUMA     : " WS-NUMA
   DISPLAY "WS-NUMB     : " WS-NUMB
   DISPLAY "WS-NUMC     : " WS-NUMC

STOP RUN.

JCL執(zhí)行上述COBOL程序。

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

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

WS-NUM1     : 000000010
WS-NUM2     : 000000010
WS-NUM3     : 000000010
WS-NUMA     : 000000050
WS-NUMB     : 000000010
WS-NUMC     : 000000105
以上內(nèi)容是否對(duì)您有幫助:
在線(xiàn)筆記
App下載
App下載

掃描二維碼

下載編程獅App

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

編程獅公眾號(hào)