GnuCOBOL

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
 WORKING-STORAGE SECTION.
  01 MY-COLORS.
    03 MY-COLOR OCCURS 3 PIC X(10).
PROCEDURE DIVISION.
  MAIN SECTION.
    MOVE "red" TO MY-COLOR(1).
    MOVE "yellow" TO MY-COLOR(2).
    MOVE "blue" TO MY-COLOR(3).
    DISPLAY MY-COLORS.
    DISPLAY MY-COLOR(2).
    STOP RUN.
[vagrant@localhost cobol]$ cobc -x --free hello.cob
[vagrant@localhost cobol]$ ./hello
red       yellow    blue
yellow

演算

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
 WORKING-STORAGE SECTION.
  01 MY-DATA.
    03 D1 PIC 99999 VALUE 0.
    03 D2 PIC 99999 VALUE 0.
    03 D3 PIC 99999 VALUE 0.
    03 D4 PIC 99999 VALUE 0.
PROCEDURE DIVISION.
  MAIN SECTION.
    ADD 5 TO D1 D2 D3.
    SUBTRACT 2 FROM D1 D2.
    DISPLAY MY-DATA.
    STOP RUN.

入力受付

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
 WORKING-STORAGE SECTION.
  01 MY-DATA PIC X(10).
PROCEDURE DIVISION.
  MAIN SECTION.
    DISPLAY "whats your name?".
    ACCEPT MY-DATA.
    DISPLAY "hello! " MY-DATA.
    STOP RUN.

条件分岐

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
 WORKING-STORAGE SECTION.
  01 MY-SCORE PIC 9(3).
PROCEDURE DIVISION.
  MAIN SECTION.
    MOVE 80 TO MY-SCORE.
    If MY-SCORE > 60 THEN
      DISPLAY "OK!"
    END-IF.
    STOP RUN.

条件分岐

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
 WORKING-STORAGE SECTION.
  01 MY-SIGNAL PIC X(5).
PROCEDURE DIVISION.
  MAIN SECTION.
    MOVE "red" TO MY-SIGNAL.
    EVALUATE MY-SIGNAL
      WHEN "red"
        DISPLAY "stop!"
      WHEN "blue"
          DISPLAY "run!"
      WHEN "yello"
            DISPLAY "caution!"
    END-EVALUATE.
    STOP RUN.

繰り返し処理

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
 WORKING-STORAGE SECTION.
  01 MY-COUNTER PIC 9(3) VALUE 1.
PROCEDURE DIVISION.
  MAIN SECTION.
    PERFORM 10 TIMES
      DISPLAY "counter = " MY-COUNTER
      ADD 1 TO MY-COUNTER
    END-PERFORM.
    STOP RUN.

ファイルの書き込み

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
ENVIRONMENT DIVISION.
  INPUT-OUTPUT SECTION.
    FILE-CONTROL.
      SELECT OUT-FILE ASSIGN TO 'out.txt'
        ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
  FILE SECTION.
    FD OUT-FILE.
      01 OUT-FILE-REC PIC 99.
 WORKING-STORAGE SECTION.
  01 LINE-COUNT PIC 99 VALUE 0.
PROCEDURE DIVISION.
  MAIN SECTION.
    OPEN OUTPUT OUT-FILE.

    PERFORM 10 TIMES
      ADD 1 TO LINE-COUNT
      MOVE LINE-COUNT TO OUT-FILE-REC
      WRITE OUT-FILE-REC
      DISPLAY OUT-FILE-REC
    END-PERFORM.

    CLOSE OUT-FILE.
    STOP RUN.

ファイルの読み出し

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
ENVIRONMENT DIVISION.
  INPUT-OUTPUT SECTION.
   FILE-CONTROL.
    SELECT IN-FILE ASSIGN TO 'in.txt'
     ORGANIZATION IS LINE SEQUENTIAL
     STATUS IN-FILE-STATUS.
DATA DIVISION.
  FILE SECTION.
   FD IN-FILE.
    01 In-FILE-REC PIC X(20).
  WORKING-STORAGE SECTION.
   01 IN-FILE-STATUS PIC XX.
PROCEDURE DIVISION.
  MAIN SECTION.
    OPEN INPUT IN-FILE.

    PERFORM UNTIL IN-FILE-STATUS NOT = "00"
     READ IN-FILE
       AT END
         DISPLAY "-- end --"
       NOT AT END
         DISPLAY "Content: " IN-FILE-REC
    END-READ
    END-PERFORM.

    CLOSE IN-FILE.
    STOP RUN.