Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement...

Preview:

Citation preview

Any Questions?

Agenda

• Level 77

• Initialize

• Display & Accept

• Arithmetic Verbs

• Compute statement

• String/Unstring

• Inspect

Level 77• Used to define stand alone elementary

items.

• No limit to the “number of” used in a program.

• Exclusively used in the

WORKING-STORAGE SECTION

ie. 77 ws-counter pic s9(5).

77 ws-alpha-work pix x(30).

Initialize Verb01 Group-Item-1.

05 Num1 PIC 9(4) VALUE 9.

05 Num2 PIC 9(4) VALUE 35.

05 Alpha1 PIC X(10) VALUE ‘COBOL’.

05 Alpha2 PIC X(20) VALUE ‘FUN’.

INITIALIZE NUM1.

INITIALIZE ALPHA1 NUM2.

What happens?

Initialize Verb01 Group-Item-1.

05 Num1 PIC 9(4) VALUE 9.

05 Num2 PIC 9(4) VALUE 35.

05 Alpha1 PIC X(10) VALUE ‘COBOL’.

05 Alpha2 PIC X(20) VALUE ‘FUN’.

INITIALIZE Group-Item-1.

What happens?

Initialize Verb

INITIALIZE variable1 variable2 variable3 …

ALPHABETIC

REPLACING ALPHANUMERIC

NUMERIC

ALPHANUMERIC-EDITED

NUMERIC-EDITED

BY Variable4

literal-1

Initialize Verb01 Group-Item-1.

05 Num1 PIC 9(4) VALUE 9.

05 Num2 PIC 9(4) VALUE 35.

05 Alpha1 PIC X(10) VALUE ‘COBOL’.

05 Alpha2 PIC X(20) VALUE ‘FUN’.

INITIALIZE Group-Item-1 REPLACING ALPHANUMERICS BY ‘X’

NUMERIC BY 9

What happens?

Initialize Verb01 Group-Item-1.

05 Num1 PIC 9(4) VALUE 9.

05 Num2 PIC 9(4) VALUE 35.

05 Alpha1 PIC X(10) VALUE ‘COBOL’.

05 Alpha2 PIC X(20) VALUE ‘FUN’.

INITIALIZE Group-Item-1 REPLACING NUMERICS BY 100.

What happens?

Display & Accept

• Fast Interactive Screens

• Use of message queue / command line

Display

DISPLAY variable1/literal1 variable2/literal2…

AT LINE 99 COLUMN 99.

Examples:DISPLAY STUDENT-RECORD.DISPLAY ‘Amount: ’ TRANS-AMOUNT.DISPLAY ‘Amount: ’ TRANS-AMOUNT AT LINE 8 COLUMN 10.

ACCEPT

ACCEPT variable1

AT LINE 99 COLUMN 99.

Examples:

DISPLAY ‘Press Enter to Continue’.

ACCEPT ENTER-VARIABLE.

Arithmetic Verbs

Add, Subtract, Multiply, Divide

And Compute

Add Verb

• Three Formats:

ADD variable/constant TO variable

ADD variable/constant, variable/constant, … GIVING variable

ADD variable/constant TO variable/constant GIVING variable

ADD Verb

A = 100 B = 50 C = 300

ADD A to B

A = B = C =

ADD A to B GIVING C

A = B = C =

What would happen if C was PIC 9(2)?

SUBTRACT Verb

• Two Formats:

SUBTRACT Variable/Constant Variable/Constant ... FROM Variable

SUBTRACT Variable/Constant Variable/Constant … FROM Variable

GIVING Variable

SUBTRACT Statement

A = 100 B = 50 C = 150 D = 200

SUBTRACT A B FROM C

A = B = C = D =

SUBTRACT A B FROM C GIVING D

A = B = C = D =

MULTIPLY Statement

• More restrictive

• Two Formats

MULTIPLY variable/constant BY variable

MULTIPLY variable/constant

BY variable/constant

GIVING variable

MULTIPLY Statement

A = 50 B = 100 C = 20

