§ — — Structured Programming (COBOL)
After studying this lesson, learners should be able to:
PIC characters and editing characters.A COBOL data name should:
Good examples:
STUDENT-NO
STUDENT-NAME
TOTAL-SALES
NET-PAY
EOF-SW
Weak examples:
A1 too vague
SID unclear abbreviation
JOHN person-specific and not descriptive
TOTAL too broad if several totals exist
COBOL programs use both changing and fixed data.
| Type | Meaning | Example |
|---|---|---|
| Variable data | Data that can change during processing. | TOTAL-SALES, COUNTER, AVERAGE-GRADE |
| Constant data | Fixed data coded into the program. | VALUE 0, VALUE "YES", report headings |
Business programs usually organize data in layers:
Example:
File: STUDENT-FILE
Record: STUDENT-RECORD
Fields: STUDENT-NO, STUDENT-NAME, MIDTERM-GRADE, FINAL-GRADE
COBOL commonly uses three field categories:
| Field type | PIC symbol | Use |
|---|---|---|
| Numeric | 9 | Numbers used for arithmetic. |
| Alphabetic | A | Letters and spaces. |
| Alphanumeric | X | Letters, digits, spaces, and symbols. |
Examples:
05 STUDENT-NAME PIC X(30).
05 AGE PIC 99.
05 SECTION-CODE PIC X(10).
05 LAST-NAME PIC A(20).
Rules for numeric literals:
0 through 9.-125 or +50.Examples:
| Valid | Invalid | Reason invalid |
|---|---|---|
10.50 | 10,000 | Commas are not allowed in numeric literals. |
-387.58 | $225 | Currency symbol is not part of the numeric literal. |
+10000 | 175- | Sign must not appear at the right. |
A group field contains smaller fields. An elementary field cannot be subdivided further.
Example:
01 STUDENT-RECORD.
05 STUDENT-NO PIC X(10).
05 STUDENT-NAME.
10 LAST-NAME PIC X(15).
10 FIRST-NAME PIC X(15).
10 MIDDLE-INITIAL PIC X.
05 FINAL-GRADE PIC 9V99.
Here, STUDENT-RECORD and STUDENT-NAME are group items. STUDENT-NO, LAST-NAME, FIRST-NAME, MIDDLE-INITIAL, and FINAL-GRADE are elementary items.
A common Data Division contains two major sections:
DATA DIVISION.
FILE SECTION.
FD file-name.
01 record-name.
05 field-name PIC clause.
WORKING-STORAGE SECTION.
01 variable-name PIC clause VALUE initial-value.
The File Section describes input and output files and their records.
FILE SECTION.
FD STUDENT-FILE
LABEL RECORD IS STANDARD
RECORD CONTAINS 41 CHARACTERS
DATA RECORD IS STUDENT-RECORD.
01 STUDENT-RECORD.
05 STUDENT-NO PIC X(10).
05 STUDENT-NAME PIC X(25).
05 MIDTERM-GRADE PIC 9V99.
05 FINAL-GRADE PIC 9V99.
Common File Description entries:
| Entry | Meaning |
|---|---|
FD | Begins the file description and names the file. |
LABEL RECORD | Describes whether system labels are standard or omitted. |
RECORD CONTAINS | Indicates record size. |
DATA RECORD IS | Names the record associated with the file. |
01 record-name | Starts the record description. |
The Working-Storage Section stores variables, counters, switches, report headings, and intermediate results that are not directly part of an input file.
WORKING-STORAGE SECTION.
01 EOF-SW PIC 9 VALUE 0.
01 RECORD-COUNT PIC 9(4) VALUE 0.
01 TOTAL-GRADE PIC 9(5)V99 VALUE 0.
01 AVERAGE-GRADE PIC 9V99 VALUE 0.
Common uses:
The PICTURE or PIC clause defines the type and size of an elementary data item.
05 CUSTOMER-NAME PIC X(30).
05 QUANTITY PIC 9(3).
05 PRICE PIC 9(5)V99.
Rules:
PIC clause.PIC clause.PIC 9999 and PIC 9(4) mean the same thing.9, not X.| Character | Meaning | Example |
|---|---|---|
9 | Numeric digit | PIC 9(3) stores three digits. |
V | Assumed decimal point | PIC 999V99 treats stored 12345 as 123.45. |
X | Alphanumeric character | PIC X(20) stores up to 20 characters. |
A | Alphabetic character | PIC A(15) stores letters/spaces. |
S | Signed numeric value | PIC S9(4) allows positive/negative values. |
P | Decimal scaling position | Used for special scaling cases. |
VThe V character marks a decimal location without storing an actual decimal point.
| Stored digits | PIC clause | Interpreted value |
|---|---|---|
12050 | PIC 999V99 | 120.50 |
020050 | PIC 999V999 | 020.050 |
125 | PIC 99V9 | 12.5 |
FILLER reserves positions that do not need a unique name.
Example:
01 PRINT-LINE.
05 FILLER PIC X(10) VALUE SPACES.
05 PRINT-NAME PIC X(25).
05 FILLER PIC X(5) VALUE SPACES.
05 PRINT-GRADE PIC ZZ9.99.
Use FILLER for spacing, labels, or unused areas in report records.
The VALUE clause gives an initial value to a Working-Storage item.
05 WS-DISCOUNT PIC V99 VALUE .15.
05 WS-LAB-FEE PIC 9(3) VALUE 350.
05 WS-MORE-RECORDS PIC X(3) VALUE "YES".
05 WS-TITLE PIC X(14) VALUE "MONTHLY REPORT".
Data editing formats values for output. It is usually done by moving a raw numeric value into an edited field.
Common editing symbols:
| Symbol | Use |
|---|---|
. | Actual decimal point in displayed output. |
, | Comma insertion. |
Z | Suppress leading zeroes. |
B | Insert blank. |
0 | Insert zero. |
/ | Insert slash, often for dates. |
$ | Insert currency symbol. |
+ or - | Show sign. |
Examples:
05 AMOUNT-IN PIC 9(5)V99 VALUE 0250025.
05 AMOUNT-OUT PIC ZZ,ZZ9.99.
05 DATE-IN PIC 9(6) VALUE 011591.
05 DATE-OUT PIC 99/99/99.
After moving DATE-IN to DATE-OUT, the displayed date is formatted as 01/15/91.
ProReviewer — locked
Drills, code labs, and full solutions.
ProReviewer — locked
Drills, code labs, and full solutions.
Done with this module? Track it — your progress shows on the subject list.
Up next
Procedure Division and COBOL Statements→←Previous: Identification Division and Environment Division