GNUCobol语言基本语法 - Mon, Jul 4, 2022
GNUCobol语言基本语法
1. hello world
IDENTIFICATION DIVISION.
PROGRAM-ID. hello.
PROCEDURE DIVISION.
DISPLAY 'hello world'.
STOP RUN.
output
hello world
2. 加减乘除
IDENTIFICATION DIVISION.
PROGRAM-ID. SAMPLE.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 NUM-Count PIC 9(03) VALUES 10.
PROCEDURE DIVISION.
BEGIN.
*< NUM-Count += NUM-Count
ADD NUM-Count TO NUM-Count.
DISPLAY "ADD: " NUM-Count END-DISPLAY.
*< NUM-Count -= NUM-Count
SUBTRACT NUM-Count FROM NUM-Count.
DISPLAY "SUBTRACT: " NUM-Count END-DISPLAY.
MOVE 10 TO NUM-Count.
*< NUM-Count *= NUM-Count
MULTIPLY NUM-Count BY NUM-Count.
DISPLAY "MULTIPLY: " NUM-Count END-DISPLAY.
*< NUM-Count /= NUM-Count
DIVIDE NUM-Count INTO NUM-Count.
DISPLAY "DIVIDE: " NUM-Count END-DISPLAY.
STOP RUN.
END PROGRAM SAMPLE.
output
ADD: 020
SUBTRACT: 000
MULTIPLY: 100
DIVIDE: 001
3. 计算语句
IDENTIFICATION DIVISION.
PROGRAM-ID. hello.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 NUM-Count PIC 9(03) VALUES 10.
PROCEDURE DIVISION.
COMPUTE NUM-Count =
(NUM-Count * NUM-Count) -
(NUM-Count / NUM-Count) +
NUM-Count.
DISPLAY "COMPUTE: " NUM-Count END-DISPLAY.
STOP RUN.
output
COMPUTE: 109
4. 变量定义
IDENTIFICATION DIVISION.
PROGRAM-ID. hello.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUM1 PIC S9(3)V9(2).
01 WS-NUM2 PIC PPP999.
01 WS-NUM3 PIC S9(3)V9(2) VALUE -123.45.
01 WS-NAME PIC A(6) VALUE 'ABCDEF'.
01 WS-ID PIC X(5) VALUE 'A121$'.
PROCEDURE DIVISION.
DISPLAY "WS-NUM1 : "WS-NUM1.
DISPLAY "WS-NUM2 : "WS-NUM2.
DISPLAY "WS-NUM3 : "WS-NUM3.
DISPLAY "WS-NAME : "WS-NAME.
DISPLAY "WS-ID : "WS-ID.
STOP RUN.
output
WS-NUM1 : +000.00
WS-NUM2 : .000000
WS-NUM3 : -123.45
WS-NAME : ABCDEF
WS-ID : A121$
5. 变量赋值
IDENTIFICATION DIVISION.
PROGRAM-ID. hello.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUM1 PIC S9(3)V9(2).
01 WS-NUM2 PIC PPP999.
01 WS-NUM3 PIC S9(3)V9(2) VALUE -123.45.
01 WS-NAME PIC A(6) VALUE 'ABCDEF'.
01 WS-ID PIC X(5) VALUE 'A121$'.
01 WS-GROUP1.
05 WS-G1-1 PIC S9(3)V9(2).
05 WS-G1-2 PIC S9(3)V9(2).
01 WS-GROUP2 OCCURS 3 TIMES INDEXED BY I.
05 WS-G2-1 PIC S9(3)V9(2) VALUE +45.32.
05 WS-G2-2 PIC S9(3)V9(2).
PROCEDURE DIVISION.
MOVE 123.45 TO WS-NUM1.
DISPLAY "WS-NUM1 : "WS-NUM1.
MOVE -123.45 TO WS-NUM1.
DISPLAY "WS-NUM1 : "WS-NUM1.
DISPLAY "WS-NUM2 : "WS-NUM2.
DISPLAY "WS-NUM3 : "WS-NUM3.
DISPLAY "WS-NAME : "WS-NAME.
DISPLAY "WS-ID : "WS-ID.
*< 0000000000
DISPLAY "WS-GROUP1 : "WS-GROUP1.
MOVE 123.45 TO WS-G1-1.
MOVE -123.45 TO WS-G1-2.
DISPLAY "WS-GROUP1 : "WS-GROUP1.
DISPLAY "WS-1 : "WS-G1-1.
DISPLAY "WS-2 : "WS-G1-2.
PERFORM VARYING I FROM 1 BY 1 UNTIL I > 3
DISPLAY "WS-GROUP2["I"] : "WS-GROUP2(I)
END-PERFORM.
PERFORM VARYING I FROM 1 BY 1 UNTIL I > 3
MOVE -23.45 TO WS-G2-2(I)
END-PERFORM.
PERFORM VARYING I FROM 1 BY 1 UNTIL I > 3
DISPLAY "WS-GROUP2["I"] : "WS-GROUP2(I)
END-PERFORM.
STOP RUN.
output
WS-NUM1 : +123.45
WS-NUM1 : -123.45
WS-NUM2 : .000000
WS-NUM3 : -123.45
WS-NAME : ABCDEF
WS-ID : A121$
WS-GROUP1 : 0000000000
WS-GROUP1 : 123451234u
WS-1 : +123.45
WS-2 : -123.45
WS-GROUP2[+000000001] : 0453200000
WS-GROUP2[+000000002] : 0453200000
WS-GROUP2[+000000003] : 0453200000
WS-GROUP2[+000000001] : 045320234u
WS-GROUP2[+000000002] : 045320234u
WS-GROUP2[+000000003] : 045320234u