Sie sind auf Seite 1von 46

IF Syntax.

StatementBlock

IF Condition THEN
NEXT
SENTENCE

StatementBlock

END - IF
ELSE
NEXT
SENTENCE

CONDITION TYPES

Simple
SimpleConditions
Conditions

Relation
RelationConditions
Conditions
Class
ClassConditions
Conditions
Sign
SignConditions
Conditions

Complex
ComplexConditions
Conditions

Condition
ConditionNames
Names

09/03/16

ER/CORP/CRS/LA01

Relation Conditions
NOT GREATER THAN

NOT >

NOT
NOT
NOT
NOT

Identifier

Literal
IS
ArithmeticExpression

LESS THAN

<

EQUAL TO

Identifier

Literal

ArithmeticExpression

GREATER THAN OR EQUAL TO


>=

LESS
THAN
OR
EQUAL
TO

<=

09/03/16

ER/CORP/CRS/LA01

Class Conditions.
NUMERIC

ALPHABETIC

ALPHABETIC - LOWER
Identifier IS [ NOT]

ALPHABETIC - UPPER

Although COBOL data items are not typed they do fall into
some broad categories, or classes, such a numeric or
alphanumeric, etc.

A Class Condition determines whether the value of data item is


a member of one these classes.

09/03/16

ER/CORP/CRS/LA01

Sign Conditions
POSITIVE

ArithExp IS [NOT] NEGATIVE


ZERO

The sign condition determines whether or not the value of an


arithmetic expression is less than, greater than or equal to
zero.

Sign conditions are just another way of writing some of the


Relational conditions.

09/03/16

ER/CORP/CRS/LA01

Complex conditions.

AND

Condition
Condition
OR

Programs often require conditions which are more complex


than single value testing or determining a data class.

Like all other programming languages COBOL allows simple


conditions to be combined using OR and AND to form
composite conditions.

Like other conditions, a complex condition evaluates to true or


false.

A complex condition is an expression which is evaluated from


left to right unless the order of evaluation is changed by the
precedence rules or bracketing.
09/03/16

ER/CORP/CRS/LA01

Nested IFs
IF
IF (( VarA
VarA << 10
10 )) AND
AND (( VarB
VarB NOT
NOT >> VarC
VarC )) THEN
THEN
IF
IF VarG
VarG == 14
14 THEN
THEN
DISPLAY
DISPLAY First
First
ELSE
ELSE
DISPLAY
DISPLAY Second
Second
END-IF
END-IF
ELSE
ELSE
DISPLAY
DISPLAY Third
Third
END-IF
END-IF

VarA
VarA VarB
VarB VarC
VarC VarG
VarG
33 T
33 T
33 T
F
13
13
09/03/16

44 T
44 T
44 F
44 T

DISPLAY
DISPLAY

First
15
14
15
14 T
F
Second
15
15
15
15
Third
33
14
14
Third
15
14
15
14
ER/CORP/CRS/LA01

Condition Names.
IFVarA
GREATER
THAN
VarB
THEN
Action

Condition is either
TRUE or FALSE

Wherever a condition can occur, such as in an IF statement or


an EVALUATE or a PERFORM..UNTIL, a CONDITION
NAME (Level 88) may be used.
A Condition Name is essentially a BOOLEAN variable which
is either TRUE or FALSE.
Example.
IF StudentRecord = HIGH-VALUES THEN Action
The statement above may be replaced by the one below. The condition
name EndOfStudentFile may be used instead of the condition
StudentRecord = HIGH-VALUES.

IF EndOfStudentFile THEN Action

09/03/16

ER/CORP/CRS/LA01

Defining Condition Names.


Literal

VALUE

88 ConditionName
THROUGH


Literal2
VALUES Literal1
THRU

Condition Names are defined in the DATA DIVISION using the special
level number 88.

They are always associated with a data item and are defined immediately
after the definition of the data item.

A condition name takes the value TRUE or FALSE depending on the value
in its associated data item.

A Condition Name may be associated with ANY data item whether it is a


group or an elementary item.

The VALUE clause is used to identify the values which make the Condition
Name TRUE.

09/03/16

ER/CORP/CRS/LA01

01
01 CityCode
CityCode
88
88 New-York
New-York
88
88 Limerick
Limerick
88
88 Cork
Cork
88
88 Galway
Galway
88
88 Sligo
Sligo
88
88 Waterford
Waterford
88
88 UniversityCity
UniversityCity

PIC
PIC 99
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE

IF
IF Limerick
Limerick
DISPLAY
DISPLAY "Hey,
"Hey, we're
we're home."
home."
END-IF
END-IF
IF
IF UniversityCity
UniversityCity
PERFORM
PERFORM CalcRentSurcharge
CalcRentSurcharge
END-IF
END-IF

09/03/16

VALUE
VALUE 5.
5.
1.
1.
2.
2.
3.
3.
4.
4.
5.
5.
6.
6.
11 THRU
THRU 4.
4.
City Code

6
Dublin
Limerick
Cork
Galway
Sligo
Waterford
UniversityCity

ER/CORP/CRS/LA01

FALSE
FALSE
FALSE
FALSE

FALSE
TRUE
FALSE

