COBOL Basics 1
COBOL coding
rules
Almost all COBOL compilers treat a line of COBOL code
as if it contained two distinct areas. These are known as;
Area A and Area B
When a COBOL compiler recognizes these two areas, all
division, section, paragraph names, FD entries and 01
level numbers must start in Area A. All other sentences
must start in Area B.
Area A is four characters wide and is followed by Area B.
In Microfocus COBOL the compiler directive
$ SET SOURCEFORMAT"FREE" frees us from all formatting
restrictions.
COBOL coding
rules
Almost all COBOL compilers treat a line of COBOL code
as if it contained two distinct areas. These are known as;
Area A and Area B
When a COBOL compiler recognizes these two areas, all
division, section, paragraph names, FD entries and 01
level numbers must start in Area A. All other sentences
must start in Area B.
Area A is four characters wide and is followed by Area B.
In Microfocus COBOL the compiler directive
$ SET SOURCEFORMAT"FREE" frees us from all formatting
$$ SET
SET SOURCEFORMAT"FREE"
restrictions. IDENTIFICATION
SOURCEFORMAT"FREE"
IDENTIFICATION DIVISION.
DIVISION.
PROGRAM-ID. ProgramFragment.
PROGRAM-ID. ProgramFragment.
** This
This is
is aa comment.
comment. It
It starts
starts
** with
with an asterisk in column 11
an asterisk in column
Name
Construction.
All user defined names, such as data names, paragraph
names, section names and mnemonic names, must adhere
to the following rules;
They must contain at least one character and not more than
30 characters.
They must contain at least one alphabetic character and they
must not begin or end with a hyphen.
They must be contructed from the characters A to Z, the
number 0 to 9 and the hyphen.
e.g. TotalPay, Gross-Pay, PrintReportHeadings,
Customer10-Rec
All data-names should describe the data they contain.
All paragraph and section names should describe the
function of the paragraph or section.
Describing
DATA.
There are basically three kinds of data used in COBOL
programs;
ŒVariables.
Literals.
ŽFigurative Constants.
Unlike other programming languages, COBOL does not
support user defined constants.
Data-Names /
Variables
A variable is a named location in memory into which a
program can put data and from which it can retrieve
data.
A data-name or identifier is the name used to identify
the area of memory reserved for the variable.
Variables must be described in terms of their type and
size.
Every variable used in a COBOL program must have a
description in the DATA DIVISION.
Using
Variables
01 StudentName PIC X(6) VALUE SPACES.
MOVE "JOHN" TO StudentName.
DISPLAY "My name is ", StudentName.
StudentName
Using
Variables
01 StudentName PIC X(6) VALUE SPACES.
MOVE "JOHN" TO StudentName.
DISPLAY "My name is ", StudentName.
StudentName
J O H
N
Using
Variables
01 StudentName PIC X(6) VALUE SPACES.
MOVE "JOHN" TO StudentName.
DISPLAY "My name is ", StudentName.
StudentName My name is
JOHN
J O H N
COBOL Data
Types
COBOL is not a “typed” language and the distinction
between some of the data types available in the
language is a little blurred.
For the time being we will focus on just two data types,
numeric
text or string
Data type is important because it determines the
operations which are valid on the type.
COBOL is not as rigorous in the application of typing
rules as other languages.
For example, some COBOL “numeric” data items may,
from time to time, have values which are not “numeric”!
Quick Review of “Data
Typing”
In “typed” languages simply specifying the type of a
data item provides quite a lot of information about it.
The type usually determines the range of values the data
item can store.
For instance a CARDINAL item can store values between
0..65,535 and an INTEGER between -32,768..32,767
From the type of the item the compiler can establish how
much memory to set aside for storing its values.
If the type is “REAL” the number of decimal places is
allowed to vary dynamically with each calculation but
the amount of the memory used to store a real number is
fixed.
COBOL data
description.
Because COBOL is not typed it employs a different
mechanism for describing the characteristics of the
data items in the program.
COBOL uses what could be described as a “declaration
by example” strategy.
In effect, the programmer provides the system with an
example, or template, or PICTURE of what the data item
looks like.
From the “picture” the system derives the information
necessary to allocate it.
COBOL ‘PICTURE’ Clause
symbols
To create the required ‘picture’ the programmer uses a
set of symbols.
The following symbols are used frequently in picture
clauses;
9 (the digit nine) is used to indicate the occurrence of a
digit at the corresponding position in the picture.
X (the character X) is used to indicate the occurrence of
any character from the character set at the corresponding
position in the picture
V (the character V) is used to indicate position of the
decimal point in a numeric value! It is often referred to as
the “assumed decimal point” character.
S (the character S) indicates the presence of a sign and can
only appear at the beginning of a picture.
COBOL ‘PICTURE’
Clauses
Some examples
PICTURE 999 a three digit (+ive only) integer
PICTURE S999 a three digit (+ive/-ive) integer
PICTURE XXXX a four character text item or string
PICTURE 99V99 a +ive ‘real’ in the range 0 to 99.99
PICTURE S9V9 a +ive/-ive ‘real’ in the range ?
If you wish you can use the abbreviation PIC.
Numeric values can have a maximum of 18 (eighteen)
digits (i.e. 9’s).
The limit on string values is usually system-dependent.
Abbreviating recurring
symbols
Recurring symbols can be specified using a ‘repeat’
factor inside round brackets
PIC 9(6) is equivalent to PICTURE 999999
PIC 9(6)V99 is equivalent to PIC 999999V99
PICTURE X(10) is equivalent to PIC XXXXXXXXXX
PIC S9(4)V9(4) is equivalent to PIC S9999V9999
PIC 9(18) is equivalent to PIC 999999999999999999
Declaring DATA in
COBOL
In COBOL a variable declaration consists of a line
containing the following items;
ŒA level number.
A data-name or identifier.
ŽA PICTURE clause.
We can give a starting value to variables by means of an
extension to the picture clause called the value clause.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Num1 PIC 999 VALUE ZEROS.
01 VatRate PIC V99 VALUE .18.
01 StudentName PIC X(10) VALUE SPACES.
DATA
Num1
Num1 VatRate
VatRate StudentName
StudentName
000
000 .18
.18
COBOL
Literals.
String/Alphanumeric literals are enclosed in quotes and
may consists of alphanumeric characters
e.g. "Michael Ryan", "-123", "123.45"
Numeric literals may consist of numerals, the decimal
point and the plus or minus sign. Numeric literals are
not enclosed in quotes.
e.g. 123, 123.45, -256, +2987
Figurative
Constants
COBOL provides its own, special constants called
Figurative Constants.
SPACE
SPACEor
orSPACES
SPACES ==
ZERO
ZEROor
orZEROS
ZEROSor
orZEROS
ZEROS == 00
QUOTE
QUOTEororQUOTES
QUOTES == ""
HIGH-VALUE
HIGH-VALUEor
orHIGH-VALUES
HIGH-VALUES == Max
MaxValue
Value
LOW-VALUE
LOW-VALUEor orLOW-VALUES
LOW-VALUES == Min
MinValue
Value
ALL
ALLliteral
literal == Fill
FillWith
WithLiteral
Literal
Figurative Constants -
Examples
01
01 GrossPay
GrossPay PICPIC 9(5)V99
9(5)V99 VALUE
VALUE 13.5.
13.5.
ZERO
MOVE ZEROS
MOVE
ZEROES
TO
TO GrossPay.
GrossPay.
GrossPay
0 0 0 1 3 5
0
01
01 StudentName
StudentName PIC
PIC X(10)
X(10) VALUE
VALUE "MIKE".
"MIKE".
MOVE
MOVE ALL
ALL "-"
"-" TO
TO StudentName.
StudentName.
StudentName
M I K E
Figurative Constants -
Examples
01
01 GrossPay
GrossPay PICPIC 9(5)V99
9(5)V99 VALUE
VALUE 13.5.
13.5.
ZERO
MOVE ZEROS
MOVE
ZEROES
TO
TO GrossPay.
GrossPay.
GrossPay
0 0 0 0 0 0
0
01
01 StudentName
StudentName PIC
PIC X(10)
X(10) VALUE
VALUE "MIKE".
"MIKE".
MOVE
MOVE ALL
ALL "-"
"-" TO
TO StudentName.
StudentName.
StudentName
- - - - - - - - - -
Editing, Checking, Compiling,
Running
PROCO
Menus
PROCO 1 - Menu ALT key PROCO 2 - Alt Menu
PROCO 1 - Menu PROCO 2 - Alt Menu
F1=help F2=edit F3=check F4=animate F1=help F2=screens F7=link F10=CoWriter
F1=help F2=edit F3=check F4=animate F1=help F2=screens F7=link F10=CoWriter
F5=compile F6=run F7=library F8=build
F5=compile F6=run F7=library F8=build
F10=directory CTRL key PROCO 3 - Ctrl Menu
F10=directory PROCO 3 - Ctrl Menu
F1=help F3=update-menu F4=UseUpdatedMenu
F1=help F3=update-menu F4=UseUpdatedMenu
F5=batch-files F7=OS-command F8=config
F5=batch-files F7=OS-command F8=config
F10=user-menu
F10=user-menu
EDIT 1 -Menu ALT key EDIT
EDIT22- -Alt
AltMenu
Menu
EDIT 1 -Menu
F1=help F2=COBOL F3=InsertLine F1=help F2=library F3=load-file F4=save-file
F1=help F2=COBOL F3=InsertLine F1=help F2=library F3=load-file F4=save-file
F4=DeleteLine F5=RepeatLine F6=RestoreLine F5=split-line F6=join-line F7=print
F4=DeleteLine F5=RepeatLine F6=RestoreLine F5=split-line F6=join-line F7=print
F7=RetypeChar F8=RestoreChar F9=WordLeft F8=calculate
F8=calculate F9=untype-word-left
F9=untype-word-left
F7=RetypeChar F8=RestoreChar F9=WordLeft
F10=WordRight F10=DeleteWord
F10=WordRight F10=DeleteWord
CTRL key EDIT 3 - Ctrl Menu
EDIT 3 - Ctrl Menu
F1=help F2=find F3=block F4=clear
F1=help F2=find F3=block F4=clear
F5=margins
F5=marginsF6=draw/forms
F6=draw/formsF7=tags
F7=tags
COBOL menu
COBOL menu F8=WordWrap F9=window F10=scroll
F1=help F8=WordWrap
(move in window) F9=window F10=scroll
F1=help F2=check/animateF3=cmd-file
F2=check/animate F3=cmd-file <-/-> Home/End (of text) PgUp/PgDn
<-/-> (move in window) Home/End (of text) PgUp/PgDn
F7=locate-previous F8=locate-next
F7=locate-previous F8=locate-next
F9=locate-current
F9=locate-current
Animate-Menu
Animate-Menu
F1=help F2=view F3=align F4=exchange
F1=help F2=view F3=align F4=exchange
Checker Menu F5=where F6=look-up F9/F10=word-</>
Checker Menu F5=where
Escape F6=look-up
Animate Step Wch Go ZoomF9/F10=word-</>
nx-If Prfm Rst Brk Env
F1=help
F1=help F2=check/animF3=pause
F2=check/anim F3=pause Escape AnimateQury
Step Find
Wch Locate
Go Zoom
Txtnx-If
Do Prfm Rst Brk Env
F4=list F6=lang F7=ref F9/F10=directives Qury Find Locate Txt Do
F4=list F6=lang F7=ref F9/F10=directives