MULTIPLY A BY B

A = B = C =

MULTIPLY A BY B GIVING C

A = B = C =

DIVIDE StatementMore restrictive - Three Formats - Decimal alignment

DIVIDE variable/constant INTO variable

DIVIDE variable/constant

INTO variable/constant

GIVING variable

DIVIDE variable/constant

BY variable/constant

GIVING variable

DIVIDE Statement

A = 100 B = 50 C = 25

DIVIDE A INTO B

A = B = C =

DIVIDE A BY B GIVING C

A = B = C =

REMAINDER Clause• Two formats:DIVIDE variable/constant INTO variable/constant GIVING

variable

REMAINDER variable

DIVIDE variable/constant BY variable/constant GIVING variable

REMAINDER variable

DECIMAL ALIGNMENT!

REMAINDER Clause

A = 100 B = 150 C = 20 D = 10

DIVIDE A INTO B GIVING C REMAINDER D.

A = B = C = D =

Clauses that work with all arithmetic statements.

ROUNDED

ON SIZE ERROR

Rounding

• Add the ROUNDED option to the math statement directly following the resulting field

• Eg. DIVIDE A INTO B GIVING C ROUNDED.

ON SIZE ERROR option

• What to do if the resulting value from the Mathematical Expression is too big for the result variable leading or trailing the decimal position.

• Used to Resolve Divide by Zero errors.

ADD A B GIVING C

ON SIZE ERROR

PERFORM ERROR-HANDLING-ROUTINE.

Compute Statement

Combines all of the math verbs plus exponentiation

Compute Statement Symbols

Symbol Meaning

+ ADD

- SUBTRACT

* MULTIPLY

/ DIVIDE

** exponentiation

Order of Operations

• Operations in brackets ( ) are performed first

• ** (exponentiation)

• * or / (from left to right)

• + or - (from left to right)

Compute Statement

A = 10 B = 2 C = 60 D is PIC 9(4)

COMPUTE D ROUNDED =

A ** B / ( A - B) + C.

What is the value of D?

73

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 doesnot 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’

STRING Statement

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 ‘

STRING StatementMOVE ‘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 ‘

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 BYVariable/Constant/SIZE

INTO variable1 variable2 variable3

END-UNSTRING

UNSTRING01 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’.

UNSTRING Entire-Name DELIMITED BY ‘ ‘

INTO First-Name Middle-Initial Last-Name.

UNSTRING01 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.

UNSTRING01 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 ‘ ‘ or ‘-’INTO First-Name Middle-Initial Last-Name.

Substring(nn:nn)position, length

01 whole-name pic x(50).

01 first-name pic x(25).

move whole-name(1:25) to first-name

starts at the first character of whole name and retrieves 25 characters

01 Student-Number PIC 9(9) Comp-3.

We would like to print it as

999-999-999

The Long Way

File-Section. 05 student-number pic 9(9) comp-3.

Working-Storage.01 student-number-unpacked pic 9(9).01 Student-number-edited redefines student-number-unpacked.. 05 part1 pic 999. 05 part2 pic 999. 05 part3 pic 999.01 detail-line. 05 prt-part1 pic 999. 05 pic X value ‘-’. 05 prt-part2 pic 999. 05 pic X value ‘-’. 05 prt-part3 pic 999.

Inspect Verb

• Used to replace one character or character string with another

INSPECT variable REPLACING

{ALL}

{LEADING} var/lit by var/lit

{FIRST}

Print Student Number with ‘-’ using Inspect

File-Section. 05 student-number pic 9(9) comp-3.

Working-Storage.01 detail-line. 05 prt-student-number pic 999B999B999. Procedure Division.

inspect prt-student-number replacing all ‘ ‘ by ‘-’.

Printing with Externally Described Printer Files

• New addition to the select statement – FORMATFILE!

select report-file

assign to formatfile-filename.

Writing to an Externally Described Printer File

Write record-name

format is ‘RECORD’

eop perform print-heading

End-write.

(eop means end of page)

Recommended