01
01 CityCode
CityCode
88
88 Dublin
Dublin
88
88 Limerick
Limerick
88
88 Cork
Cork
88
88 Galway
Galway
88
88 Sligo
Sligo
88
88 Waterford
Waterford
88
88 UniversityCity
UniversityCity

PIC
PIC 99
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE

IF
IF Limerick
Limerick
DISPLAY
DISPLAY "Hey,
"Hey, we're
we're home."
home."
END-IF
END-IF
IF
IF UniversityCity
UniversityCity
PERFORM
PERFORM CalcRentSurcharge
CalcRentSurcharge
END-IF
END-IF

09/03/16

VALUE
VALUE 5.
5.
1.
1.
2.
2.
3.
3.
4.
4.
5.
5.
6.
6.
11 THRU
THRU 4.
4.
City Code

2
Dublin
Limerick
Cork
Galway
Sligo
Waterford
UniversityCity

ER/CORP/CRS/LA01

FALSE

TRUE

FALSE
FALSE
FALSE
FALSE

TRUE

10

01
01 CityCode
CityCode
88
88 New-York
New-York
88
88 Limerick
Limerick
88
88 Cork
Cork
88
88 Galway
Galway
88
88 Sligo
Sligo
88
88 Waterford
Waterford
88
88 UniversityCity
UniversityCity

PIC
PIC 99
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE

IF
IF New-York
New-York
DISPLAY
DISPLAY "Hey,
"Hey, we're
we're home."
home."
END-IF
END-IF
IF
IF UniversityCity
UniversityCity
PERFORM
PERFORM CalcRentSurcharge
CalcRentSurcharge
END-IF
END-IF

09/03/16

VALUE
VALUE 5.
5.
1.
1.
2.
2.
3.
3.
4.
4.
5.
5.
6.
6.
11 THRU
THRU 4.
4.
City Code

6
Dublin
Limerick
Cork
Galway
Sligo
Waterford
UniversityCity

ER/CORP/CRS/LA01

FALSE
FALSE
FALSE
FALSE
FALSE

TRUE

FALSE

11

01
PIC
01 InputChar
InputChar
PIC X.
X.
88
VALUE
88 Vowel
Vowel
VALUE "A","E","I","O","U".
"A","E","I","O","U".
88
88 Consonant
Consonant VALUE
VALUE "B"
"B" THRU
THRU "D",
"D", "F","G","H"
"F","G","H"
"J"
"J" THRU
THRU "N",
"N", "P"
"P" THRU
THRU "T"
"T"
"V"
"V" THRU
THRU "Z".
"Z".
88
VALUE
88 Digit
Digit
VALUE "0"
"0" THRU
THRU "9".
"9".
88
88 LowerCase
LowerCase VALUE
VALUE "a"
"a" THRU
THRU "z".
"z".
88
88 ValidChar
ValidChar VALUE
VALUE "A"
"A" THRU
THRU "Z","0"
"Z","0" THRU
THRU "9".
"9".
IF
IF ValidChar
ValidChar
DISPLAY
DISPLAY "Input
"Input OK."
OK."
END-IF
END-IF
IF
IF LowerCase
LowerCase
DISPLAY
DISPLAY "Not
"Not Upper
Upper Case"
Case"
END-IF
END-IF
IF
IF Vowel
Vowel
Display
Display "Vowel
"Vowel entered."
entered."
END-IF
END-IF

09/03/16

Input Char

E
Vowel
Consonant
Digit
LowerCase
ValidChar

ER/CORP/CRS/LA01

TRUE
FALSE
FALSE
FALSE

TRUE

12

01
PIC
01 InputChar
InputChar
PIC X.
X.
88
VALUE
88 Vowel
Vowel
VALUE "A","E","I","O","U".
"A","E","I","O","U".
88
88 Consonant
Consonant VALUE
VALUE "B"
"B" THRU
THRU "D",
"D", "F","G","H"
"F","G","H"
"J"
"J" THRU
THRU "N",
"N", "P"
"P" THRU
THRU "T"
"T"
"V"
"V" THRU
THRU "Z".
"Z".
88
VALUE
88 Digit
Digit
VALUE "0"
"0" THRU
THRU "9".
"9".
88
88 LowerCase
LowerCase VALUE
VALUE "a"
"a" THRU
THRU "z".
"z".
88
88 ValidChar
ValidChar VALUE
VALUE "A"
"A" THRU
THRU "Z","0"
"Z","0" THRU
THRU "9".
"9".
IF
IF ValidChar
ValidChar
DISPLAY
DISPLAY "Input
"Input OK."
OK."
END-IF
END-IF
IF
IF LowerCase
LowerCase
DISPLAY
DISPLAY "Not
"Not Upper
Upper Case"
Case"
END-IF
END-IF
IF
IF Vowel
Vowel
Display
Display "Vowel
"Vowel entered."
entered."
END-IF
END-IF

09/03/16

Input Char

4
Vowel
Consonant
Digit
LowerCase
ValidChar

ER/CORP/CRS/LA01

FALSE
FALSE

TRUE

FALSE

TRUE

13

