You are on page 1of 5

Structure of COBOL programs

COBOL programs are hierarchical in structure. Each element of the hierarchy consists of one or more
subordinate elements.
The hierarchy consists of Divisions, Sections, Paragraphs, Sentences and Statements.
A Division may contain one or more Sections, a Section one or more Paragraphs, a Paragraph one or
more Sentences and a Sentence one or more Statements.
We can represent the COBOL hierarchy using the COBOL metalanguage as follows;

Divisions
A division is a block of code, usually containing one or more sections, that starts where the division name
is encountered and ends with the beginning of the next division or with the end of the program text.

Sections
A section is a block of code usually containing one or more paragraphs. A section begins with the section
name and ends where the next section name is encountered or where the program text ends.
Section names are devised by the programmer, or defined by the language. A section name is followed
by the word SECTION and a period.
See the two example names below -
SelectUnpaidBills SECTION.
FILE SECTION.
Paragraphs
A paragraph is a block of code made up of one or more sentences. A paragraph begins with the
paragraph name and ends with the next paragraph or section name or the end of the program text.
A paragraph name is devised by the programmer or defined by the language, and is followed by a period.
See the two example names below -
PrintFinalTotals.
PROGRAM-ID.
Sentences and statements
A sentence consists of one or more statements and is terminated by a period.
For example:
MOVE .21 TO VatRate
MOVE 1235.76 TO ProductCost
COMPUTE VatAmount = ProductCost * VatRate.
A statement consists of a COBOL verb and an operand or operands.
For example:
SUBTRACT Tax FROM GrossPay GIVING NetPay.
COBOL Character Set

The COBOL character set, shown in Table is used to form character-strings and separators.
The COBOL Character Set
Character Meaning
0, 1, ..., 9 digit
A, B, ..., Z letter
a, b, ..., z lowercase letter (equivalent to letter)
+ plus sign
- minus sign (hyphen)
* Asterisk
/ slash (stroke, virgule)
\ backslash
= equal sign
$ currency sign
> greater than symbol
< less than symbol
: colon
_ underline (underscore)
space
[Tab] horizontal tab
( left parenthesis
) right parenthesis
, comma (decimal point)
; semicolon
. period (decimal point, full stop)
" quotation mark (double quotation mark)
' apostrophe (single quotation mark)
{ left brace
} right brace
[ left bracket
] right bracket
« double left-angle brackets
» double right-angle brackets
COBOL Words
A character-string is a character or sequences of contiguous characters that form a COBOL word, a
literal, a PICTURE character-string, or a comment-entry, Separators delimit character-strings.
A COBOL word is a character-string of not more than 30 characters that forms one of the following:
• A user-defined word
• A system-name
• A reserved word
• A function-name
A word must conform to the following rules
1. The total number of characters must not be greater than 30
2. One of the characters must be a letter. Some compilers put the additional restriction that the first
character must be a letter
3. A word cannot begin or end with a Hypen.
4. A word must not contain a blank and any special characters except a Hypen
Example
Valid word Invalid word
GROSS-PAY -GROSS-(as it starts and ends with a hypen)
OVERTIME-HOURS OVERTIME HOUR(Blank space)

DATA-NAME OR IDENTIFIER
There are three categories of data item used in COBOL programs:
o Variables.
o Literals.
o Figurative Constant.

Variables

A data-name or identifier is the name used to identify the area of memory reserved for a variable. A
variable is a named location in memory into which a program can put data, and from which it can retrieve
data.
Every variable used in a COBOL program must be described in the DATA DIVISION.
In addition to the data-name, a variable declaration also defines the type of data to be stored in the
variable. This is known as the variable's data type.
DATA TYPE
Some languages like Modula-2, Pascal or Ada are described as being strongly typed. In these languages
there are a large number of different data types and the distinction between them is rigorously enforced
by the compiler. For instance, the compiler will reject a statement that attempts to assign character value
to an integer data item.

