COP 2121 Some Basic Cobol Verbs

From In The Wings
Jump to navigation Jump to search

The DISPLAY Verb

  • Used to display several data items or literals or any combination of these
  • The WITH NO ADVANCING clause suppresses the carriage return/line feed
IDENTIFICATION DIVISION.
PROGRAM-ID. DisplayOption.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 	Int-1		PIC 999 		VALUE 100.
01 	Alph-1  	PIC XXX 	VALUE “ABC”

PROCEDURE DIVISION.
MAIN-ROUTINE.
	DISPLAY Int-1
	DISPLAY Alpha-1
	DISPLAY “Demonstrating NO ADVANCING  Option”
	DISPLAY Int-1 WITH NO ADVANCING
	DISPLAY Alpha-1
	DISPLAY Alpha-1
        STOP RUN.

The ACCEPT Verb

  • Gets the user response and stores the data in the memory allocated for the Identifier in the WS section
IDENTIFICATION DIVISION.
PROGRAM-ID.  AcceptAndDisplay.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 StudentDetails.
   02  StudentName.
       03 Surname      		PIC X(8).
       03 Initials    	 	PIC XX.
   02  StudentId       		PIC 9(7).
   02  CourseCode      		PIC X(4).
   02  Section           		PIC 9(4).
   02  Gender          		PIC X.

01 CurrentDate.
   02  CurrentYear     		PIC 99.
   02  CurrentMonth    		PIC 99.
   02  CurrentDay      		PIC 99.

01 DayOfYear.
   02  FILLER          		PIC 99.
   02  YearDay         		PIC 9(3).

01 CurrentTime.
   02  CurrentHour     		PIC 99.
   02  CurrentMinute   		PIC 99.
   02  FILLER         		PIC 9(4).
PROCEDURE DIVISION.
Begin.
    DISPLAY "Enter student details using template below".
    DISPLAY "NNNNNNNNNNSSSSSSSCCCCGGGGS                ".
    ACCEPT  StudentDetails.
    ACCEPT  CurrentDate FROM DATE.
    ACCEPT  DayOfYear FROM DAY.
    ACCEPT  CurrentTime FROM TIME.
    DISPLAY "Name is ", Initials SPACE Surname.
    DISPLAY "Date is " CurrentDay SPACE CurrentMonth SPACE CurrentYear.
    DISPLAY "Today is day " YearDay " of the year".
    DISPLAY "The time is " CurrentHour ":" CurrentMinute.
    STOP RUN.

The MOVE Verb

  • Copies data from the Identifier-1 or literal to one or more destination identifiers
  • The source and destination identifiers can be group or elementary data items

MOVEing Data to Alphanumeric Item

  • When the destination item is alphanumeric or alphabetic (PIC X or A) data is copied into the destination area from left to right
    • If receiving field is longer, low-order (right-most) positions are replaced with spaces
    • If receiving field is shorter, low-order characters in sending field are truncated

MOVEing Data to Numeric Item

  • Data is aligned along the decimal point
    • When the decimal point is not explicitly specified in either the source or destination items, the item is treated as if it had an assumed decimal point immediately after its rightmost character
  • Digits in integer part moved right to left starting at decimal point
    • Unfilled high-order (leftmost) integer positions filled with zeros if receiving field has more integer positions than sending field
    • High-order digits truncated if receiving field has fewer integer digits than sending field
  • Digits in decimal part moved left to right starting at decimal point
    • Unfilled low-order decimal positions filled with zeros if receiving field has more decimal positions than sending field
    • Low-order digits are truncated if receiving field has fewer decimal positions than sending field

Group Move

  • When receiving field is a group item, alphanumeric MOVE rules are followed
  • If subfields are numeric, invalid data may be stored in subfields

Recommendations

  • Avoid using sending field with different data type than receiving field
  • Move numeric fields, numeric literals, or ZEROS to numeric fields
  • Move alphanumeric fields, nonnumeric literals, ZEROS or SPACES to alphanumeric fields

Arithmetic Verbs

  • ADD, SUBTRACT, MULTIPLY, DIVIDE
  • All require fields operated on to
    • Have numeric PICTURE clause
    • Contain numeric data when statements are executed
  • When GIVING phrase is not used
    • The identifiers after the word TO, FROM, BY, INTO both contribute to the result and receive the result
  • WHEN GIVING phrase is used
    • The values of the identifiers to the left of GIVING phrase are left intact after the operation and result is moved to the Identifier after the GIVING phrase
  • Only numeric identifiers or literals can be used
    • Only the identifiers to the right of reserved word GIVING can have edited symbols
  • Result is moved into a receiving data-items according to the rules of numeric move

The ROUNDED Option

  • When this option is used, the result is rounded to fit the receiving data-item
  • Otherwise it is truncated

The ON SIZE ERROR Option

  • A size error condition exists when, after decimal point alignment, the result is truncated on either the left or the right hand side of the decimal point
  • If ROUNDED option is used, then a size error only occurs if there is truncation on the left hand side of the decimal point

What Happens if SIZE ERROR Occurs

  • The computer does not perform the operation but instead executes the statements in SIZE ERROR clause
  • Example
ADD amt1 amt2 TO amt3 GIVING total
  SIZE ERROR MOVE ZEROS TO total
END-ADD

Multiply Rules

  • Only two operands can be multiplied
  • To obtain product of 3 operands requires two instructions

The COMPUTE Verb

  • Arithmetic expression evaluated according to normal arithmetic rules from left to right following precedence rules
  • Precedence rules:
  1. Brackets
  2. Power
  3. Multiply, divide
  4. Addition, subtraction

Edited Pictures

  • "Edit Symbols" are used in PICTURE clause to make data fields more readable
    • e.g. Display 001495 as $14.95
  • PICTURE clauses which include edit symbols are called "Edited Pictures"
  • Edit symbols have the effect of changing, or editing, the data inserted into the edited item
  • Edited items can not be used as operands in a computation but they may be used as the result or destination of a computation (i.e. to the right of the word GIVING)

Example: Printing Decimal Point

  • Operation: Move In-Amt to Out-Amt
Field Positions Picture Contents
In-Amt 99V99 12^34
Out-Amt 99.99 12.34
  • DISPLAY In-Amt
    • Will display 1234
  • DISPLAY Out-Amt
    • Will display 12.34

Edit Types

  • COBOL provides two basic types of editing
  1. Insertion Editing - Modifies a value by including additional items
  2. Suppression and Replacement Editing - Suppresses and replaces leading zeros
Edit Symbols
Edit Symbol Editing Type
, B 0 / Simple Insertion
. Special Insertion
+ - CR DB $ Fixed Insertion
+ - $ Floating Insertion
Z * Suppression and Replacement

De-Editing

01 Amt-in      PIC 9.99
ACCEPT Amt-in
  • Can not use Amt-in in arithmetic operations
  • Solution:
01 Amt         PIC 9V99
MOVE Amt-in Amt
ADD 10 TO Amt

COP 2121 IF Statements