01
PIC
01 InputChar
InputChar
PIC X.
X.
88
VALUE
88 Vowel
Vowel
VALUE "A","E","I","O","U".
"A","E","I","O","U".
88
88 Consonant
Consonant VALUE
VALUE "B"
"B" THRU
THRU "D",
"D", "F","G","H"
"F","G","H"
"J"
"J" THRU
THRU "N",
"N", "P"
"P" THRU
THRU "T"
"T"
"V"
"V" THRU
THRU "Z".
"Z".
88
VALUE
88 Digit
Digit
VALUE "0"
"0" THRU
THRU "9".
"9".
88
88 LowerCase
LowerCase VALUE
VALUE "a"
"a" THRU
THRU "z".
"z".
88
88 ValidChar
ValidChar VALUE
VALUE "A"
"A" THRU
THRU "Z","0"
"Z","0" THRU
THRU "9".
"9".
IF
IF ValidChar
ValidChar
DISPLAY
DISPLAY "Input
"Input OK."
OK."
END-IF
END-IF
IF
IF LowerCase
LowerCase
DISPLAY
DISPLAY "Not
"Not Upper
Upper Case"
Case"
END-IF
END-IF
IF
IF Vowel
Vowel
Display
Display "Vowel
"Vowel entered."
entered."
END-IF
END-IF

09/03/16

Input Char

g
Vowel
Consonant
Digit
LowerCase
ValidChar

ER/CORP/CRS/LA01

FALSE
FALSE
FALSE

TRUE

FALSE

14

01
01 EndOfFileFlag
EndOfFileFlag
88
88 EndOfFile
EndOfFile

PIC
PIC 99 VALUE
VALUE 0.
0.
VALUE
VALUE 1.
1.

EndOfFileFlag

0
EndOfFile

READ
READ InFile
InFile
AT
AT END
END MOVE
MOVE 11 TO
TO EndOfFileFlag
EndOfFileFlag
END-READ
END-READ
PERFORM
PERFORM UNTIL
UNTIL EndOfFile
EndOfFile
Statements
Statements
READ
READ InFile
InFile
AT
AT END
END MOVE
MOVE 11 TO
TO EndOfFileFlag
EndOfFileFlag
END-READ
END-READ
END-PERFORM
END-PERFORM
09/03/16

ER/CORP/CRS/LA01

15

01
01 EndOfFileFlag
EndOfFileFlag
88
88 EndOfFile
EndOfFile

PIC
PIC 99 VALUE
VALUE 0.
0.
VALUE
VALUE 1.
1.

EndOfFileFlag

1
EndOfFile

READ
READ InFile
InFile
AT
AT END
END MOVE
MOVE 11 TO
TO EndOfFileFlag
EndOfFileFlag
END-READ
END-READ
PERFORM
PERFORM UNTIL
UNTIL EndOfFile
EndOfFile
Statements
Statements
READ
READ InFile
InFile
AT
AT END
END MOVE
MOVE 11 TO
TO EndOfFileFlag
EndOfFileFlag
END-READ
END-READ
END-PERFORM
END-PERFORM
09/03/16

ER/CORP/CRS/LA01

16

Using the SET verb.


01
PIC
01 FILLER
FILLER
PIC 99 VALUE
VALUE 0.
0.
88
VALUE
88 EndOfFile
EndOfFile
VALUE 1.
1.
88
88 NotEndOfFile
NotEndOfFile VALUE
VALUE 0.
0.

FILLER

0
EndOfFile
1
NotEndOfFile 0

READ
READ InFile
InFile
AT
AT END
END SET
SET EndOfFile
EndOfFile TO
TO TRUE
TRUE
END-READ
END-READ
PERFORM
PERFORM UNTIL
UNTIL EndOfFile
EndOfFile
Statements
Statements
READ
READ InFile
InFile
AT
AT END
END SET
SET EndOfFile
EndOfFile TO
TO TRUE
TRUE
END-READ
END-READ
END-PERFORM
END-PERFORM
Set
TO
Set NotEndOfFile
NotEndOfFile
TO TRUE.
TRUE.
09/03/16
ER/CORP/CRS/LA01

17

Using the SET verb.


01
PIC
01 FILLER
FILLER
PIC 99 VALUE
VALUE 0.
0.
88
VALUE
88 EndOfFile
EndOfFile
VALUE 1.
1.
88
88 NotEndOfFile
NotEndOfFile VALUE
VALUE 0.
0.

FILLER

1
EndOfFile
1
NotEndOfFile 0

READ
READ InFile
InFile
AT
AT END
END SET
SET EndOfFile
EndOfFile TO
TO TRUE
TRUE
END-READ
END-READ
PERFORM
PERFORM UNTIL
UNTIL EndOfFile
EndOfFile
Statements
Statements
READ
READ InFile
InFile
AT
AT END
END SET
SET EndOfFile
EndOfFile TO
TO TRUE
TRUE
END-READ
END-READ
END-PERFORM
END-PERFORM
Set
TO
Set NotEndOfFile
NotEndOfFile
TO TRUE.
TRUE.
09/03/16
ER/CORP/CRS/LA01

18

Using the SET verb.


01
PIC
01 FILLER
FILLER
PIC 99 VALUE
VALUE 0.
0.
88
VALUE
88 EndOfFile
EndOfFile
VALUE 1.
1.
88
88 NotEndOfFile
NotEndOfFile VALUE
VALUE 0.
0.

