[This chapter] [TOC] [Tutorial]
[This chapter] [TOC] [Tutorial]
IF condition THEN
{statement-1 | NEXT SENTENCE}
{ELSE
{statement-2 | NEXT SENTENCE}}
[END-IF]
[This chapter] [TOC] [Tutorial]
The exact syntax of conditions can be found in the COBOL manual, but I will introduce the level 88 and the programmer defined class names here.
- Variable names and literal values
- Comparison operators (<, >, = …)
- AND
- OR
- NOT
- Predefined condition names (numeric)
- Condition names defined by the programmer (level 88)
- Class names
[This chapter] [TOC] [Tutorial]
There can be given a name to the fact that a variable contains one or more specific values. If the variable - at any given time during execution of the program - contains the value, the name of the condition is TRUE, else it is FALSE.
- The program will be better to read
- Reduced possibility of errors due to inconsistency
000000* An example illustrating the use of a programmer defined conditionname
identification division.
program-id. level88.
author. kik.
environment division.
configuration section.
special-names.
console is crt
decimal-point is comma.
data division.
working-storage section.
77 transaction-kode pic 99.
88 valid-kode value 4, 8 thru 15.
88 create value 10.
88 destroy value 15.
procedure division.
main section.
*
* Some code leading to "transacion-kode" getting a value
*
move 10 to transaction-kode.
*
* Testing the conditions
*
if valid-kode then
if create then
perform p-create
else
if destroy then
perform p-destroy
else
perform ordinary-transaction.
*
p-create.
* some creation code
p-destroy.
* some destruction code
ordinary-transaction.
* some ordinary data processing code
*-------------------------------------------------------
* This is how the selection should have been made without the level88.
if transaction-kode = 4 or
( transaction-kode >= 8 and transaction-kode <= 15 ) then
if transaction-kode = 10 then
perform p-create
else
if transaction-kode = 15 then
perform p-destroy
else
perform ordinary-transaction.
[This chapter] [TOC] [Tutorial]
000000* An example illustrating the use of a programmer defined classname
identification division.
program-id. classname.
author. kik.
environment division.
configuration section.
special-names.
console is crt
decimal-point is comma
class hexadecimal is '0' thru '9',
'a' thru 'f', 'A' thru 'F'.
data division.
working-storage section.
77 data-string pic x(30).
procedure division.
main section.
*
* Some code leading to "data-string" getting a value
*
move '15f6bC6' to data-string.
*
* Testing the conditions
*
if data-string is hexadecimal then
perform hex-process
else
perform ordinary-process.
*
hex-process.
*
ordinary-process.
[This chapter] [TOC] [Tutorial]
Zero or more iterations
One or more iterations
Incrementing a counter
[This chapter] [TOC] [Tutorial]
PERFORM statement series END-PERFORM
[This chapter] [TOC] [Tutorial]
PERFORM paragraph-1 [THRU paragraph-2]
[This chapter] [TOC] [Tutorial]
PERFORM paragraph [THRU paragraph-2]
WITH TEST {BEFORE | AFTER}
UNTIL condition
Se also : PERFORM - repeat (inline)[This chapter] [TOC] [Tutorial]
PERFORM [WITH TEST {BEFORE | AFTER}]
UNTIL condition
statement series
END-PERFORM
Se also : PERFORM - repeat[This chapter] [TOC] [Tutorial]
PERFORM paragraph [THRU paragraph-2] VARYING counter FROM start value BY interval UNTIL end condition [AFTER counter-2 FROM start value-2 BY interval-2 UNTIL end condition-2] [AFTER counter-3 FROM start value-3 BY interval-3 UNTIL end condition-3]
[This chapter] [TOC] [Tutorial]
GO TO paragraph-1 [paragraph-2] ... DEPENDING ON identifier.
[This chapter] [TOC] [Tutorial]
EVALUATE TRANSACTION-CODE WHEN 1 PERFORM P-A WHEN 2 PERFORM P-B WHEN 3 PERFORM P-C WHEN OTHER PERFORM P-D.Depending on the value in TRANSACTION-CODE Evaluate will execute one of the four paragraphs P-A .. P-D.
000000* An example illustrating the use of the EVALUATE instruction
identification division.
program-id. evaluat.
author. kik.
environment division.
configuration section.
special-names.
console is crt
decimal-point is comma.
data division.
working-storage section.
77 transaction-kode pic 99.
88 valid-kode value 4, 8 thru 15.
88 create value 10.
88 destroy value 15.
77 balance pic 9(9)v99 comp.
procedure division.
main section.
*
* Some code leading to variables getting a value
*
move 10 to transaction-kode.
move 1234,56 to balance.
*
* Testing the conditions
*
if valid-kode then
evaluate true also balance > zero
when create also any perform p-create
when destroy also false perform p-destroy
when other
perform ordinary-transaction.
*
p-create.
* some creation code
p-destroy.
* some destruction code
ordinary-transaction.
* some ordinary data processing code
*-------------------------------------------------------
* This is how the selection should have been made using IF without the level88.
if transaction-kode = 4 or
( transaction-kode >= 8 and transaction-kode <= 15 ) then
if transaction-kode = 10 then
perform p-create
else
if transaction-kode = 15 and not balance > zero then
perform p-destroy
else
perform ordinary-transaction.
[This chapter] [TOC] [Tutorial]
[This chapter] [TOC] [Tutorial]
000000* An example illustrating the use of a programmer defined paragraphs
* and perform-thru
identification division.
program-id. level88.
author. kik.
environment division.
configuration section.
special-names.
console is crt
decimal-point is comma.
data division.
working-storage section.
77 transaction-kode pic 99.
88 valid-kode value 4, 8 thru 15.
88 create value 10.
88 destroy value 15.
procedure division.
main section.
*
* Some code leading to "transacion-kode" getting a value
*
move 10 to transaction-kode.
*
* Testing the conditions
*
if valid-kode then
if create then
perform p-create thru p-create-end
else
if destroy then
perform p-destroy thru p-destroy-end
else
perform ordinary-transaction
thru ordinary-transaction-end.
*
p-create.
* some creation code
p-create-end.
exit.
p-destroy.
* some destruction code
p-destroy-end.
exit.
ordinary-transaction.
* some ordinary data processing code
ord-trns-1.
ord-trns-2.
ordinary-transaction-end.
exit.
Sections can be PERFORMed as a whole, i.e. these are to be viewed as an entity, a factor that is especially relevant in the case of overlay sections. In a modern operating system it is normally not the programmers responsibility to control what part of a program that is to be swapped to a disk-file. The operating system is usually much better at this task.[This chapter] [TOC] [Tutorial]
[This chapter] [TOC] [Tutorial]