COBOL PROGRAM – STRUCTURE
A. IDENTIFICATION DIVISION
Tells the computer the name of the program, and supplies other documentation
000100 IDENTIFICATION DIVISION.
000110 PROGRAM-ID. EXAMPLE-1-PROG. 000120 AUTHOR. ZINGMATTER.
000130 INSTALLATION. XYZ GROUP. 000140 DATE-WRITTEN. 17/5/00. 000150 DATE-COMPILED.
000160 SECURITY. LOCAL GROUP
B. ENVIRONMENT DIVISION
tells the computer what the program will be interacting with (i.e. its environment) such as:
printers disk drives other files etc
Two important Sections:
CONFIGURATION SECTION, defines the source and object computer
INPUT-OUTPUT SECTION, defines printers, files that may by used and assigns identifier names to these external
features 000260 ENVIRONMENT DIVISION. 000270 CONFIGURATION SECTION. 000280 SOURCE-COMPUTER. IBM PC. 000290 OBJECT-COMPUTER. IBM PC. 000300 INPUT-OUTPUT SECTION. 000310 FILE-CONTROL.
000320 SELECT INPUT-FILE ASSIGN TO ‘input.dat’ 000330 ORGANIZATION IS LINE SEQUENTIAL
.
C. DATA DIVISION
Is where memory space in the computer is allocated to data
and identifiers that are to be used by the program.
Two important sections of this division are the FILE SECTION
The file section is used to define the structure, size and type of the data that will be read from or written to a file.
WORKING-STORAGE SECTION 000400 DATA DIVISION. 000410 FILE SECTION. 000420 000430 FD INPUT-FILE. 000440 01 CUSTOMER-DATA. 000450 03 NAME PIC X(12). 000460 03 ADDRESS. 000470 05 HOUSE-NUMBER PIC 99. 000480 05 STREET PIC X(19). 000490 05 CITY PIC X(13). 000500 03 CUST-NUMBER PIC 9(6).
Joe Bloggs 20Shelly Road Bigtown 023320 John Dow 15Keats Avenue Nowheresville 042101 Jock MacDoon 05Elliot Drive Midwich 100230
FD stands for File Descriptor, and names the file, INPUT-FILE
(assigned in the environment division), and describes the exact structure of the data in each record. All records in this file MUST be of exactly the same structure.
01 CUSTOMER-DATA is the group name and refers to all of the
single record that is read into the computer memory from the file. The higher numbers (levels), 03.. and 05.. will contain the indivual fields of the record.
Level 01 is sub-grouped into level 03 fields. Notice that one of the level 03 sub-groups is itself subgrouped into level 05.
The PIC (short for PICTURE) clause indicates the size and type of data that that field contains.
D. PROCEDURE DIVISION
The procedure division is where the logic of the program actually found.
Here is where the various commands are written (see Commands and logic section).
COBOL is a modular language, in that a program is usually broken up into units described as paragraphs.
000900 PROCEDURE DIVISION. 000910 CONTROL-PARAGRAPH. 000920 PERFORM READ-DATA-FILE 000930 PERFORM CALULATE-PRICES 000940 PERFORM PRINT-PRICE-REPORT 000950 STOP RUN.
USER-DEFINED WORDS:
Maksimal 30 karakter
Hanya boleh menggunakan alfabet, angka, dan hypen Spasi tidak diperbolehkan
Tidak dimulai atau diakhiri dengan hypen
Tidak boleh mempergunakan COBOL Reserved Words
Aturan Pemberian Nama
Valid Name Invalid Name Explanation of Invalid Name
TOTAL-DOLLARS TOTAL-$ Uses an invalid $ in the name
SUM-OF-COLUMNS
sum-of-columns Uses lowercase letters
7-BY-5 7_BY_5 Uses the invalid _ character in
the name
MINUS-RESULT -RESULT Starts with a hyphen
BOEING-707-SEATS
BOEING-707-MAXIMUM-
SEATING-CAPACITY Exceeds 30 characters
Bentuk Data dalam COBOL:
Numeric Angka Non-Numeric Alfabet Alfanumerik Figurative Constants:
ZERO atau ZEROES SPACE(S) HIGH-VALUE(S) LOW-VALUE(S) QUOTE(S) ALL literal
PART I - THE PARTS OF A COBOL PROGRAM 000100 IDENTIFICATION DIVISION. 000200 PROGRAM-ID. MINIMUM. 000300 ENVIRONMENT DIVISION. 000400 DATA DIVISION. 000500 PROCEDURE DIVISION. 000600 000700 PROGRAM-DONE. 000800 STOP RUN.
The IDENTIFICATION DIVISION:
menandakan awal program COBOL. Nama dari program, akan
dimasukkan sebagai statement (pernyataan) di dalam IDENTIFICATION DIVISION.
PROGRAM-ID adalah sebuah paragraph yang harus ada di dalam dan digunakan untuk memberikan sebuah nama kepada program
The ENVIRONMENT DIVISION:
berisi pernyataan (statement) atau perintah untuk mendeskripsikan lingkungan fisik tempat program berjalan.
Kegunaan utama dari divisi ini adalah untuk mendeskripsikan struktur fisik dari file yang akan digunakan di dalam program.
The DATA DIVISION:
berisi pernyataan yang menggambarkan data yang digunakan oleh program. DATA DIVISION dan PROCEDURE DIVISION adalah divisi-divisi yang terpenting di dalam sebuah program COBOL.
sekitar 95% pekerjaan dilakukan pada kedua divisi tersebut The PROCEDURE DIVISION:
berisi pernyataan-pernyataan COBOL yang akan dieksekusi program setelah program mulai berjalan. PROCEDURE DIVISION adalah pekerja sebenarnya dari sebuah program COBOL.
Tanpa adanya sebuah PROCEDURE DIVISION, anda tidak akan memiliki sebuah program.
Harus ada sebuah paragraph dalam divisi ini yang berisi pernyataan STOP RUN DIVISION SECTIONs Paragraphs Sentences
A. FIVE AREAS OF A COBOL PROGRAM
1. Sequence Number Area, Enam kolom pertama dari baris yang
tidak akan diproses oleh compiler.
2. Indicator Area, Posisi karakter ke-7 biasanya adalah blank. Jika
sebuah asterisk diletakkan pada kolom ini, maka yang lainnya pada baris tersebut akan diabaikan oleh compiler. Hal ini digunakan sebagai cara untuk memasukkan sebuah komentar (comment) di dalam source code anda.
3. Area A, empat karakter pertama mulai dari kolom ke-8 hingga
kolom ke-11 disebuat sebagai Area A. DIVISION, paragraph, dan SECTION harus dimulai pada Area A.
4. Area B, posisi karakter 12 hingga 72. Sentences harus dimulai dan
diakhiri di dalam Area B.
5. Identification Area, kolom 73 hingga kolom 80. Area ini disediakan
bagi para pemrogram untuk digunakan seperlunya.
Listing 1.5. The areas of a COBOL program. ---| Area A 000100 IDENTIFICATION DIVISION. 000200 PROGRAM-ID. COMMENT. 000300 ENVIRONMENT DIVISION. 000400 DATA DIVISION. 000500 PROCEDURE DIVISION. 000600 000700* This is a comment.
000800* This paragraph displays information about the program. 000900 PROGRAM-BEGIN.
---| Area B
003700 DISPLAY "This program contains four DIVISIONS,". 003800 DISPLAY "three PARAGRAPHS".
001000 DISPLAY "and four SENTENCES".
---| Area A
001100 PROGRAM-DONE.
---| Area B
VARIABEL
Sebuah variable adalah sebuah nilai yang dapat dirubah ketika program sedang berjalan
Ketika sebuah variable diciptakan di dalam program, sebuah area di memori (RAM) disisihkan untuk menyimpan nilai-nilai. Pada sebagian besar bahasa pemrograman, termasuk COBOL, sebuah variable diberikan sebuah nama. Nama itu dapat digunakan di dalam program untuk
merujuk pada nilai.
Nilai yang disimpan di dalam memori dapat diubah ketika program sedang berjalan dengan menggunakan nama variable
Mendefinisikan Variable Numerik dalam COBOL
001400 01 THE-NUMBER PICTURE IS 9999.
Sebuah variable COBOL paling sedikit memiliki tiga bagian: Level number, menentukan derajat dan kegunaan Nama
PICTURE, mendefinisikan dua hal tentang sebuah variable: ukuran dari variable (jumlah byte yang digunakan dalam memori untuk sebuah nilai) dan tipe data yang dapat disimpan di dalam variable.
TYPE: Listing 1.4. Three levels of COBOL grammar.
000100 IDENTIFICATION DIVISION. 000200 PROGRAM-ID. SENTNCES. 000300 ENVIRONMENT DIVISION. 000400 DATA DIVISION. 000500 PROCEDURE DIVISION. 000600 000700 PROGRAM-BEGIN.
000800 DISPLAY "This program contains four DIVISIONS,". 000900 DISPLAY "three PARAGRAPHS".
001000 DISPLAY "and four SENTENCES". 001100 PROGRAM-DONE.
001200 STOP RUN.
OUTPUT:
C>pcobrun comment
Personal COBOL version 2.0 from Micro Focus
PCOBRUN V2.0.02 Copyright (C) 1983-1993 Micro Focus Ltd. This program contains four DIVISIONS,
three PARAGRAPHS and four SENTENCES
TYPE: Listing 2.4. Using variables. 000100 IDENTIFICATION DIVISION. 000200 PROGRAM-ID. ADD01. 000300 ENVIRONMENT DIVISION. 000400 DATA DIVISION. 000500 000600 WORKING-STORAGE SECTION. 000700 000800 01 FIRST-NUMBER PICTURE IS 99. 000900 01 SECOND-NUMBER PICTURE IS 99. 001000 01 THE-RESULT PICTURE IS 999. 001100 001200 PROCEDURE DIVISION. 001300 001400 PROGRAM-BEGIN. 001500
001600 DISPLAY "Enter the first number.". 001700
001800 ACCEPT FIRST-NUMBER. 001900
002000 DISPLAY "Enter the second number.". 002100
002200 ACCEPT SECOND-NUMBER. 002300
002400 COMPUTE THE-RESULT = FIRST-NUMBER + SECOND-NUMBER.
002500
002600 DISPLAY "The result is:". 002700 DISPLAY THE-RESULT. 002800
002900 PROGRAM-DONE. 003000 STOP RUN. 003100
Enter the first number.
97
Enter the second number.
33
The result is: 130
C>
ANALYSIS: Take a look at the DATA DIVISION. Here is your first example of a
section, the WORKING-STORAGE SECTION. A SECTION in COBOL is created by typing a name, similar to a paragraph name, followed by one or more spaces, followed by the word SECTION and a period. SECTIONs in COBOL can be required or
optional, depending on which DIVISION they are in. WORKING-STORAGE
SECTION is a reserved name and a required section in the DATA DIVISION if your program uses any variables--and most programs do.