FILLER

0
EndOfFile
1
NotEndOfFile 0

READ
READ InFile
InFile
AT
AT END
END SET
SET EndOfFile
EndOfFile TO
TO TRUE
TRUE
END-READ
END-READ
PERFORM
PERFORM UNTIL
UNTIL EndOfFile
EndOfFile
Statements
Statements
READ
READ InFile
InFile
AT
AT END
END SET
SET EndOfFile
EndOfFile TO
TO TRUE
TRUE
END-READ
END-READ
END-PERFORM
END-PERFORM
Set
TO
Set NotEndOfFile
NotEndOfFile
TO TRUE.
TRUE.
09/03/16
ER/CORP/CRS/LA01

19

FD
FD StudentFile.
StudentFile.
01
StudentDetails.
01 StudentDetails.
88
88 EndOfFile
EndOfFile VALUE
VALUE HIGH-VALUES.
HIGH-VALUES.
02
StudentId
PIC
02 StudentId
PIC 9(7).
9(7).
02
StudentName.
02 StudentName.
03
PIC
03 Surname
Surname
PIC X(8).
X(8).
03
Initials
PIC
XX.
03 Initials
PIC XX.
02
DateOfBirth.
02 DateOfBirth.
03
PIC
03 YOBirth
YOBirth
PIC 9(2).
9(2).
03
MOBirth
PIC
9(2).
03 MOBirth
PIC 9(2).
03
DOBirth
PIC
03 DOBirth
PIC 9(2).
9(2).
02
CourseCode
PIC
X(4).
02 CourseCode
PIC X(4).
02
Grant
PIC
02 Grant
PIC 9(4).
9(4).
02
Gender
PIC
X.
02 Gender
PIC X.
PROCEDURE
PROCEDURE DIVISION.
DIVISION.
Begin.
Begin.
OPEN
OPEN INPUT
INPUT StudentFile
StudentFile
READ
StudentFile
READ StudentFile
AT
AT END
END SET
SET EndOfFile
EndOfFile TO
TO TRUE
TRUE
END-READ
END-READ
PERFORM
PERFORM UNTIL
UNTIL EndOfFile
EndOfFile
DISPLAY
StudentId
DISPLAY StudentId SPACE
SPACE StudentName
StudentName SPACE
SPACE CourseCode
CourseCode
READ
StudentFile
READ StudentFile
AT
AT END
END SET
SET EndOfFile
EndOfFile TO
TO TRUE
TRUE
END-READ
END-READ
END-PERFORM
END-PERFORM
CLOSE
CLOSE StudentFile
StudentFile
09/03/16
ER/CORP/CRS/LA01
20
STOP
RUN.
STOP RUN.

Evaluate
EVALUATE WS-CHOICE
WHEN 1
PERFORM M0000-EMP-MAINTAIN-PARA
WHEN 2
PERFORM B0000-EMP-BROWSE-PARA
WHEN OTHER
PERFORM X0000-GEN-ERROR-PARA
END-EVALUATE

09/03/16

ER/CORP/CRS/LA01

21

The
PERFOR
M

09/03/16

ER/CORP/CRS/LA01

22

The PERFORM Verb

Iteration is an important programming construct. We use iteration


when we need to repeat the same instructions over and over again.

Most programming languages have several iteration keywords


(e.g. WHILE, FOR, REPEAT) which facilitate the creation
different types of iteration structure.

COBOL only has one iteration construct; PERFORM.

But the PERFORM has several variations.

Each variation is equivalent to one of the iteration types available


in other languages.

This lecture concentrates on three of the PERFORM formats. The


PERFORM..VARYING, the COBOL equivalent of the FOR , will
be introduced later.

09/03/16

ER/CORP/CRS/LA01

23

Paragraphs - Revisited

A Paragraph is a block of code to which we have given a name.

A Paragraph Name is a programmer defined name formed using


the standard rules for programmer defined names (A-Z, 0-9, -).

A Paragraph Name is ALWAYS terminated with a full-stop.

Any number of statements and sentences may be included in a


paragraph, and the last one (at least) must be terminated with a
full-stop.

The scope of a paragraph is delimited by the occurrence of


another paragraph name or the end of the program text.

09/03/16

ER/CORP/CRS/LA01

24

Example
P0000-PROCESS-RECORD.
DISPLAY StudentRecord
READ StudentFile
AT END MOVE HIGH-VALUES TO StudentRecord
END-READ.
D0000-PRODUCE-OUTPUT.
DISPLAY Here is a message.

NOTE
NOTE
The
Thescope
scopeof
ofP0000-PROCESSP0000-PROCESSRECORD
RECORDisisdelimited
delimitedby
bythe
the
occurrence
occurrencethe
theparagraph
paragraphname
name
D0000-PRODUCE-OUTPUT
..
09/03/16
ER/CORP/CRS/LA01
D0000-PRODUCE-OUTPUT

25

Syntax.

THRU

EndProc
PERFORM 1stProc
THROUGH

This is the only type of PERFORM that is not an