In COBOL, there are really only three data types -


• numeric
• alphanumeric (text/string)
• alphabetic
The distinction between these data types is a little blurred and only weakly enforced by the compiler. For
instance, it is perfectly possible to assign a non-numeric value to a data item that has been declared to be
numeric.
The problem with this lax approach to data typing is that, since COBOL programs crash (halt
unexpectedly) if they attempt to do computations on items that contain non-numeric data, it is up to the
programmer to make sure this never happens.
COBOL programmers must make sure that non-numeric data is never assigned to numeric items
intended for use in calculations. Programmers who use strongly typed languages don't need this level of
discipline because the compiler ensures that a variable of particular types can only be assigned
appropriate values.

Literals
A literal is a data-item that consists only of the data-item value itself. It cannot be referred to by a name.
By definition, literals are constant data-items.
There are two types of literal -
• String/Alphanumeric Literals
• Numeric Literals
String Literals
String/Alphanumeric literals are enclosed in quotes and consist of alphanumeric characters.
For example: "Michael Ryan", "-123", "123.45"
Syntax Rules
1. A space, left parenthesis, or pseudo-text delimiter (==) must immediately precede the opening
quotation mark (or apostrophe).
2. The closing quotation mark (or apostrophe) must be immediately followed by one of the following:
o Space
o Comma
o Semicolon
o Period
o Right parenthesis
o Pseudo-text delimiter
3. If a nonnumeric literal is delimited by quotation marks ("), two consecutive quotation mark characters in
the literal represent one quotation mark character.
4. If a nonnumeric literal is delimited by apostrophes ('), two consecutive apostrophes in the literal
represent one apostrophe (').

Numeric Literals
Numeric literals may consist of numerals, the decimal point, and the plus or minus sign. Numeric literals
are not enclosed in quotes.
For example: 123, 123.45, -256, +2987
Syntax Rules
1. A numeric literal must contain at least 1 digit and not more than 31 digits.
2. A numeric literal must not contain more than one sign character, which must be the leftmost character.
If the literal is unsigned, its value is positive.
3. A numeric literal must not contain more than one decimal point. The decimal point is treated as an
assumed decimal point. It can be used anywhere in the literal except as the rightmost character.
If a numeric literal contains no decimal point, it is an integer.
4. The compiler treats a numeric literal enclosed in quotation marks as a nonnumeric literal.

Figurative Constants

Actually COBOL does allow you to set up single character user-defined Figurative Constants. These can
be useful if you need to use the non-printable ASCII characters such as ESC or Form Feed.
User-defined Figurative Constants are declared in the SYMBOLIC CHARACTERS clause of the
ENVIRONMENT DIVISION.
In an extension that previews the new COBOL specification, Net Express does allow user-defined
constants. It uses the level 78 for this purpose.
Unlike most other programming languages COBOL does not provide a mechanism for creating user-
defined constants but it does provide a set of special constants called Figurative Constants.
A Figurative Constant may be used wherever it is legal to use a literal but unlike literals, when a
Figurative Constant is assigned to a data-item it fills the whole item overwriting everything in it.
The Figurative Constants are:
SPACE or SPACES Acts like one or more spaces
ZERO or ZEROS or ZEROES Acts like one or more zeros
QUOTE or QUOTES Used instead of a quotation mark
HIGH-VALUE or HIGH-VALUES Uses the maximum value possible
LOW-VALUE or LOW-VALUES Uses the minimum value possible
ALL literal Allows a ordinary literal to act as Figurative Constant
Figurative Constant Notes
• When the ALL Figurative Constant is used, it must be followed by a one character literal. The
designated literal then acts like the standard Figurative Constants.
• ZERO, ZEROS and ZEROES are synonyms, not separate Figurative Constants. The same applies to
SPACE and SPACES, QUOTE and QUOTES, HIGH-VALUE and HIGH-VALUES, LOW-VALUES and
LOW-VALUES

You might also like