2.4.13 EVALUATE
| | | | | |
Format:
EVALUATE {Datenfeld1 / TRUE / FALSE}
[
ALSO {Datenfeld2 / TRUE / FALSE}
...
]
[
WHEN {
ANY / Bedingung1 / TRUE / FALSE /
[NOT] Datenfeld3 [ {THRU / THROUGH } Datenfeld4
}
[
ALSO {
ANY / Bedingung2 / TRUE / FALSE /
[NOT] Datenfeld5 [ {THRU / THROUGH } Datenfeld6
}
....
]
Befehlssequenz 1
[WHEN OTHER Befehlssequenz 2]
...
]
[END-EVALUATE]
| |
| | | |
Mit EVALUATE können Feldinhalte auf unterschiedliche Werte oder Bedingungen untersucht werden.
Ausdruck entspricht einer IF-Abfrage
1. Direkter Wertevergleich
Beispiel:
identification division.
environment division.
data division.
working-storage section.
01 numfeld pic 99.
procedure division.
move 9 to numfeld.
perform check varying numfeld from 9 by 1 until numfeld = 21.
call getchar.
stop run.
check section.
display numfeld ' ' with no advancving.
evaluate numfeld
when 10 display 'numfeld ist 10'
when 20 display 'numfeld ist 20'
when 11 thru 15 display 'numfeld liegt zwischen 11 und 15'
when 16 thru 19 display 'numfeld liegt zwischen 16 und 19'
when other display 'numfeld ist < 10 oder > 20'
end-evaluate
2. Bedingungen abfragen
Beispiel:
identification division.
environment division.
data division.
working-storage section.
01 numfeld pic 99.
procedure division.
perform check varying numfeld from 9 by 1 until numfeld = 16.
call getchar.
stop run.
check section.
display numfeld ' ' with no advancving.
evaluate true
when numfeld < 10 display 'numfeld kleiner als 10'
when numfeld < 15 display 'numfeld kleiner als 15'
when other display 'numfeld >= 15'
end-evaluate
Wertetabellen lassen sich mit EVALUATE besonders leicht kodieren:
|
x | y | z | Ergebnis | 10 | 4 | 8 | 1 | 10 | 5 | 3 | 2 | 20 | 7 | 7 | 3 | 30 | 7 | 9 | 4 | 30 | 8 | 4 | 5 | beliebig 1 | 1 | beliebig | 6 | sonst | | | 0 |
|
identification division.
environment division.
data division.
working-storage section.
01 x pic 99.
01 y pic 99.
01 z pic 99.
01 erg pic 99.
procedure division.
move 10 to x.
move 0 to y z.
perform check.
move 4 to y.
move 8 to z.
perform check.
move 5 to y.
move 3 to z.
perform check.
move 20 to x.
move 7 to y z.
perform check.
move 30 to x.
move 9 to z.
perform check.
move 8 to y.
move 4 to z.
perform check.
move 12 to x
move 11 to y.
perform check.
move 5 to z
move 12 to y.
perform check.
call getchar.
check section.
evaluate x also y also z
when 10 also 4 also 8 move 1 to erg
when 10 also 5 also 3 move 2 to erg
when 20 also 7 also 7 move 3 to erg
when 30 also 7 also 9 move 4 to erg
when 30 also 8 also 4 move 5 to erg
when any also 11 also any move 6 to erg
when other move 0 to erg
end-evaluate
display x ' ' y ' ' z ' --> ' erg.
Zurück zu Acc-Cobol Divide Statement
Hoch zu Procedure Divison in COBOL
Weiter zu EXIT
|