iteration construct.
It instructs the computer to transfer control to an out-ofline block of code.
When the end of the block is reached, control reverts to
the statement (not the sentence) immediately following
the PERFORM.
1stProc and EndProc are the names of Paragraphs or
Sections.
The PERFORM..THRU instructs the computer to treat
the Paragraphs or Sections from 1stProc TO EndProc as
a single block of code.
09/03/16
ER/CORP/CRS/LA01
26

Example.
Run of PerformFormat1

In
In TopLevel.
TopLevel. Starting
Starting to
to run
run program
program
>>>>
>>>> Now
Now in
in OneLevelDown
OneLevelDown
>>>>>>>>
Now
>>>>>>>> Now in
in TwoLevelsDown.
TwoLevelsDown.
>>>>
Back
in
OneLevelDown
>>>> Back in OneLevelDown
Back
Back in
in TopLevel.
TopLevel.

PROCEDURE
PROCEDURE DIVISION.
DIVISION.

TopLevel.
TopLevel.
DISPLAY
DISPLAY"In
"In TopLevel.
TopLevel.Starting
Startingto
torun
run program"
program"
PERFORM
PERFORM OneLevelDown
OneLevelDown
DISPLAY
DISPLAY "Back
"Back in
in TopLevel.".
TopLevel.".
STOP
RUN.
STOP RUN.

TwoLevelsDown.
TwoLevelsDown.
DISPLAY
DISPLAY ">>>>>>>>
">>>>>>>> Now
Now in
in TwoLevelsDown."
TwoLevelsDown."
OneLevelDown.
OneLevelDown.
DISPLAY
DISPLAY ">>>>
">>>> Now
Now in
in OneLevelDown"
OneLevelDown"
PERFORM
TwoLevelsDown
PERFORM TwoLevelsDown
DISPLAY
DISPLAY ">>>>
">>>> Back
Back in
in OneLevelDown".
OneLevelDown".

09/03/16

ER/CORP/CRS/LA01

27

Format 1
Example.
Run of PerformFormat1

In
In TopLevel.
TopLevel. Starting
Starting to
to run
run program
program
>>>>
Now
in
OneLevelDown
>>>> Now in OneLevelDown
>>>>>>>>
>>>>>>>> Now
Now in
in TwoLevelsDown.
TwoLevelsDown.
>>>>
Back
in
OneLevelDown
>>>> Back in OneLevelDown
Back
Back in
in TopLevel.
TopLevel.

PROCEDURE
PROCEDURE DIVISION.
DIVISION.

TopLevel.
TopLevel.
DISPLAY
DISPLAY "In
"In TopLevel.
TopLevel. Starting
Starting to
to run
run program"
program"
PERFORM
PERFORM OneLevelDown
OneLevelDown
DISPLAY
DISPLAY "Back
"Back in
in TopLevel.".
TopLevel.".
STOP
RUN.
STOP RUN.

TwoLevelsDown.
TwoLevelsDown.
DISPLAY
DISPLAY ">>>>>>>>
">>>>>>>> Now
Now in
in TwoLevelsDown."
TwoLevelsDown."
OneLevelDown.
OneLevelDown.
DISPLAY
DISPLAY ">>>>
">>>> Now
Now in
in OneLevelDown"
OneLevelDown"
PERFORM
TwoLevelsDown
PERFORM TwoLevelsDown
DISPLAY
DISPLAY ">>>>
">>>> Back
Back in
in OneLevelDown".
OneLevelDown".

09/03/16

ER/CORP/CRS/LA01

28

Format 1
Example.
Run of PerformFormat1

In
In TopLevel.
TopLevel. Starting
Starting to
to run
run program
program

>>>>
>>>> Now
Now in
in OneLevelDown
OneLevelDown

>>>>>>>>
>>>>>>>> Now
Now in
in TwoLevelsDown.
TwoLevelsDown.
>>>>
Back
in
OneLevelDown
>>>> Back in OneLevelDown
Back
Back in
in TopLevel.
TopLevel.

PROCEDURE
PROCEDURE DIVISION.
DIVISION.
TopLevel.
TopLevel.
DISPLAY
DISPLAY "In
"In TopLevel.
TopLevel. Starting
Starting to
to run
run program"
program"
PERFORM
OneLevelDown
PERFORM OneLevelDown
DISPLAY
DISPLAY "Back
"Back in
in TopLevel.".
TopLevel.".
STOP
RUN.
STOP RUN.
TwoLevelsDown.
TwoLevelsDown.
DISPLAY
DISPLAY ">>>>>>>>
">>>>>>>> Now
Now in
in TwoLevelsDown."
TwoLevelsDown."

OneLevelDown.
OneLevelDown.
DISPLAY
DISPLAY">>>>
">>>>Now
Now in
inOneLevelDown"
OneLevelDown"
PERFORM
PERFORM
DISPLAY
DISPLAY

09/03/16

TwoLevelsDown
TwoLevelsDown
">>>>
">>>> Back
Back in
in OneLevelDown".
OneLevelDown".

ER/CORP/CRS/LA01

29

Format 1
Example.
Run of PerformFormat1

