COBOL Basics

2.1 Coding Areas

If you look at the COBOL coding in later sections (e.g. League Table program in the Sample code section) the specific positions of coding elements are important for the compiler to understand. Essentially, the first 6 spaces are ignored by the compiler and are usually used by the programmer for line numbers. These numbers are not the same as those in BASIC where the line number is used as part of the logic (e.g. GOTO 280, sending the logic to line 280).

The seventh position is called the continuation area. Only certain characters ever appear here, these being:

* (asterisk), / (solidus or forward slash), or - (hyphen). The asterisk is used to precede a comment, i.e. all that follows is ignored by the compiler. The solidus is used to indicate a page break when printing coding from the compiler, but it too can be used as comment since the rest of the line is ignored by the compiler. The hyphen is used as a continuation marker, i.e. when a quoted literal needs to be extended over to the next line. It is not for continuing a statement onto the next line (this is unnecessary*) and also cannot be used to continue a COBOL word. (*You can write any COBOL statement over as many lines as you like, so long as you stay in the correct coding region and don't split strings.)


     000200*Here is a comment.
     000210/A new line for printing and a comment.
     000220
       :
     000340       DISPLAY 'This might be a very long string that
     000350-    'needs to be continued onto the next line'

Positions 8 to 11 and 12 to 72 are called area A and area B, respectively. These are used in specific instances that will be detailed in later sections.

Summary
PositionsDesignation
 
         1 to 6

              7  

        8 to 11  

       12 to 72

line code

continuation area

area A

area B

      2.2 Syntax

Identifier names

User-defined names must conform to the following rules:

  • Must only consist of alphabetic and numeric characters and/or hyphens
  • The name must contain at least one alphabetic character
  • Must be no more than 30 characters
  • When using hyphens, they must not appear at the beginning or end of the name
Some examples of legal names:

A123
RecordCount-1
WAGE-IN
Tot-2-out

Like all COBOL code, the compiler will not distinguish between upper and lower case letters (except within quotes).

Lastly, COBOL has a large list of reserved words that cannot be used as identifier names. A list of COBOL reserved words is given elsewhere.  

Punctuation

The full stop (period) is the most important punctuation mark used, and its use will be detailed later (see Scope terminators). Generally, every line of the IDENTIFICATION, ENVIRONMENT, and DATA DIVISION end in a period.

Quotation marks, either single or double, are used to surround quoted literals (and when calling a sub-program). However, donât mix them when surrounding the literal, e.g.

" This is bad â

" but this is ok "

Commas and semi-colons are also used to separate lists of identifiers, e.g.
MOVE 2 TO DATA-ITEM-1, DATA-ITEM-2, DATA-ITEM-3
A space must follow the comma/semi-colon. They are optional however, and a space would suffice, but it does add to clarity.

 

Spelling

Since COBOL was developed in the USA, the spelling of words is American, e.g. INITIALIZE or ORGANIZATION (using Z rather than S). Brits be warned!

In many cases, abbreviations and alternative spellings are available (see reserved word list), e.g. ZERO ZEROS ZEROES all mean the same thing. Likewise, LINE and LINES, PICTURE and PIC, THROUGH and THRU.

      2.3 The 'Hello World' Program

As is traditional for all introductory lessons for a programming language, here's a 'Hello World' program: