any questions? agenda level 77 initialize display & accept arithmetic verbs compute statement...

48
Any Questions?

Upload: elinor-heath

Post on 17-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

Any Questions?

Page 2: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

Agenda

• Level 77

• Initialize

• Display & Accept

• Arithmetic Verbs

• Compute statement

• String/Unstring

• Inspect

Page 3: 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).

Page 4: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

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?

Page 5: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

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?

Page 6: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

Initialize Verb

INITIALIZE variable1 variable2 variable3 …

ALPHABETIC

REPLACING ALPHANUMERIC

NUMERIC

ALPHANUMERIC-EDITED

NUMERIC-EDITED

BY Variable4

literal-1

Page 7: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

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?

Page 8: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

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?

Page 9: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

Display & Accept

• Fast Interactive Screens

• Use of message queue / command line

Page 10: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

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.

Page 11: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

ACCEPT

ACCEPT variable1

AT LINE 99 COLUMN 99.

Examples:

DISPLAY ‘Press Enter to Continue’.

ACCEPT ENTER-VARIABLE.

Page 12: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

Arithmetic Verbs

Add, Subtract, Multiply, Divide

And Compute

Page 13: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

Add Verb

• Three Formats:

ADD variable/constant TO variable

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

ADD variable/constant TO variable/constant GIVING variable

Page 14: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

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)?

Page 15: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

SUBTRACT Verb

• Two Formats:

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

SUBTRACT Variable/Constant Variable/Constant … FROM Variable

GIVING Variable

Page 16: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

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 =

Page 17: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

MULTIPLY Statement

• More restrictive

• Two Formats

MULTIPLY variable/constant BY variable

MULTIPLY variable/constant

BY variable/constant

GIVING variable

Page 18: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

MULTIPLY Statement

A = 50 B = 100 C = 20

MULTIPLY A BY B

A = B = C =

MULTIPLY A BY B GIVING C

A = B = C =

Page 19: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

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

Page 20: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

DIVIDE Statement

A = 100 B = 50 C = 25

DIVIDE A INTO B

A = B = C =

DIVIDE A BY B GIVING C

A = B = C =

Page 21: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

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!

Page 22: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

REMAINDER Clause

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

DIVIDE A INTO B GIVING C REMAINDER D.

A = B = C = D =

Page 23: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

Clauses that work with all arithmetic statements.

ROUNDED

ON SIZE ERROR

Page 24: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

Rounding

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

• Eg. DIVIDE A INTO B GIVING C ROUNDED.

Page 25: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

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.

Page 26: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

Compute Statement

Combines all of the math verbs plus exponentiation

Page 27: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

Compute Statement Symbols

Symbol Meaning

+ ADD

- SUBTRACT

* MULTIPLY

/ DIVIDE

** exponentiation

Page 28: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

Order of Operations

• Operations in brackets ( ) are performed first

• ** (exponentiation)

• * or / (from left to right)

• + or - (from left to right)

Page 29: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

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?

Page 30: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

73

Page 31: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

STRING Statement

Used to Concatenate character strings together.

Page 32: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

STRING Statement

STRING

Variable/Constant DELIMITED BY {Variable/Constant/SIZE)

INTO variable

END-STRING

Page 33: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

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

Page 34: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

STRING Statement

STRING first-name DELIMITED BY ‘ ‘

last-name DELIMITED BY ‘ ‘

INTO full-name

END-STRING

first-name last-name full-name

‘Cindy ‘ ‘Laurin’

Page 35: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

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 ‘

Page 36: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

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 ‘

Page 37: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

We can concatenate strings together using the String Verb.

Wouldn’t it be nice if we could parse a string into other strings?

Page 38: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

UNSTRING Verb!!

UNSTRING

Variable DELIMITED BYVariable/Constant/SIZE

INTO variable1 variable2 variable3

END-UNSTRING

Page 39: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

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.

Page 40: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

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.

Page 41: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

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.

Page 42: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

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

Page 43: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

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

We would like to print it as

999-999-999

Page 44: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

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.

Page 45: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

Inspect Verb

• Used to replace one character or character string with another

INSPECT variable REPLACING

{ALL}

{LEADING} var/lit by var/lit

{FIRST}

Page 46: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

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

Page 47: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

Printing with Externally Described Printer Files

• New addition to the select statement – FORMATFILE!

select report-file

assign to formatfile-filename.

Page 48: Any Questions? Agenda Level 77 Initialize Display & Accept Arithmetic Verbs Compute statement String/Unstring Inspect

Writing to an Externally Described Printer File

Write record-name

format is ‘RECORD’

eop perform print-heading

End-write.

(eop means end of page)