In
In TopLevel.
TopLevel. Starting
Starting to
to run
run program
program
>>>>
Now
in
OneLevelDown
>>>> Now in OneLevelDown
>>>>>>>>
>>>>>>>> Now
Now in
in TwoLevelsDown.
TwoLevelsDown.
>>>>
Back
in
OneLevelDown
>>>> Back in OneLevelDown
Back
Back in
in TopLevel.
TopLevel.

PROCEDURE
PROCEDURE DIVISION.
DIVISION.
TopLevel.
TopLevel.
DISPLAY
DISPLAY "In
"In TopLevel.
TopLevel. Starting
Starting to
to run
run program"
program"
PERFORM
OneLevelDown
PERFORM OneLevelDown
DISPLAY
DISPLAY "Back
"Back in
in TopLevel.".
TopLevel.".
STOP
RUN.
STOP RUN.
TwoLevelsDown.
TwoLevelsDown.
DISPLAY
DISPLAY ">>>>>>>>
">>>>>>>> Now
Now in
in TwoLevelsDown."
TwoLevelsDown."

OneLevelDown.
OneLevelDown.
DISPLAY
DISPLAY ">>>>
">>>> Now
Now in
in OneLevelDown"
OneLevelDown"
PERFORM
PERFORM TwoLevelsDown
TwoLevelsDown

DISPLAY
DISPLAY ">>>>
">>>> Back
Back in
in OneLevelDown".
OneLevelDown".

09/03/16

ER/CORP/CRS/LA01

30

Format 1
Example.
Run of PerformFormat1

In
In TopLevel.
TopLevel. Starting
Starting to
to run
run program
program
>>>>
Now
in
OneLevelDown
>>>> Now in OneLevelDown

>>>>>>>>
>>>>>>>> Now
Now in
in TwoLevelsDown.
TwoLevelsDown.
>>>>
>>>> Back
Back in
in OneLevelDown
OneLevelDown
Back
in
TopLevel.
Back in TopLevel.

PROCEDURE
PROCEDURE DIVISION.
DIVISION.
TopLevel.
TopLevel.
DISPLAY
DISPLAY "In
"In TopLevel.
TopLevel. Starting
Starting to
to run
run program"
program"
PERFORM
OneLevelDown
PERFORM OneLevelDown
DISPLAY
DISPLAY "Back
"Back in
in TopLevel.".
TopLevel.".
STOP
RUN.
STOP RUN.

TwoLevelsDown.
TwoLevelsDown.
DISPLAY
DISPLAY">>>>>>>>
">>>>>>>>Now
Now in
inTwoLevelsDown."
TwoLevelsDown."
OneLevelDown.
OneLevelDown.
DISPLAY
DISPLAY ">>>>
">>>> Now
Now in
in OneLevelDown"
OneLevelDown"
PERFORM
TwoLevelsDown
PERFORM TwoLevelsDown
DISPLAY
DISPLAY ">>>>
">>>> Back
Back in
in OneLevelDown".
OneLevelDown".

09/03/16

ER/CORP/CRS/LA01

31

Format 1
Example.
Run of PerformFormat1

In
In TopLevel.
TopLevel. Starting
Starting to
to run
run program
program
>>>>
Now
in
OneLevelDown
>>>> Now in OneLevelDown
>>>>>>>>
>>>>>>>> Now
Now in
in TwoLevelsDown.
TwoLevelsDown.

>>>>
>>>> Back
Back in
in OneLevelDown
OneLevelDown
Back
Back in
in TopLevel.
TopLevel.

PROCEDURE
PROCEDURE DIVISION.
DIVISION.
TopLevel.
TopLevel.
DISPLAY
DISPLAY "In
"In TopLevel.
TopLevel. Starting
Starting to
to run
run program"
program"
PERFORM
OneLevelDown
PERFORM OneLevelDown
DISPLAY
DISPLAY "Back
"Back in
in TopLevel.".
TopLevel.".
STOP
RUN.
STOP RUN.
TwoLevelsDown.
TwoLevelsDown.
DISPLAY
DISPLAY ">>>>>>>>
">>>>>>>> Now
Now in
in TwoLevelsDown."
TwoLevelsDown."

OneLevelDown.
OneLevelDown.
DISPLAY
DISPLAY
PERFORM
PERFORM

">>>>
">>>> Now
Now in
in OneLevelDown"
OneLevelDown"
TwoLevelsDown
TwoLevelsDown

DISPLAY
DISPLAY">>>>
">>>>Back
Backin
in OneLevelDown".
OneLevelDown".
09/03/16
ER/CORP/CRS/LA01

32

Format 1
Example.
Run of PerformFormat1

In
In TopLevel.
TopLevel. Starting
Starting to
to run
run program
program
>>>>
Now
in
OneLevelDown
>>>> Now in OneLevelDown
>>>>>>>>
>>>>>>>> Now
Now in
in TwoLevelsDown.
TwoLevelsDown.
>>>>
Back
in
OneLevelDown
>>>> Back in OneLevelDown

Back
Back in
in TopLevel.
TopLevel.
PROCEDURE
PROCEDURE DIVISION.
DIVISION.

TopLevel.
TopLevel.

DISPLAY
DISPLAY
PERFORM
PERFORM

"In
"In TopLevel.
TopLevel. Starting
Starting to
to run
run program"
program"
OneLevelDown
OneLevelDown

