Agenda Collating sequence / Sorting data Selection (Logic) Infinite Loops n!!Not me! Case statements Iteration Manipulating / selecting data in variables STRISDB also see handout
Sorting on the AS/400 EBCDIC Sort (Collating sequence) 1. Blanks 2. Special Characters 3. Lower Case Letters 4. Upper Case Letters 5. Numbers
Selection
Selection Condition? Statement2 Statement1
Relational Operators
Selection IF Condition THEN Statement(s) ELSE END-IF
If-Then-Else A = 10 B = 20 If A > B THEN MOVE B TO A ELSE MOVE A TO B.
If-Then-Else A = 10 B = 20 If (A > B) THEN MOVE B TO A.
If-Then-Else NESTED A = 10 B = 20 C = 30 D = 40 If A < B THEN IF A > C THEN MOVE A TO C ELSE MOVE C TO A ENDIF MOVE A TO B END-IF.
IF-THEN-ELSE Statements IF MARKS >= 80 THEN MOVE ‘A’ TO GRADE ELSE IF MARKS >= 70 THEN MOVE ‘B’ TO GRADE IF MARKS >= 60 THEN MOVE ‘C’ TO GRADE IF MARKS >= 55 THEN MOVE ‘D’ TO GRADE MOVE ‘F’ TO GRADE END-IF ENDIF.
Condition Names - 88’s Used with Code Fields Eg. ARE-THERE-MORE-RECORDS Eg. FINAL-GRADE Eg. Indicators
Condition Names 01 ARE-THERE-MORE-RECORDS PIC X(3) VALUE ‘YES’. 88 THERE-ARE-MORE-RECORDS VALUE ‘YES’. 88 END-OF-FILE VALUE ‘NO’. IF THERE-ARE-MORE-RECORDS READ EMPLOYEE-FILE. IF END-OF-FILE PERFORM TERMINATION-RTN.
Set Verb Used to initialize fields to a Condition-Name. 01 ARE-THERE-MORE-RECORDS PIC X(3). 88 THERE-ARE-MORE-RECORDS VALUE ‘YES’. 88 END-OF-FILE VALUE ‘NO’. SET END-OF-FILE TO TRUE.
Conditions Cont’d 01 WORK-DAYS PIC X(3). 88 MONDAY VALUE ‘MON’. 88 TUESDAY VALUE ‘TUE’. 88 WEDNESDAY VALUE ‘WED’. 88 THURSDAY VALUE ‘THU’ 88 FRIDAY VALUE ‘FRI’. SET MONDAY TO TRUE. IF FRIDAY DISPLAY ‘GO HOME EARLY’.
Conditions Cont’d 01 FALL-MONTHS PIC X(3). 88 SEPTEMBER VALUE ‘SEP’. 88 OCTOBER VALUE ‘OCT’. 88 NOVEMBER VALUE ‘NOV’. 88 DECEMBER VALUE ‘DEC’. IF OCTOBER DISPLAY ‘HAPPY HALLOWEEN!!’ SET DECEMBER TO TRUE.
Option Indicators and Conditions 01 WS-indicators. 05 IN90 INDICATOR 90 PIC 1. 88 display-message value B’1’. 88 dont-display-message value B’0’. Set display-message to true. Set dont-display-message to true. If display-message If don’t-display-message If not display-message
Response Indicators and Conditions 01 WS-Control. 05 ws-function-key pic x(2). 88 F3 value ’03’ 88 F12 value ’12’. 88 Enter value ’00’ 05 ws-device-name pic x(10). 05 ws-record-format pic x(10). If F3 If not F3
Selection Statements
Sign Test A = 10 B = -10 If A IS NEGATIVE IF A IS POSITIVE MOVE B TO A MOVE B TO A ELSE ELSE MOVE A TO B. MOVE A TO B.
Sign Test A = 10 B = -10 If A IS NEGATIVE IF A IS POSITIVE MOVE B TO A MOVE B TO A ELSE ELSE MOVE A TO B. MOVE A TO B.
Numeric Test A = 10 B = -10 If A IS NUMERIC ADD A TO B.
Alphabetic Test C = ‘CJ’ D = ‘Christy’ If C is ALPHABETIC THEN MOVE C TO OLDER-SISTER ELSE MOVE D TO OLDER-SISTER END-IF
Alphabetic-Upper Test C = ‘CJ’ D = ‘Christy’ If C is ALPHABETIC-UPPER THEN MOVE C TO OLDER-SISTER ELSE MOVE D TO OLDER-SISTER END-IF
Alphabetic-Lower Test C = ‘CJ’ D = ‘Christy’ If D is ALPHABETIC-LOWER THEN MOVE D TO OLDER-SISTER ELSE MOVE C TO OLDER-SISTER END-IF
AND
OR
AND/OR Order of Operations Brackets First ANDs (From Left to Right) ORs (From Left to Right)
ANDs/ORs A = 10 B = -10 C = 20 If (A<B) OR (B<C) AND (A<C) THEN MOVE A TO C ELSE MOVE A TO B END-IF
Replacing nested IF’s with Case Statements Replacing nested IF’s with “CASE” statements
IF-THEN-ELSE Statements IF MARKS >= 80 THEN MOVE ‘A’ TO GRADE ELSE IF MARKS >= 70 THEN MOVE ‘B’ TO GRADE IF MARKS >= 60 THEN MOVE ‘C’ TO GRADE IF MARKS >= 55 THEN MOVE ‘D’ TO GRADE MOVE ‘F’ TO GRADE END-IF ENDIF.
CASE Statements EVALUATE TRUE WHEN MARKS >= 80 MOVE ‘A’ TO GRADE WHEN MARKS >= 70 AND MARKS < 80 MOVE ‘B’ TO GRADE WHEN MARKS >= 60 AND MARKS < 70 MOVE ‘C’ TO GRADE WHEN MARKS >= 55 AND MARKS < 60 MOVE ‘D’ TO GRADE WHEN OTHER MOVE ‘F’ TO GRADE END-EVALUATE.
CASE Statements EVALUATE GRADE WHEN ‘A’ DISPLAY ‘WONDERFUL!!!!’ WHEN ‘B’ DISPLAY ‘GREAT!!!’ WHEN ‘C’ DISPLAY ‘NEED ANY HELP?’ WHEN ‘D’ DISPLAY ‘COME AND SEE ME!’ WHEN ‘F’ DISPLAY ‘SEE YOU NEXT SEMESTER’ [WHEN OTHER DISPLAY ‘WE HAVE A PROBLEM!’] END-EVALUATE.
Problem FILENAME is a 10 character field in the file, FILELIST. FILELIST is a Physical file that contains a record for each of the physical files in QGPL. You are required to write a COBOL program that prints all of the files listed in FILELIST. What COBOL statement would list all of the Files in QGPL that start with ‘CHAP’?
Iteration Looping / repetition
Perform Statement COBOL’s version of the execute sub-routine command. Works as follows: Goto the start of a paragraph. Repeat in sequence, all of the statements in the paragraph as many times as required. UNTIL Clause Return to the calling statement.
PERFORM STATEMENT Basic Format: PERFORM paragraph-name. ==== Perform executes a group of statements within the paragraph ‘paragraph-name’ Then, control comes back to the next statement following the perform
Basic PERFORM READ Emp-File PERFORM DSP-Rtn STOP RUN DSP-RTN move emp-in to emp-out. write dsp-record format is ‘SCREEN’. read dsp-file record.
PERFORM STATEMENT PERFORM HEADER-LINE ======================= PROCEDURE DIVISION. 00-MAIN. OPEN INPUT PAYROLL-FILE OUTPUT PRINT-FILE. READ PAYROLL-FILE AT END MOVE 'N' TO EOF-FLAG END-READ. PERFORM HEADER-LINE PERFORM PROCESS-RECORDS UNTIL EOF-FLAG = 'N'. ======================= HEADER-LINE. MOVE HEADING-LINE TO PRINT-LINE. WRITE PRINT-LINE.
Iteration Statement to Use? Perform Until Tests for the condition first Statements are executed only if the condition is true Perform With Test After tests for the condition last Statements are always executed at least once
PERFORM Until PERFORM paragraph-name UNTIL Condition
PERFORM Until Condition Met? YES NO Execute Program Statements
PERFORM….UNTIL STATEMENT PROCEDURE DIVISION. 00-MAIN. : PERFORM PROCESS-RECORDS UNTIL EOF-FLAG = 'N'. ======================= PROCESS-RECORDS. MOVE EMP-NAME TO DET-NAME MOVE EMP-HOURS TO DET-HOURS COMPUTE DET-PAY = EMP-HOURS * EMP-RATE MOVE DETAIL-LINE TO PRINT-LINE WRITE PRINT-LINE READ PAYROLL-FILE AT END MOVE 'N' TO EOF-FLAG END-READ.
PERFORM Until READ Emp-File AT END Move ‘YES’ TO EOF. PERFORM Dsp-Rtn UNTIL EOF = ‘Y’ STOP RUN DSP-RTN move emp-in to emp-out. write dsp-record format is ‘SCREEN’. read dsp-file RECORD. read emp-file at end move ‘YES’ to EOF.
Iteration Statement to Use? Perform X Times Use this when you know the number of times the paragraph(s) are to be executed.
PERFORM X Times PERFORM (paragraph-name) THROUGH/THRU (paragraph-name) (integer/variable) TIMES
PERFORM X Times Number of Times Met? YES NO Execute Program Statements
PERFORM X Times READ Emp-File AT END Move ‘YES’ TO EOF. PERFORM Dsp-Rtn 5 TIMES STOP RUN DSP-RTN move emp-in to emp-out. write dsp-record format is ‘SCREEN’. read dsp-file record.
PERFORM With Test After PERFORM (paragraph-name) THROUGH/THRU (paragraph-name) WITH TEST AFTER UNTIL Condition
PERFORM With Test After Execute Program Statements Condition Met? YES NO
PERFORM With Test After READ Emp-File AT END Move ‘YES’ TO EOF. PERFORM Dsp-Rtn WITH TEST AFTER UNTIL EOF = ‘Y’ STOP RUN DSP-RTN move emp-in to emp-out. write dsp-record format is ‘SCREEN’. read dsp-file record. read emp-file AT END MOVE ‘YES’ TO EOF.
PERFORMs within PERFORMs READ Emp-File AT END Move ‘YES’ TO EOF. PERFORM DSP-Rtn UNTIL 5 TIMES STOP RUN DSP-RTN MOVE EMP-IN TO EMP-OUT. PERFORM Write-Rtn
Moving Data & Text Manipulation Chapter 6 Moving Data & Text Manipulation
Fun with Functions ILEFUNCT
System Date Function current-date Function convert-date-time
Display file ‘Both’ Fields Input and Output buffers REDEFINES clause Different data specifications for same memory location Screens with both fields need an input and an output buffer.
Used to Concatenate character strings together. STRING Statement Used to Concatenate character strings together.
STRING Statement STRING Variable/Constant DELIMITED BY {Variable/Constant/SIZE) INTO variable END-STRING
STRING Statement Rules DELIMITED BY required Receiving field must be an elementary data (not a group item) element with no editing symbols All literals must be alphanumeric String moves data from left-right but does not pad with blanks
STRING Statement STRING first-name DELIMITED BY ‘ ‘ last-name DELIMITED BY ‘ ‘ INTO full-name END-STRING first-name last-name full-name ‘Cindy‘ ‘Laurin-Moogk’
STRING Statement ‘Cindy ‘ ‘Laurin-Moogk ‘ STRING first-name DELIMITED BY ‘ ‘ ‘ ‘ DELIMITED BY SIZE last-name DELIMITED BY ‘ ‘ INTO full-name END-STRING first-name last-name full-name ‘Cindy ‘ ‘Laurin-Moogk ‘
STRING Statement MOVE ‘XXXXXXXXXXXXXXXXXXXXXXXX’ TO full-name. STRING first-name DELIMITED BY ‘ ‘ ‘ ‘ DELIMITED BY SIZE last-name DELIMITED BY ‘ ‘ INTO full-name END-STRING first-name last-name full-name ‘Cindy ‘ ‘Laurin-Moogk ‘ ‘Cindy Laurin-MoogkXXXXX’
We can concatenate strings together using the String Verb. Wouldn’t it be nice if we could parse a string into other strings?
UNSTRING Verb!! UNSTRING Variable DELIMITED BY Variable/Constant/SIZE INTO variable1 variable2 variable3 END-UNSTRING
UNSTRING 01 Name-In-Pieces. 05 Last-Name PIC X(16). 05 First-Name PIC X(10). 05 Middle-Initial PIC X. 01 Entire-Name PIC X(31) VALUE ‘Cindy M Laurin-Moogk’. UNSTRING Entire-Name DELIMITED BY ‘ ‘ INTO First-Name Middle-Initial Last-Name.
UNSTRING 01 Name-In-Pieces. 05 Last-Name PIC X(16). 05 First-Name PIC X(10). 05 Middle-Initial PIC X. 01 Entire-Name PIC X(31) VALUE ‘Eddie Laurin’. UNSTRING Entire-Name DELIMITED BY ‘ ‘ INTO First-Name Middle-Initial Last-Name.
UNSTRING 01 Name-In-Pieces. 05 Last-Name PIC X(16). 05 First-Name PIC X(10). 05 Middle-Initial PIC X. 01 Entire-Name PIC X(31) VALUE ‘Cindy M Laurin Moogk’ UNSTRING Entire-Name DELIMITED BY ‘ ‘ INTO First-Name Middle-Initial Last-Name.
Next Class Work on Assignment 1 Do your readings per the text book. Due end of this week!!!!!! Do your readings per the text book.