-
Cobol
Common business oriented language
-
been in use since
the 1960's
-
Record Format
- Col 1-6: card sequence number, not often used any more
- Col 7: control character
- * comment record
- D debug record
- - literal continuation
- Col 8-11: A-area
- division, section and paragraph names
- FD entries, 01 level numbers
- Col 12-72: B-area
- all other entries
- Col 73-80: Program identification(compiler ignores)
-
Col1-6
- Col 7
- Col1-6: card sequence number
- Col7: * comment
- D debug record
- - literal
-
Col 8-11
- Col 12-72
- Coll73-80
- Col 8-11: A area
- divisions, section, paragraph names
- FD entries, 01 level numbers
- Col 12-72: B-area
- all other code
- Coll73-80: Program identification
- compiler ignores
-
main divisions
- Identification: program name and comments
- Environment: file references to external OS file name
- eg People.dat F01-PEOPLE-FILE
- DATA: define variables
- File section: defines input record and output record
- input has 3 field: name, address, age
- output contains: name, age
- Working Storage section: all other variable declarations
- Procedure: define the program logic
-
blanks
- are use to separate names and commands
- Multiple blanks are treated as a single blank.
- Lines can be continued without a continuation character.
-
Names
- programmer defined name(paragraph names, variable names) must be:
- 1 to 30 characters
- a-z, A-Z, numbers, -
- not begin or end with -
- normally begin with letter
- use meaningful names
- F01-READ
-
Variables
- must have numeric "level number"
- root variables start with level 01.
- standard definition is:
- level# varName format data-value period
- 01 W01-CUSTOMERNUMBER PIC X(5)
- VALUE “ax345”.
- 01 W02-ADDRESS.
- 05 W02-STREET PIC X(25)
- VALUE “123 Main St.”.
- 05 W02-CITYPIC X(15)
- VALUE “Kingston”.01 W03-AMOUNTOWING PIC 999.
- CUSTOMERNUMBER is a variable that stores 5 bytes
- The ADDRESS variable is 40 bytes of memory.
- AMOUNTOWING stores a number less than 1000.
-
Picture Clause
- Define format of variables by defining a picture of what you want, use PIC for short.
- 9 to define one byte to hold a numeric digit.
- X to define one byte to hold an alpha-numeric character.
- V to indicate an “implied decimal”. To save disk and memory, the period is not stored with the data.
-
PIC 999999 is same
- PIC XXXXXXX is same as
- PIC 99V99 is used to store a number like
- PIC 999999 is same as PIC 9(6)
- PIC XXXXXXX is same as PIC X(7)
- PIC 99V99 is used to store a number like 12.76
-
Examples of
- numeric literals:
- non-numeric literals:
numeric literals: 17, -893, 47.592
-
non-numeric literals: "Cat", "613 544 5400"
-
Procedure Division
- paragraph.
- A paragraph starts with a programmer defined name. It can have many statements and sentences.The first paragraph does not need a paragraph name. The last statement in a paragraph must end with a period.
-
Paragraph Names
- Name should describe what the paragraph does (its function).
- should only do ONE function.
- Use format of “nnn-verb-noun”.
- Example “100-INITIALIZE-PROGRAM”.
- Follow every paragraph name with comments to describe the function of paragraph.
-
Move
- MOVE statement : copies the data from a sending memory location to a receiving memory location.
- assignment statement.
- MOVE sending-var TO receiving-var
- MOVE “Bob” TO MY-NAME
- MOVE MY-NAME TO YOUR-NAME
-
PERFORM
- will cause a paragraph to be executed (a procedure call).
- Simple format is:
- PERFORM paragraph-name
- This means go to the paragraph that has this name, execute all the statements in that paragraph, then return to here and fall to the next statement.
-
Looping
- A loop is used run code repeatedly until a condition is true.
- Syntax:
- PERFORM paragraph-name UNTIL conditional-expression
- Example:
- PERFORM 200-PROCESS-RECORD
- UNTIL W01-EOF-SWITCH = ‘Y’
- PERFORM 200-PROCESS-RECORDS
- UNTIL W01-DATA-REMAINS-SWITCH = 'NO'
-
If
- Syntax:
- IF conditional-expression
- true-statements
- ELSE
- false-statements
- END-IF
- Example:
- IF W01-MY-CITY = ‘Kingston’
- MOVE 10.00 TO W02-DELIVERY-FEE
- MOVE ‘LOCAL’ TO W02-CARRIER-NAME
- ELSE
- MOVE 25.00 TO W02-DELIVERY-FEE
- MOVE ‘UPS’ TO W02-CARRIER-NAME
- END-IF
- Programming standards require an END-IF statement to complete the condition.
-
Continue Statement
- A Key word/null command: do nothing.
- Used as a place holder in IF statements.
- Helpful to allow the asking of a positive question but only do action if it is not true.
- IF AGE IS NOT NUMERIC
- DISPLAY “Bad AGE value”
- END-IF
- IF AGE IS NUMERIC
- CONTINUE
- ELSE
- DISPLAY “Bad AGE value”
- END-IF
-
name the main divisions
- IDENTIFICATION
- ENVIRONMENT
- DATA
- PROCEDURE
-
The identification division is
- The identification division is the first and only
- mandatory division of every COBOL program.
- contains program name and author's name.
-
COBOL program structure
- program
- Divisions
- Sections
- Paragraphs
- Sentences/Statements
-
file record field
- field: basic fact
- record: a set of fields
- file: a set of records
-
IDENTIFICATION DIVISION.
- IDENTIFICATION DIVISION.
- PROGRAM-ID. SENIOR.
- AUTHOR. ROBERT GRAUER.
-
ENVIRONMENT DIVISION.
associates the file names referenced in program to the input and output devices recognized by the OS
- ENVIRONMENT DIVISION.
- INPUT-OUTPUT SECTION.
- FILE-CONTROL.
- SELECT F01-STUDENT-FILE ASSIGN
- TO 'SENIOR.DAT'
- ORGANIZATION IS LINE SEQUENTIAL.
- SELECT F02-PRINT-FILE ASSIGN
- TO 'SENIOR.OUT'
- ORGANIZATION IS LINE SEQUENTIAL.
-
template
- IDENTIFICATION DIVISION.
- PROGRAM-ID. PutIDHere.
- /AUTHOR. PutYourNameHere.
- ENVIRONMENT DIVISION.
- INPUT-OUTPUT SECTION.
- FILE-CONTROL.
- SELECT F01-SomeFile ASSIGN TO
- 'SomeFile.DAT'
- ORGANIZATION IS LINE
- SEQUENTIAL.
- SELECT F02-SomeFile ASSIGN TO
- 'SomeFile.OUT'
- ORGANIZATION IS LINE
- SEQUENTIAL.
- DATA DIVISION.
- FILE SECTION.
WORKING-STORAGE SECTION.
PROCEDURE DIVISION.
-
DATA DIVISION
describes all the data elements used by the program
-
FILE SECTION.
- FILE SECTION.
- * This is the definition of the input file.
- FD F01-STUDENT-FILE
- RECORD CONTAINS 43 CHARACTERS
- DATA RECORD IS
- F01-STUDENT-RECORD.
- 01 F01-STUDENT-RECORD.
- 05 F01-STU-NAME PIC X(25).
- 05 F01-STU-CREDITS PIC 9(3).
- 05 F01-STU-MAJOR PIC X(15).
- * This is the definition of the output file.
- FD F02-PRINT-FILE
- RECORD CONTAINS 132 CHARACTERS
- DATA RECORD IS
- F02-PRINT-LINE-RECORD.
- 01 F02-PRINT-LINE-RECORD PIC X(132).
-
WORKING-STORAGE SECTION.
- WORKING-STORAGE SECTION.
- 01 W01-DATA-REMAINS-SWITCH PIC X(2)
- VALUE SPACES.
- 01 W02-HEADING-LINE.
- 05 PIC X(10) VALUE SPACES.
- 05 PIC X(12) VALUE 'STUDENT NAME'.
- 05 PIC X(110) VALUE SPACES.
- 01 W03-DETAIL-LINE.
- 05 PIC X(8) VALUE SPACES.
- 05 W03-PRINT-NAME PIC X(25).
- 05 PIC X(99) VALUE SPACES.
-
Reserved words
- also called figurative constants
- SPACE SPACES
- ZERO ZEROS
- QUOTE QUOTES
- HIGH-VALUE HIGH-VALUES
- LOW-VALUE LOW-VALUES
-
generating blank lines
- MOVE SPACES TO F02-PRINT-LINE
- WRITE F02-PRINT-LINE
- on mainframe
- BEFORE ADVANCING x LINES
- AFTER ADVANCEING x LINES
-
Screen I/O
- DISPLAY "What is your favourite band?"
- To receive keyboard input:
- ACCEPT W01-BAND
-
READ
- READ F01-STUDENT-FILE
- AT END MOVE 'NO' TO W01-DATA-REMAINS-SWITCH
- END-READ
-
Hierarch
- PROCEDURE DIVISION.
- 100-HOUSEKEEPING
- 110-PRINT-HEADINGS
- 120-PRIME-READ
- 200-MAINLOOP
- 210-DO-MATH
- 220-PRINT-DATA
- 230-GET-INPUT
- 300-FINALIZE
|
|