DISPLAY
DISPLAY"Back
"Backin
inTopLevel.".
TopLevel.".
STOP
STOP RUN.
RUN.

TwoLevelsDown.
TwoLevelsDown.
DISPLAY
DISPLAY ">>>>>>>>
">>>>>>>> Now
Now in
in TwoLevelsDown."
TwoLevelsDown."
OneLevelDown.
OneLevelDown.
DISPLAY
DISPLAY ">>>>
">>>> Now
Now in
in OneLevelDown"
OneLevelDown"
PERFORM
TwoLevelsDown
PERFORM TwoLevelsDown
DISPLAY
DISPLAY ">>>>
">>>> Back
Back in
in OneLevelDown".
OneLevelDown".

09/03/16

ER/CORP/CRS/LA01

33

Format 2 - Syntax

THRU

EndProc
PERFORM 1stProc
THROUGH

RepeatCount TIMES

StatementBlock END - PERFORM


PROCEDURE
PROCEDUREDIVISION.
DIVISION.
Begin.
Begin.
Statements

PERFORM DisplayName 4 TIMES


Statements

STOP RUN.
DisplayName.
DisplayName.
DISPLAY Tom Ryan.

09/03/16

ER/CORP/CRS/LA01

34

Example
IDENTIFICATION
IDENTIFICATION DIVISION.
DIVISION.
PROGRAM-ID.
PERFORM2.
PROGRAM-ID. PERFORM2.
AUTHOR.
AUTHOR. Michael
Michael Coughlan.
Coughlan.
DATA
DATA DIVISION.
DIVISION.
WORKING-STORAGE
WORKING-STORAGE SECTION.
SECTION.
01
NumofTimes
PIC
01 NumofTimes
PIC 99 VALUE
VALUE 5.
5.

Run of PerformExample2
Starting to run program
Starting to run program
>>>>This is an in line Perform
>>>>Thisis
isan
anin
inline
linePerform
Perform
>>>>This
>>>>This is an in line Perform
>>>>This is an in line Perform
>>>>This is an in line Perform
Finished in line Perform
Finished in line Perform
>>>> This is an out of line Perform
>>>> This is an out of line Perform
>>>> This is an out of line Perform
>>>>This
Thisis
isan
anout
outof
ofline
linePerform
Perform
>>>>
>>>> This is an out of line Perform
>>>> This is an out of line Perform
>>>> This is an out of line Perform
>>>> This is an out of line Perform
>>>> This is an out of line Perform
Back in Begin. About to Stop
Back in Begin. About to Stop

PROCEDURE
PROCEDURE DIVISION.
DIVISION.
A0000-MAIN-PARA.
A0000-MAIN-PARA.
DISPLAY
DISPLAY "Starting
"Starting to
to run
run program"
program"
PERFORM
3
TIMES
PERFORM 3 TIMES
DISPLAY
DISPLAY ">>>>This
">>>>This is
is an
an in
in line
line Perform"
Perform"
END-PERFORM
END-PERFORM
DISPLAY
DISPLAY "Finished
"Finished in
in line
line Perform"
Perform"
PERFORM
D0000-OUTOFLINE-EG
PERFORM D0000-OUTOFLINE-EG NumOfTimes
NumOfTimes TIMES
TIMES
DISPLAY
"Back
in
Begin.
About
to
Stop".
DISPLAY "Back in Begin. About to Stop".
STOP
STOP RUN.
RUN.

D0000-OUTOFLINE-EG.
D0000-OUTOFLINE-EG.
DISPLAY
DISPLAY ">>>>
">>>> This
This is
is an
an out
out of
of line
line Perform".
Perform".

09/03/16

ER/CORP/CRS/LA01

35

Syntax

THRU

BEFORE
EndProc WITH TEST

PERFORM 1stProc
THROUGH
AFTER

UNTIL Condition

StatementBlock END - PERFORM

This format is used where the WHILE or REPEAT


constructs are used in other languages.

If the WITH TEST BEFORE phrase is used the


PERFORM behaves like a WHILE loop and the condition
is tested before the loop body is entered.

If the WITH TEST AFTER phrase is used the PERFORM


behaves like a REPEAT loop and the condition is tested
after the loop body is entered.

The WITH TEST BEFORE phrase is the default and so is


rarely explicitly stated.
09/03/16
ER/CORP/CRS/LA01
36

PERFORM
PERFORMWITH
WITH
TEST
TESTBEFORE
BEFORE==
WHILE
WHILE...
...DO
DO

PERFORM
PERFORMWITH
WITH
TEST
TESTAFTER
AFTER==
REPEAT
REPEAT...
...UNTIL
UNTIL

Loop Body

test

Loop Body

False

test

True

True

Next Statement

09/03/16

False

Next Statement

ER/CORP/CRS/LA01

37

Arithmetic
and
Edited Pictures

09/03/16

ER/CORP/CRS/LA01

38

Arithmetic Verb Template


TO

Identifier FROM Identifier

VERB
Identifier
GIVING
Identifier

Literal BY

INTO

ROUNDED

ON SIZE ERROR StatementBlock END - VERB

Most COBOL arithmetic verbs conform to the template above. For


