Title: Selection
1Selection Iteration
- DT266/2 Information System
- COBOL
2Conditions in COBOL
- Conditions - punctuation ().
- lt, , gt, lt or , less than, ..., alphabetic,
numeric, negative. - Level 88s
- owning group
- condition name
- Value
- true values
- individual, range, selection
- Truth, falseness
3Level 88s
- Level 88
- Condition Name
- Specifies that a condition is true when value of
data item changes to that defined in the
associated picture clause - 01 MONTH-IN PIC 99.
- 88 VALID-MONTH value 1 through 12.
- 88 FEB value 2.
- 88 30-DAY value 4, 6, 9,11.
- 01 DATE-INDICATOR PIC 9 value 0.
- 88 VALID-DATE value 1.
4Selection
- 3 types of selection
- IF ltconditiongt code/paragraph-name
- END-IF
- IF ltconditiongt code/paragraph-name
- ELSE code/paragraph-name
- END-IF
- Evaluate ...When ...When Other...End-Evaluate
- Examples - Select1, Select2
5Identification Division. Program-Id. PGMSLECT
initial. Data Division. Working-Storage
Section. 01 Amount-In pic 99. Procedure
Division. Display "Age eligibility for free
schooling". Display "Enter a 2-digit age"
with no advancing. Accept Amount-In. If
Amount-In lt 4 Display "This child is too
young" else if Amount-In lt 19
Display "This person is eligible for free
schooling" else Display
"This person is not automatically eligible"
end-if end-if Stop run.
6Evaluating against a value
- Evaluate TRUE
- When A gt B
- code-1
- When C gt D
- code-2
- When other
- code-3
- End-Evaluate
- This construct allows several conditions to be
checked sequentially. As soon as a true
condition is met, the code relating to that
condition is executed and control skips past
End-evaluate. - A period at any point will terminate the evaluate.
7- Identification Division.
- Program-Id. PGMSLECT2 initial.
- Data Division.
- Working-Storage Section.
- 01 Amount-In pic 99.
- Procedure Division.
- Display "Age eligibility for free
schooling". - Display "Enter a 2-digit age" with no
advancing. - Accept Amount-In.
- Evaluate Amount-In
- When 0 through 3
- Display "This child is too
young" - When 4 through 19
- Display "This person is
eligible for free schooling" - When other
- Display "This person is not
automatically eligible" - End-evaluate
- Stop run.
8Evaluating with condition names
Evaluate true When 30-day
If day-in lt 31 set
valid-date to true End-if
When not Feb Set valid-date to
true When other Perform
Validate-feb End-evaluate.
In this example, a data item containing the month
is declared as follows- 01 MONTH-IN PIC
99. 88 VALID-MONTH value 1 through 12.
88 FEB value 2. 88
30-DAY value 4, 6, 9,11. 01
DATE-INDICATOR PIC 9 value 0. 88 VALID-DATE
value 1.
9Evaluating menu options
Evaluate true When Add-new-record
perform ENTER-TOY When
View-a-record perform VIEW-a-TOY When
Delete-a-record perform Delete-toy
When Amend-a-record perform Change-toy
End-evaluate.
In this example, the option has been chosen from
either a drop-down or numeric menu. 01
menu-option pic 9. 88 add-new-record value
1. 88 view-a-record value 2. 88
delete-a-record value 3. 88 amend-a-record
value 4. 88 exit-menu value 5.
10Performing paragraphs
- Paragraphs are pieces of code that can be placed
anywhere in the program. - A displaced paragraph occurs after the stop run
and is performed from elsewhere in the program by
the statement - Perform ltparagraph-namegt
- Paragraphs end when a new one begins
- When the paragraph is executed, control returns
to the statement after the perform
11Example of displaced paragraph
Procedure Division. Main Section.
Housekeeping-paragraph. Perform
Get-Radius. Multiply R by R giving
R-Squared. Multiply R-Squared by PI
giving D-Area. Display 'Area is ' with
no advancing. Display D-Area.
Stop run. Get-Radius. Display
'Enter 2-digit integer radius' with no
advancing. Accept R.
12Iteration
- PERFORM statement
- LEADING EDGE
- While Loop
- Test Before
- TRAILING EDGE
- Repeat Loop
- Test After
- VARYING
- For Loop
13Use of PERFORM for iteration
- All forms of iteration use PERFORM
- The leading edge or test before loop is a while
loop - The condition is tested before the code is
executed. - PERFORM ltloop-paragraphgt until ltconditiongt.
- PERFORM until ltcondition
- ltcode statementsgt
- END-PERFORM.
14Sample leading edge loop
Procedure Division. Display "Age
eligibility for free schooling".
Perform enter-age until amount-in gt3 and
amount-in lt 19. Stop run.
Enter-age. Display "Enter a 2-digit
age" with no advancing Accept
Amount-In If not (amount-in gt 3 and
amount-in lt 19) Display "Not an
eligible age - retry!" End-if.
15Embedding paragraphs
- Sometimes it is preferable to be able to see the
code in the line of the program. In this case,
the code is embedded between - PERFORM until ltconditiongt
- embedded code
- END-PERFORM.
- There should be no period within this code.
16- Identification Division.
- Program-Id. iter1 initial.
- Data Division.
- Working-Storage Section.
- 01 Amount-In pic 99 value 0.
- Procedure Division.
- Display "Age eligibility for free
schooling". - Perform until amount-in gt3 and
amount-in lt 19 - Display "Enter a 2-digit age"
with no advancing - Accept Amount-In
- If not (amount-in gt 3 and
amount-in lt 19) - Display "Not an eligible age -
retry!" - End-if
- End-perform
- Stop run.
17Trailing-edge loops
- The trailing-edge loop or repeat loop is
implemented in Cobol by the with test after
clause. - This loop forces the code to be executed at least
once - Iteration ceases subject to the falseness of the
condition.
18Sample trailing edge loop
- Perform accept-menu-option with test after
until option gt 0 and option lt 6. - Perform Carry-out-option.
- Stop run.
- Accept-menu-option.
- Accept menu-option.
- If menu-option lt 1 or gt 5
- display Invalid option - try again
- end-if.
- Carry-out-option.
19Embedded trailing edge loop
- Perform with test after until option gt 0 and
option lt 6 - Accept menu-option
- If menu-option lt 1 or gt 5
- display Invalid option - try again
- end-if
- end-perform.
- Perform carry-out-option.
- Stop run.
- Carry-out-option.