COBOL Ch5-6

  1. A   PIC 99 VALUE 12
    B   PIC 99V9  VALUE 345
    C   PIC 99V99 VALUE 4712
    ADD B TO A
    ADD A TO B
    ADD B TO C
    ADD C TO B
    • ADD B TO A: 46
    • ADD A TO B: 46V5
    • ADD B TO C: 81V62
    • ADD C TO B: 81V6
  2. A   PIC 99 VALUE 12
    B   PIC 99V9  VALUE 345
    C   PIC 99V99 VALUE 4712
    ADD C TO A
    ADD A TO C
    • ADD C TO A: 49
    • ADD A TO C: 49V12
  3. A PIC 9V99 VALUE 123
    B PIC 9V99 VALUE 456
    C PIC 9V9  VALUE 0
    ADD A B GIVING C
    ADD A B GIVING C ROUNDED
    • ADD A B GIVING C: 5V7
    • ADD A B GIVING C ROUNDED: 5V8
  4. PART B
    SENDING PIC 9(6) data 002178
    REC. PIC ***,***
    SENDING PIC 9(6) data 120183 REC. PIC 99B99B99
    SENDING PIC 9(6) data 120183 REC. PIC 99/99/99
    • **2,178
    • 12 01 83
    • 12/01/83
  5. PART C
    send PIC 999V99 VALUE 12345 rec PIC 999.99
    send PIC 999V99 VALUE 02345 rec 999.9
    send PIC 999V99 VALUE 51234 rec 99.99
    send PIC 999 VALUE 456 rec 999.99
    • 123.45
    • 023.4
    • 12.34
    • 456.00
  6. PART D
    send PIC S999 VALUE -123 rec PIC -999
    send PIC S999 VALUE -123 rec PIC 999-
    send PIC S999 VALUE +123 rec PIC -999
    • -123
    • 123-
    • _123
  7. PART D
    send PIC S9(5) VALUE +12345 rec PIC +9(5)
    send PIC S9(3) VALUE -123 rec PIC +9(3)
    send PIC S9(3) VALUE -123 rec PIC 999+
    • +12345
    • -123
    • 123-
  8. PART E
    send PIC S9(4) VALUE +1234 rec PIC 9(4)CR
       VALUE -1234 rec PIC 9(4) CR
       VALUE +1234 rec PIC 9(4) DB
       VALUE -1234 rec PIC 9(4) DB
    rec PIC
    • 1234_ _
    • 1234CR
    • 1234_ _
    • 1234DB
  9. PART E
    send PIC 9(4) VALUE 1234 rec PIC$99999
    send PIC 9(4) VALUE 000 rec PIC $ZZZZZ
    • $01234
    • $_ _ _ _ _
  10. PART F
    send PIC 9(4) VALUE 0000 rec PIC $$,$$9.99
    send PIC 9(4) VALUE 0080 rec PIC $$,$$9.00
    send PIC 9(4) VALUE 0128 rec PIC $$,$$9.99
    send PIC 9(5) VALUE 57397 rec PIC $$,$$9
    • $0.00
    • $80.00
    • $128.00
    • $7,397
  11. PART F
    send PIC S9(4) VALUE -0005 rec PIC ++++9
    send PIC S9(4) VALUE +0080 rec PIC ++++9
    send PIC S9(4) VALUE -0080 rec PIC ----9
    send PIC S9(5) VALUE +71234 rec PIC ----9
    • bbb-5
    • bb+80
    • bb-80
    • b1234
  12. part g
    send PIC  9(5)
    VALUE 12345 rec PIC ZZ,999
    VALUE 02345 rec PIC ZZ,999
    VALUE 00123 rec PIC ZZ,999
    VALUE 00012 rec PIC ZZ,999
    VALUE 05678 rec PIC **,**9
    VALUE 00567 rec PIC **,**9
    VALUE 00000 rec PIC **,***
    • 12,345
    • b1,234
    • bbb123
    • bbb012
    • *5,678
    • ***567
    • ******
  13. Record Buffers
    • Remember records are read from the file one record at a time.
    • The computer uses the programmers description of the record (the FD record in theData Division) to set aside sufficient memory to store one instance of the record.
    • Memory allocated for storing a record is called a “record buffer”
  14. Implications of Buffers
    • You have one record buffer (FD record) for each file.
    • To transfer a record from an input file to an output file we will have to
    • – read the record into the input record buffer (FD for the input file)
    • – move it to the output record buffer (FD for the output file)
    • – write the data to the output file from the output record buffer
  15. Select and Assign
    • SELECT FileName ASSIGN TO Ex-FileRef
    •   ORGANIZATION IS {LINE, RECORD} SEQUENTIAL.
    • LINE SEQUENTIAL means each record is followed by the carriage return and line feedcharacters. This is standard for PC based files.
    • RECORD SEQUENTIAL means that the file consists of a stream of bytes. Only the fact that we know the size of each record allows us to retrieve them. This is standard on a mainframe.
  16. OPEN and CLOSE syntax
    • OPEN INPUT F01-STUDENT-FILE
    •          OUTPUT F02-PRINT-FILE
    • CLOSE F01-STUDENT-FILE
    •           F02-PRINT-FILE
  17. READ syntax
    • READ F01-STUDENT-FILE
    •         AT END MOVE 'NO' TO
    •                W01-DATA-REMAINS-SWITCH
    • END-READ

    • READ InternalFilename
    •     INTO Identifier
    •     AT END StatementBlock
    •     NOT AT END StatementBlock
    • END-READ
  18. WRITE syntax
    • MOVE DETAIL-LINE TO PRINT-LINE
    • WRITE PRINT-LINE
    • or
    • WRITE PRINT-LINE FROM DETAIL-LINE

    • WRITE recordname [FROM identifies]
    •     [{BEFORE, AFTER} ADVANCING
    •        {num [LINES,LINES]...
  19. Line Spacing: To create a blank line before the total line:
    • On the PC, write a blank record
    • WRITE PRINT-LINE FROM SPACES
    • WRITE PRINT-LINE FROM TOTAL-LINE
    • On the mainframe, use the AFTER ADVANCING clause.
    • WRITE PRINT-LINE FROM TOTAL-LINE
    • AFTER ADVANCING 1 LINE
  20. Read a ...
    Read a File, Write a Record

    • OPEN, CLOSE and READ a “file-name”
    • ex READ STUDENT-FILE (declared with FD in file section)
    • WRITE a “record-name”
    • ex WRITE STUDENT-RECORD (declared with 01 in file section)
  21. ROUNDED
    The Rounded option causes decimals higher than 5 to round up, others are rounded down. If you leave out the rounded clause, Cobol truncates any extra decimals!
  22. ON SIZE ERROR
    On Size Error is used to protect against a calculation that results in a field that is too small to fit the number. A size error condition exists when, after decimal point alignment, the result is truncated on either the left or the right hand side.
  23. Additional Data Types
    • Alphabetic Fields: PIC A.
    •     Can only contain letters and space.
    • Alphanumeric Fields: PIC X.
    •     Can contain anything, numbers, letters, special characters.
    • Signed Fields: PIC S9V9.
    •      Sign is optional, if the sign is missing, the number will always be positive.
    •     Use “S” at the beginning of the picture to indicate a sign is needed.
  24. Numeric Edited
    • Special set of editing characters allowed in the picture.
    • PIC 9,999.9 indicates a 5 digit number, with one digit after the decimal and a comma between the thousands and hundreds digit.
    • This variable requires 7 bytes of memory. The comma and the decimal point plus 5digits.
    • It is a text field.
  25. Moving Rules
    • When receiving field is smaller:
    • the MOVE truncates
    • -text by dropping trailing character (right side).
    • – numeric by dropping high order (left side).
    • When receiving field is larger:
    • Cobol pads
    • – text by adding spaces on the right.
    • – numeric by adding zeros on leading integers and trailing decimals .
  26. send PIC X(5) value ABCDE rec PIC X(5)
    send PIC X(5) value ABCDE rec PIC X(4)
    send PIC X(5) value ABCDE rec PIC X(6)
    • ABCDE
    • ABCD
    • ABCDEb
  27. send PIC 9(5) value 12345 rec PIC 9(5)
    send PIC 9(5) value 12345 rec PIC 9(4)
    send PIC 9(5) value 12345 rec PIC 9(6)
    send PIC 9(3)V99 value 12345 rec PIC 9(3)
    send PIC 9(3)V99 value 12345 rec PIC 9V99
    send PIC 9(3) value 123 rec PIC 9(3)V99
    • 12345
    • 2345
    • 012345
    • 123
    • 3v45
    • 123v00
  28. Standard IF Statement
    • IF conditional-phrase
    •     true action statements  (CONTINUE)
    • ELSE
    •     false action statements
    • END-IF
  29. Implied Condition Example
    IF W01-CODE = ‘1’ OR W01-CODE = ‘5’
        ADD 10 TO TOTAL
    END-IF
    can be written as
    • IF W01-CODE = ‘1’ OR ‘5’
    •     ADD 10 TO TOTAL
    • END-IF
  30. IF Order of Precedence
    • All AND are done first , one at a time, left to right.
    • OR is done second, left to right.
    • Use brackets to override.
    • Anything inside brackets is done first.
  31. EVALUATE ex 1
    • EVALUATE W01-STU-MARK
    •     WHEN >= 85
    •         MOVE “A” TO W02-STU-GRADE
    •     WHEN >= 75
    •         MOVE “B” TO W02-STU-GRADE
    •     WHEN >= 66
    •         MOVE “C” TO W02-STU-GRADE
    •     WHEN >= 60
    •         MOVE “D” TO W02-STU-GRADE
    •     WHEN OTHER
    •         MOVE “F” TO W02-STU-GRADE
    • END-EVALUATE
  32. EVALUATE ex 2
    • EVALUATE TRUE
    •     WHEN W01-STU-MARK >= 85
    •         MOVE “A” TO W02-STU-GRADE
    •     WHEN W01-STU-MARK >= 75
    •         MOVE “B” TO W02-STU-GRADE
    •     WHEN W01-STU-MARK >= 66
    •         MOVE “C” TO W02-STU-GRADE
    •     WHEN W01-STU-MARK >= 60
    •         MOVE “D” TO W02-STU-GRADE
    •     WHEN OTHER
    •         MOVE “F” TO W02-STU-GRADE
    • END-EVALUATE
  33. EVALUATE ex 3
    • EVALUATE W01-DENTAL = ‘Y’ ALSO  W01-FAMILY = 'Y'
    •    WHEN TRUE ALSO TRUE
    •         MOVE 90 TO W02-DEDUCTION
    •     WHEN TRUE ALSO FALSE
    •         MOVE 45 TO W02-DEDUCTION
    •     WHEN FALSE ALSO ANY
    •         MOVE 0 TO W02-DEDUCTION
    • END-EVALUATE
  34. Formatting Data
    • Edited Pictures are PICTURE clauses which format data intended for output to the screen or printer. In other words, they turn raw data in to pretty data.
    • The additional symbols are referred to as “Edit Symbols”.
    • Picture clauses which include edit symbols are called “Edited Pictures”.
    • Edited items cannot be used in a computation.
    • COBOL provides two basic types of editing
    • -Insertion Editing modifies a value by including additional items, such as a comma.
    • -Suppression and Replacement Editing suppresses and replaces leading zeroes.
  35. Insertion Characters
    • / 0 B
    • Insertion characters are / 0 and B. They add additional column(s). Note that the hyphen is notan insertion character. It cannot use it for SINs or dates. If you want to insert characters thatare not insertion characters, use the INSPECT or STRING command.
  36. * Symbol
    * is used to fill blanks between a $ sign and a number. A cheque amount is often written as $**3,000.00. This prohibits fraud. You can’t add in a 5 to change $3,000 in to $53,000
  37. $
    tells Cobol to place a $ in the output. Repeating dollars signs indicate that a $sign should go immediately to the left of the number.
  38. Z
    • If a Z is used and the number to display is:
    • non zero, then the number is displayed.
    • zero, then a blank is displayed.
  39. Issues With Blanks
    Variable W01-TUITION is PIC 9(6) and contains 003000.
    PIC $$$999
    PIC X(6)
    PIC 9(6)
    ZZZ999
    • PIC $$$999: B$3000
    • PIC X(6): BB3000
    • PIC 9(6): 003000
    • ZZZ999: BB3000
  40. Issues With Zeros
    PIC ZZ9.99
    PIC ZZZ.99
    PIC ZZZ.Z9
    PIC ZZZ.99
    • bb0.00
    • bbb.00
    • bbbbb0
    • bbbbbb
  41. Plus and Minus Signs
    • The repetition of a plus or minus sign denotes a sign which shows immediately to the left of the number.
    • The receiving field must be at least one character longer than the sending field to accommodate the sign.
    • A plus sign tells Cobol to display the sign of the edited number when the number is positive, negative or zero.
    • Specification of a minus sign tells Cobol to display the sign only when negative.
  42. Common Errors
    • Missing periods – data definition, paragraph names and end of paragraph MUST have a period, error is assigned to next line when missing.
    • Mainframe compiler must have a space before and after all relational and arithmetic operators.
    • Conflicting picture and value clause. Invalid picture clause.
    • RECORD CONTAINS number of characters conflicts with FD record description.
    • Read a file, write a record.
    • Invalid character in numeric field – variable is defined as numeric but has character data or spaces in it. this error may not occur until you attempt to perform a calculation.
  43. Extra Periods
    • Periods in the procedure division are only needed to end the paragraph name or end the paragraph.
    • A period will terminate all “logical conditions” (IF , PERFORM UNTIL, AT END),including all nested conditions.
    • To prevent logic errors, avoid extra periods and use the proper scope delimiter (END-IF, END-PERFORM, END-READ).
Author
slc53
ID
329660
Card Set
COBOL Ch5-6
Description
COBOL Ch5-6
Updated