Lesson 2: COBOL Character Set and COBOL Words
Learning goals
After studying this lesson, learners should be able to:
- Identify the basic COBOL character set.
- Distinguish between user-defined words and reserved words.
- Apply naming rules for files, records, and data fields.
- Read a basic average-grade COBOL example.
COBOL character set
Traditional COBOL uses letters, digits, spaces, and selected special symbols.
| Character type | Examples |
|---|---|
| Digits | 0 1 2 3 4 5 6 7 8 9 |
| Letters | A B C ... Z |
| Space | blank space |
| Special symbols | ( ) " = $ , ; + - * / > < |
Types of COBOL words
COBOL words can be grouped into two major categories:
- User-defined words - names created by the programmer.
- Reserved words - words that already have a defined meaning in COBOL.
User-defined words
A user-defined word is a name chosen by the programmer. It may name a file, record, field, paragraph, section, or program.
Rules for user-defined words:
- Use only letters, digits, and hyphens.
- Do not use spaces.
- Do not begin or end with a hyphen.
- Include at least one letter.
- Keep the name within the allowed length; many COBOL references use a 30-character limit.
- Do not use a COBOL reserved word.
Examples:
| Name | Valid? | Reason |
|---|---|---|
SALES-AMT | Yes | Uses letters and hyphen correctly. |
3STUD-NAME | Yes in many COBOL systems | Begins with a digit but includes letters; check compiler rules. |
143 | No | Contains no alphabetic character. |
STUDENT ID | No | Contains a space. |
SALES*REP | No | Contains *, which is not allowed in user-defined names. |
AMOUNT-DUE- | No | Ends with a hyphen. |
Reserved words
A reserved word has a built-in COBOL meaning. Programmers cannot freely reuse it as a data name or file name.
Reserved words include:
- Keywords: Required words in a statement, such as
READ,AT END,STOP RUN,MOVE, andDISPLAY. - Optional words: Words that improve readability but may not be required by the compiler in some formats.
- Figurative constants: Reserved words representing common values, such as
ZERO,ZEROS,ZEROES,SPACE,SPACES,QUOTE, andQUOTES.
Example:
READ MASTER-FILE AT END
MOVE 1 TO EOF-SW.
In this example, READ, AT END, and MOVE are COBOL words. MASTER-FILE and EOF-SW are user-defined names.
Figurative constants
Figurative constants are readable substitutes for common values.
| Constant | Meaning |
|---|---|
ZERO, ZEROS, ZEROES | Numeric zero or repeated zeroes. |
SPACE, SPACES | One or more blank spaces. |
QUOTE, QUOTES | Quotation mark character. |
HIGH-VALUE, HIGH-VALUES | Highest value in the system's collating sequence. |
LOW-VALUE, LOW-VALUES | Lowest value in the system's collating sequence. |
Example:
MOVE ZEROS TO TOTAL-SALES.
MOVE SPACES TO CUSTOMER-NAME.
Average-grade case study
A simple COBOL exercise can ask the program to:
- Accept a student's name.
- Accept a midterm grade.
- Accept a final grade.
- Compute the average.
- Display the average.
- Ask whether another record should be entered.
A generic screen layout might be:
ENTER NAME: __________
ENTER MIDTERM GRADE: __________
ENTER FINAL GRADE: __________
AVERAGE: __________
INPUT MORE? (Y/N): __________