example;
ADD Takings TO CashTotal.
ADD Males TO Females GIVING TotalStudents.
SUBTRACT Tax FROM GrossPay.
SUBTRACT Tax FROM GrossPay GIVING NetPay.
DIVIDE Total BY Members GIVING MemberAverage.
DIVIDE Members INTO Total GIVING MemberAverage.
MULTIPLY 10 BY Magnitude.
MULTIPLY Members BY Subs GIVING TotalSubs.

The exceptions are the COMPUTE and the DIVIDE with


REMAINDER.

09/03/16

ER/CORP/CRS/LA01

39

The ROUNDED
option
Receiving Field Actual Result Truncated Result Rounded Result
PIC 9(3)V9.

123.25

123.2

123.3

PIC 9(3).

123.25

123

123

The ROUNDED option takes effect when, after decimal


point alignment, the result calculated must be
truncated on the right hand side.

The option adds 1 to the receiving item when the


leftmost truncated digit has an absolute value of 5 or
greater.
09/03/16

ER/CORP/CRS/LA01

40

ON SIZE ERROR option


Receiving Field
PIC 9(3)V9.
PIC 9(3)V9.

Actual Result
245.96
1245.9

PIC 9(3).

124

PIC 9(3).

1246

PIC 9(3)V9 Not Rounded

124.45

PIC 9(3)V9 Rounded

124.45

PIC 9(3)V9 Rounded

3124.45

SIZE ERROR

Yes
Yes
No
Yes
Yes
No
Yes

A size error condition exists when, after decimal point


alignment, the result is truncated on either the left or
the right hand side.

If an arithmetic statement has a rounded phrase then a


size error only occurs if there is truncation on the left
hand side (most significant digits).
09/03/16
ER/CORP/CRS/LA01
41

ADD Examples
Before
Before
After
After
Before
Before
After
After
Before
Before
After
After
Before
Before
After
After

ADD
ADD Cash
Cash TO
TO Total.
Total.
33
1000
1000
3

1003

ADD
ADD Cash,
Cash, 20
20 TO
TO
33
3

Total,
Total, Wage.
Wage.
1000
1000 100
100

1023

123

ADD
ADD Cash,
Cash, Total
TotalGIVING
GIVINGResult.
Result.
33
1000
0015
1000
3
1000
10030015
ADD
ADDMales
MalesTO
TOFemales
FemalesGIVING
GIVING TotalStudents.
TotalStudents.
1500
0625
1500
0625
1234
1500
0625
21251234

09/03/16

ER/CORP/CRS/LA01

42

SUBTRACT Examples
Before
Before
After
After

SUBTRACT
SUBTRACT Tax
Tax FROM
FROMGrossPay,
GrossPay,Total.
Total.
120
4000
9120
120
4000
9120
120
3880
9000

Before
Before
After
After

SUBTRACT
SUBTRACT Tax,
Tax,80
80FROM
FROMTotal.
Total.
100
480
100
480
100
300

Before
Before
After
After

SUBTRACT
SUBTRACT Tax
Tax FROM
FROMGrossPay
GrossPayGIVING
GIVINGNetPay.
NetPay.
750
1000
0012
750
1000
0012
750
1000
0250

09/03/16

ER/CORP/CRS/LA01

43

MULTIPLY and DIVIDE Examples


MULTIPLY
MULTIPLY Subs
SubsBY
BYMembers
MembersGIVING
GIVINGTotalSubs
TotalSubs
ON
ONSIZE
SIZEERROR
ERRORDISPLAY
DISPLAY"TotalSubs
"TotalSubstoo
toosmall"
small"
END-MULTIPLY.
END-MULTIPLY.
Subs
Subs

15.50
15.50

Before
Before
After
After

Before
Before
After
After

15.50

100
100

0123.45
0123.45

1550.00

MULTIPLY
Magnitude,
Size.
MULTIPLY10
10BY
BY
Magnitude,
Size.
3550
1250
355
125
355
125
9234.55

Before
Before
After
After

100

Members
Members TotalSubs
TotalSubs

100

92.35

DIVIDE
DIVIDE Total
Total BY
BY Members
MembersGIVING
GIVINGAverage
Average ROUNDED.
ROUNDED.
9234.55
100
1234.56
9234.55
100
1234.56

09/03/16

ER/CORP/CRS/LA01

44

The COMPUTE

COMPUTE Identifier [ ROUNDED ] ... = ArithmeticExpression


ON SIZE ERROR


NOT ON SIZE ERROR

StatementBlock END - COMPUTE

Precedence
Precedence Rules.
Rules.
1.
1.
2.
2.
3.
3.

Before
Before
After
After

****
**
//
++
--

== POWER
POWER
== MULTIPLY
MULTIPLY
== DIVIDE
DIVIDE
== ADD
ADD
== SUBTRACT
SUBTRACT

NNNN
xx

++
--

Compute
ComputeIrishPrice
IrishPrice==SterlingPrice
SterlingPrice//Rate
Rate ** 100.
100.
1000.50
156.25
87
1000.50
156.25
87

09/03/16

179.59

156.25

ER/CORP/CRS/LA01

87

45

Thank You

09/03/16

ER/CORP/CRS/LA01

46

Das könnte Ihnen auch gefallen