MainframeMaster

COBOL Tutorial

COBOL Basics

Progress0 of 0 lessons

COBOL Character Set

The COBOL character set consists of the following characters:

Letters

  • Uppercase A through Z
  • Lowercase a through z (treated the same as uppercase)

Digits

  • 0 through 9

Special Characters

  • Space ( )
  • Plus sign (+)
  • Minus sign or hyphen (-)
  • Asterisk (*)
  • Forward slash (/)
  • Dollar sign ($)
  • Comma (,)
  • Semicolon (;)
  • Period (.)
  • Quotation mark (")
  • Left and right parentheses ()
  • Greater than and less than signs (>, <)
  • Colon (:)
  • Equal sign (=)

All COBOL programs must be written using only these characters. Most modern COBOL compilers also support additional characters, but for maximum portability, it's best to stick with the standard character set.

COBOL Coding Format (Area A, Area B)

Traditionally, COBOL used a fixed column format with specific areas for different parts of the code. While modern COBOL can use free format, understanding the traditional format is important for maintaining legacy code.

COBOL Line Format

ColumnsAreaUsage
1-6Sequence Number AreaOptional numbering (historically used for card decks)
7Indicator AreaSpecial characters: * (comment), - (continuation), / (form feed), D (debugging)
8-11Area ADivision names, section names, paragraph names, level numbers 01 and 77, FD, SD
12-72Area BStatements, clauses, level numbers 02-49, qualifying names
73-80Identification AreaOptional program identification (historically used)

Area A Items

  • Division headers (IDENTIFICATION DIVISION, etc.)
  • Section headers (FILE SECTION, etc.)
  • Paragraph names
  • Level numbers 01 and 77
  • FD, SD entries
  • SELECT statements

Area B Items

  • COBOL statements
  • Clauses
  • Level numbers 02-49
  • Continuation of statements
  • Qualifying names
  • Entries subordinate to 01 or 77 entries
cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
000100 IDENTIFICATION DIVISION. 000200 PROGRAM-ID. EXAMPLE. 000300 ENVIRONMENT DIVISION. 000400 DATA DIVISION. 000500 WORKING-STORAGE SECTION. 000600 01 CUSTOMER-RECORD. 000700 05 CUSTOMER-ID PIC 9(5). 000800 05 CUSTOMER-NAME PIC X(20). 000900 05 CUSTOMER-ADDRESS. 001000 10 STREET PIC X(20). 001100 10 CITY PIC X(15). 001200 10 STATE PIC XX. 001300 10 ZIP-CODE PIC 9(5). 001400 PROCEDURE DIVISION. 001500 DISPLAY "COBOL FORMAT EXAMPLE". 001600 STOP RUN.

In this example, notice how divisions, sections, and level 01 items start in Area A (column 8), while subordinate items are indented into Area B (starting at column 12).

Free Format COBOL

Modern COBOL (2002 and later) also supports free format coding, which removes column restrictions:

cobol
1
2
3
4
5
6
7
8
9
10
11
identification division. program-id. FreeFormat. environment division. data division. working-storage section. 01 customer-record. 05 customer-id pic 9(5). 05 customer-name pic x(20). procedure division. display "Free format COBOL example". stop run.

COBOL Reserved Words and User-Defined Names

COBOL has a set of reserved words that have special meaning to the compiler and cannot be used as user-defined names.

Reserved Words

Reserved words include:

  • Keywords for divisions, sections, and paragraphs (IDENTIFICATION, PROCEDURE, etc.)
  • Verbs (MOVE, COMPUTE, DISPLAY, etc.)
  • Special registers (ADDRESS OF, LENGTH OF, etc.)
  • Figurative constants (ZERO, SPACE, HIGH-VALUE, etc.)

There are over 500 reserved words in modern COBOL.

User-Defined Names Rules

  • Must begin with a letter (A-Z, a-z), hyphen (-), or underscore (_)
  • Can contain letters, digits, hyphens, and underscores
  • Cannot end with a hyphen
  • Cannot contain spaces
  • Maximum length is typically 30 characters (compiler-dependent)
  • Cannot be a COBOL reserved word
  • Must be unique within their scope

Naming Conventions

  • Use hyphens to separate words (CUSTOMER-NAME instead of CustomerName)
  • Use meaningful, descriptive names
  • Indicate the type of data in the name (DATE-FIELD, AMOUNT-TOTAL)
  • Consistent capitalization (traditionally uppercase)
  • Use standardized prefixes for common types (WS- for working storage, FD- for file descriptors)

COBOL Statement Structure

COBOL statements follow a structured format that makes the language readable and English-like.

Elements of COBOL Statements

  • Verbs: Action words that begin statements (MOVE, COMPUTE, DISPLAY)
  • Operands: Data items or literals that the verb acts upon
  • Clauses: Combinations of keywords and operands
  • Phrases: Groups of clauses
  • Sentences: One or more statements ending with a period
  • Paragraphs: Named groups of sentences

Statement Examples

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
* Simple statement: MOVE CUSTOMER-NAME TO OUTPUT-NAME. * Compound statement: ADD ITEM-PRICE, TAX-AMOUNT GIVING TOTAL-PRICE ROUNDED. * Conditional statement: IF BALANCE IS GREATER THAN 1000 DISPLAY "Premium Customer" ELSE DISPLAY "Regular Customer" END-IF. * Performing a paragraph: PERFORM CALCULATE-TOTALS.

All COBOL statements are terminated by either a period, or in modern COBOL, by scope terminators like END-IF, END-PERFORM, etc. However, excessive use of periods can lead to logic errors, so modern COBOL programming style minimizes their use except where required by the language.

Comments and Continuation Lines in COBOL

Comments

There are several ways to add comments in COBOL:

  • Comment lines: Place an asterisk (*) in column 7
  • Comment paragraph: Place the word REMARKS in Area A followed by comment text
  • Inline comments: Use a comment entry starting with a slash and asterisk (/*)
  • Floating comment: Place the slash and asterisk at the beginning of a statement
cobol
1
2
3
4
5
6
7
8
* This is a comment line (asterisk in column 7) / This is also a comment line (slash in column 7) MOVE AMOUNT TO TOTAL-FIELD *> This is an inline comment *> This is a modern floating comment style DISPLAY "Result: " TOTAL-FIELD.

Continuation Lines

When a COBOL statement is too long for one line, it can be continued on the next line:

  1. Break the line at a logical point (like after a comma or space)
  2. Place a hyphen (-) in column 7 of the continuation line
  3. Continue the statement in Area B (column 12+)
cobol
1
2
3
4
5
6
7
8
9
000100 MOVE "This is a very long literal string that needs to be " 000200- "continued on the next line" TO LONG-MESSAGE. 000300 000400 IF CUSTOMER-STATUS = "A" AND ACCOUNT-BALANCE > 1000 AND 000500- CREDIT-RATING = "GOOD" 000600 PERFORM PREFERRED-CUSTOMER-ROUTINE 000700 ELSE 000800 PERFORM REGULAR-CUSTOMER-ROUTINE 000900 END-IF.

Well-commented COBOL code is essential for maintainability, especially since COBOL programs often have long lifespans and may be maintained by different programmers over decades.

Exercise: COBOL Format Identification

Look at the following COBOL code and identify formatting issues:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
IDENTIFICATION DIVISION. PROGRAM-ID. BADFORMAT. ENVIRONMENT DIVISION. DATA DIVISION. WORKING-STORAGE SECTION. 01 TOTAL-AMOUNT PIC 9(5)V99. 01 QUANTITY PIC 999. 01 price pic 999v99. PROCEDURE DIVISION. DISPLAY "Enter quantity: " ACCEPT QUANTITY DISPLAY "Enter unit price: " ACCEPT price MULTIPLY QUANTITY BY price GIVING TOTAL-AMOUNT DISPLAY "Total amount: $" TOTAL-AMOUNT STOP RUN.

Identify at least 5 formatting issues in the code above according to standard COBOL conventions.

FAQ

Why does COBOL use such a rigid column structure?

COBOL's column structure dates back to its creation in the era of punch cards, where each card had 80 columns. The structure was designed to make programs readable and to accommodate the technology of the time. Modern COBOL allows free-format coding, but the traditional format is still used in legacy code and for readability.

What's the difference between fixed format and free format COBOL?

Fixed format COBOL requires specific elements to appear in certain columns (Area A, Area B, etc.), while free format COBOL removes these restrictions. Free format was introduced in the COBOL 2002 standard, allowing more flexible coding styles similar to modern programming languages. Free format is indicated by >> SOURCE FORMAT IS FREE in the program or as a compiler option.

Why are COBOL terms written in ALL CAPS?

COBOL terms were traditionally written in uppercase because early computers and terminals only supported uppercase letters. While modern COBOL is case-insensitive and can be written in lowercase, uppercase is still commonly used for reserved words and user-defined names by convention, making it easier to distinguish between language elements and improving readability.

How do I know which words are reserved in COBOL?

Each COBOL compiler provides a list of reserved words in its documentation. The list varies slightly between compiler implementations, but the core reserved words are consistent. Most modern COBOL editors and IDEs provide syntax highlighting that identifies reserved words. When in doubt, choose a different name for your variables and procedures to avoid potential conflicts.