---
# System prepended metadata

title: COBOL

---

# COBOL
### 線上編譯器
```
https://www.tutorialspoint.com/compile_cobol_online.php
```
### Picture Clause
```
9 數字
S9 有正負號的數字
V9 小數點
A 字母
X 字母數字都可以放
P 小數點0


9(2) 數字.長度2
S9(2) 有正負號的數字,長度2
V9(2) 小數點,小數點2位.
A(2) 字母.長度2
X(2) 字母數字都可以放.長度2
PP9 小數點0 e.g.0.123 前面2個,就會被取代0,變成0.003


9 Numeric 數字
A Alphabetic 字母
X Alphanumeric 字母數字
V Implicit Decimal隱式十進制
S Sign
P Assumed Decimal 假定十進制
```
### Alias
```
IDENTIFICATION DIVISION.
PROGRAM-ID. MULTIPLICATION-TABLE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 TESTA.
    05 MIDDLE.
        10 STAR PIC 9(2) VALUE 5.
01 TESTB.
    05 MIDDLE.
        10 STAR PIC 9(2) VALUE 10.
01 TESTC.
    05 MIDDLE.
        10 STAR PIC 9(2) VALUE 99.
        
PROCEDURE DIVISION.
    MOVE STAR OF MIDDLE OF TESTA TO STAR OF MIDDLE OF TESTC
    MOVE CORR TESTA TO TESTC. //另一寫法
    
    DISPLAY 'TESTA : ', STAR OF MIDDLE OF TESTA.
    DISPLAY 'TESTB : ', STAR OF MIDDLE OF TESTB.
    DISPLAY 'TESTC : ', STAR OF MIDDLE OF TESTC.
STOP RUN.
```
```
TESTA : 05
TESTB : 10
TESTC : 05
```
### BMI計算
```
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
AUTHOR. Jeff
DATE-WRITTEN. 2023-05-05.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 height PIC 999v99.
01 weight PIC 999v99.
01 BMI PIC 99v99.
PROCEDURE DIVISION.
DISPLAY "please input your height(cm)" WITH NO ADVANCING.
ACCEPT height.  //接值
DISPLAY "please input your weight(kg)" WITH NO ADVANCING.
ACCEPT weight.
COMPUTE height = height / 100.
COMPUTE BMI = weight / (height * height).
DISPLAY "your BMI is: " BMI
STOP RUN.
```
### 四則運算
```
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
AUTHOR. Jeff
DATE-WRITTEN. 2023-05-05.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 age PIC 9(4).
01 result PIC 9(4).
PROCEDURE DIVISION.
DISPLAY "the first value is " age.
MOVE 1450 TO age. //賦值
DISPLAY "the second value is " age.
ADD 50 TO age.  //加
DISPLAY "the third value is " age.
SUBTRACT 200 FROM age.  //減
DISPLAY "the forth value is " age.
MULTIPLY 2 BY age.  //乘
DISPLAY "the fifth value is " age.
DIVIDE 2 INTO age GIVING result.  //除
DISPLAY "the sixth value is " result.
STOP RUN.
```
### IF...ELSE
* 關係條件Case1
```
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).
PROCEDURE DIVISION.
A000-FIRST-PARA.
MOVE 25 TO WS-NUM1 WS-NUM3.
MOVE 15 TO WS-NUM2 WS-NUM4.
IF WS-NUM1 > WS-NUM2 THEN
DISPLAY 'IN LOOP 1 - IF BLOCK'
   IF WS-NUM3 = WS-NUM4 THEN
   DISPLAY 'IN LOOP 2 - IF BLOCK'
   ELSE
   DISPLAY 'IN LOOP 2 - ELSE BLOCK'
   END-IF
ELSE
DISPLAY 'IN LOOP 1 - ELSE BLOCK'
END-IF.
STOP RUN.
```
```
IN LOOP 1 - IF BLOCK
IN LOOP 2 - ELSE BLOCK
```
* 關係條件Case2
```
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUM1 PIC 9(9).
01 WS-NUM2 PIC 9(9).
PROCEDURE DIVISION.
A000-FIRST-PARA.
MOVE 25 TO WS-NUM1.
MOVE 15 TO WS-NUM2.
IF WS-NUM1 IS GREATER THAN OR EQUAL TO WS-NUM2 THEN
DISPLAY 'WS-NUM1 IS GREATER THAN WS-NUM2'
ELSE
DISPLAY 'WS-NUM1 IS LESS THAN WS-NUM2'
END-IF.
STOP RUN.
```
* 檢查數位運算元條件
```
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUM1 PIC S9(9) VALUE -1234.
01 WS-NUM2 PIC S9(9) VALUE 123456.
PROCEDURE DIVISION.
A000-FIRST-PARA.
IF WS-NUM1 IS POSITIVE THEN
DISPLAY 'WS-NUM1 IS POSITIVE'.
IF WS-NUM1 IS NEGATIVE THEN
DISPLAY 'WS-NUM1 IS NEGATIVE'.
IF WS-NUM1 IS ZERO THEN
DISPLAY 'WS-NUM1 IS ZERO'.
IF WS-NUM2 IS POSITIVE THEN
DISPLAY 'WS-NUM2 IS POSITIVE'.
STOP RUN.
```
```
WS-NUM1 IS NEGATIVE
WS-NUM2 IS POSITIVE
```
* 類型條件
```
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUM1 PIC X(9) VALUE 'ABCD '.
01 WS-NUM2 PIC 9(9) VALUE 123456789.
PROCEDURE DIVISION.
A000-FIRST-PARA.
IF WS-NUM1 IS ALPHABETIC THEN
DISPLAY 'WS-NUM1 IS ALPHABETIC'.
IF WS-NUM1 IS NUMERIC THEN
DISPLAY 'WS-NUM1 IS NUMERIC'.
IF WS-NUM2 IS NUMERIC THEN
DISPLAY 'WS-NUM2 IS NUMERIC'.
STOP RUN.
```
```
WS-NUM1 IS ALPHABETIC
WS-NUM2 IS NUMERIC
```
### EVALUATE
```
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 WS-A PIC 9 VALUE 0.
   
PROCEDURE DIVISION.
   MOVE 3 TO WS-A.
   
   EVALUATE TRUE
      WHEN WS-A > 2
         DISPLAY 'WS-A GREATER THAN 2'

      WHEN WS-A < 0
         DISPLAY 'WS-A LESS THAN 0'

      WHEN OTHER
         DISPLAY 'INVALID VALUE OF WS-A'
   END-EVALUATE.
   
STOP RUN.
```
```
WS-A GREATER THAN 2
```
### 迴圈語句
* 多行執行
```
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'.
```
```
IN A-PARA
IN C-PARA
IN D-PARA
IN E-PARA
IN B-PARA
```
* GO TO 子句
```
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 X PIC 9 VALUE 0.
   
PROCEDURE DIVISION.
A-PARA.
PERFORM C-PARA THRU E-PARA.
B-PARA.
DISPLAY 'IN B-PARA'.
STOP RUN.
C-PARA.
DISPLAY 'IN C-PARA'.
MOVE 1 TO X
IF X = 1 THEN GO TO E-PARA
END-IF.
D-PARA.
DISPLAY 'IN D-PARA'.
E-PARA.
DISPLAY 'IN E-PARA'.
```
```
IN C-PARA
IN E-PARA
IN B-PARA
```
* 條件判斷控制
```
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.
```
```
WS-CNT : 0
WS-CNT : 1
WS-CNT : 2
WS-CNT : 3
```
* 指定被執行次數Case1
```
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'.
```
```
IN B-PARA
IN B-PARA
IN B-PARA
```
* 指定被執行次數Case2
```
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 WS-A PIC 9 VALUE 0.

PROCEDURE DIVISION.
   MOVE 0 TO WS-A.
   
A-PARA.
PERFORM B-PARA 5 TIMES.
STOP RUN.   

B-PARA.     
   EVALUATE TRUE
      WHEN WS-A < 3
      ADD 1 TO WS-A
         DISPLAY WS-A
         DISPLAY 'WS-A LESS THAN 2'

      WHEN WS-A > 2
      ADD 1 TO WS-A
         DISPLAY WS-A      
         DISPLAY 'WS-A GRATER THAN 2'

      WHEN OTHER
         DISPLAY 'INVALID VALUE OF WS-A'
   END-EVALUATE.
```
```
1
WS-A LESS THAN 2
2
WS-A LESS THAN 2
3
WS-A LESS THAN 2
4
WS-A GRATER THAN 2
5
WS-A GRATER THAN 2
```
### PERFORM AND IF
```
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
AUTHOR. Jeff
DATE-WRITTEN. 2023-05-05.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 X PIC 9(4).
01 Y PIC X(4).
PROCEDURE DIVISION.
PERFORM PROCESS-A UNTIL Y = 'NO'
STOP RUN.

PROCESS-A.
PERFORM UNTIL X > 3
DISPLAY 'PROCESS-A : ' , X
ADD 1 TO X
IF X = 3 THEN  MOVE 'NO' TO Y
END-IF
END-PERFORM.
```
```
PROCESS-A : 0000
PROCESS-A : 0001
PROCESS-A : 0002
PROCESS-A : 0003
```
### 9x9 Table
```
IDENTIFICATION DIVISION.
PROGRAM-ID. MULTIPLICATION-TABLE.

DATA DIVISION.
WORKING-STORAGE SECTION.
01  X            PIC 99 VALUE 1.
01  Y            PIC 99 VALUE 1.
01  RESULT       PIC 99.

PROCEDURE DIVISION.
MULTIPLY-LOOP.
    PERFORM UNTIL X > 9
        MOVE 1 TO Y
        PERFORM UNTIL Y > X
            MULTIPLY X BY Y GIVING RESULT
            DISPLAY X, '*', Y, '=', RESULT , " " WITH NO ADVANCING
            ADD 1 TO Y
        END-PERFORM
            DISPLAY " "
        ADD 1 TO X
    END-PERFORM.
STOP RUN.
```
### Triangle
```
IDENTIFICATION DIVISION.
PROGRAM-ID. MULTIPLICATION-TABLE.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 X PIC 9(2) VALUE 1.
01 Y PIC 9(2) VALUE 1.
01 RESULT PIC 9(3).

PROCEDURE DIVISION.
    PERFORM UNTIL X > 5
    MOVE 1 TO Y
        PERFORM UNTIL Y > X
            DISPLAY "*" WITH NO ADVANCING
            ADD 1 TO Y
        END-PERFORM 
            DISPLAY " "
        ADD 1 TO X
    END-PERFORM 
STOP RUN.
```
### 字串串接
```
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-STRING PIC A(30).
01 WS-STR1 PIC A(4) VALUE 'TEST'.
01 WS-STR2 PIC A(3) VALUE '123'.
PROCEDURE DIVISION.
STRING WS-STR1,WS-STR2 
INTO WS-STRING 
END-STRING.
DISPLAY 'WS-STRING : 'WS-STRING.
STOP RUN.
```
```
WS-STRING : TEST123  
```
### Tree
```
IDENTIFICATION DIVISION.
PROGRAM-ID. MULTIPLICATION-TABLE.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 X PIC 9(2) VALUE 1.
01 Y PIC 9(2) VALUE 1.
01 Z PIC 9(2) VALUE 1.
01 LAYER PIC 9(2) VALUE 10.
01 STAR PIC 9(2).
01 NOSTAR PIC 9(2).
01 RESULT PIC 9(3).

PROCEDURE DIVISION.
    PERFORM UNTIL X > LAYER
    MOVE 1 TO Y
    MOVE 1 TO Z
    MULTIPLY 2 BY X GIVING STAR
    SUBTRACT 1 FROM STAR
    SUBTRACT X FROM LAYER GIVING NOSTAR
       PERFORM UNTIL Y > NOSTAR
       IF Y = 1 THEN 
            DISPLAY '-' WITH NO ADVANCING
            ADD 1 TO Y
        ELSE 
            DISPLAY ' ' WITH NO ADVANCING
            ADD 1 TO Y 
        END-IF
        END-PERFORM 
        PERFORM UNTIL Z > STAR
            DISPLAY "*" WITH NO ADVANCING
            ADD 1 TO Z
        END-PERFORM 
            DISPLAY " "
        ADD 1 TO X
    END-PERFORM 
STOP RUN.
```