any questions? week 1 - 2nd lecture intro to cobol programming defining files and processing data

65
Any Questions?

Upload: angelica-simpson

Post on 19-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

Any Questions?

Page 2: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

Week 1 - 2nd Lecture

Intro to COBOL Programming

Defining Files and Processing Data

Page 3: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

What is COBOL?

• Designed by the US defense department

• Meant to be English like for ease of maintenance

COmmon Business Oriented Language

Page 4: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

Agenda

• How does a compiler work?

• Creating a COBOL Program

• Parts of a COBOL Program

• The Perform Statement

• Defining Files

• Defining Variables

• COBOL Verbs

Page 5: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

Compilers

• Translate programs written by humans into Machine Language Code

directly executable code

vs

2 step translate and execute programs

ie Basic, C, C++, HTML etc.

Page 6: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

COBOL Compiler on the iSeries

COBOL Compiler

Source Code

QCBLLESRC(PGM1)

CompiledListing(PGM1)

Program Object

(PGM1)

Page 7: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

Steps to Create a COBOL Program

Working in the Source Physical File – QCBLLESRC (created previously)

1. Create a Source Member in the Source Physical file with the type CBLLE

2. Type in the COBOL code syntax statements using an editor i.e. RDS, SEU

3. Save & Exit

4. Compile and generate the executable code

5. Repeat 1 through 4 for each program required

Page 8: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

8

Defining Files and Processing Data

Cobol Program Structure

Common Elements

Page 9: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

9

A source program consists of

FOUR DIVISIONS

PROCEDURE DIVISION.

•Instructing the computer to work with the data

IDENTIFICATION DIVISION.

•Identifying the program

ENVIRONMENT DIVISION.

•Defining the files, peripherals, etc.

DATA DIVISION.

•Defining the variables to use in the program

Page 10: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

10

Identifying The Program

PT001F

Payroll Records

PAYREP

The Cobol Program

PRINTER FILE

Payroll Report

Page 11: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

11

IDENTIFICATION DIVISION. PROGRAM-ID. PAYREP. AUTHOR. Barake, Juan - DB344E40. * TO PRINT PAYROLL REPORT

Column 7Comment LinesIgnored by the Compiler

Column 8

To start Division, Paragraph

Page 12: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

Identification Division

• Required– Name of the Program in the Program-ID

paragraph.

• Optional– Author– Installation– Date-Written– etc.

Page 13: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

Identification Division(Syntax)

IDENTIFICATION DIVISION.

PROGRAM-ID. program-name.

[AUTHOR. [comment-entry] … ]

[INSTALLATION. [comment-entry] …]

[DATE-WRITTEN. [comment-entry] …]

[DATE-COMPILED. [comment-entry] …]

[SECURITY. [comment-entry] …]

Page 14: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

Environment Division

Defines the operational environment and connecting a programs internal

file / device requirements to the physical system environment.

Page 15: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

Environment Division

[ENVIRONMENT DIVISION.][CONFIGURATION SECTION.]

Identifies the computer used to write / create the program and the computer it is intended to operate / execute on.

[INPUT-OUTPUT SECTION.][FILE-CONTROL.]

Connects the programs internally declared devices / files to the operating system devices / files.

Page 16: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

Environment Division(Syntax)

[ENVIRONMENT DIVISION.][CONFIGURATION SECTION.][SOURCE-COMPUTER. AS/400.][OBJECT-COMPUTER. AS/400.]

[INPUT-OUTPUT SECTION.][FILE-CONTROL.]

[SELECT nice-file-nameASSIGN to DISK/PRINTER/WORKSTATION- physical-

file-name.]

Page 17: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

Environment Division

• INPUT-OUTPUT SECTION is only required if your program uses files.– Contains the FILE-CONTROL paragraph

• DISK

• PRINTER

• DATABASE

• WORKSTATION

• FORMATFILE

– similar to setting up file pointers

Page 18: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

18

THE ENVIRONMENTThe Data File

PT001F

Payroll Records

PAYREP

The Cobol Program

PRINTER FILE

Payroll Report

Page 19: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

19

ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT PAYROLL-FILE ASSIGN TO DATABASE-PT001F ORGANIZATION IS SEQUENTIAL.

As known to the AS400 File System

As known to the Cobol Program

Syntax can use up to 30 char names AS400 system only 10

Easier to maintain links internal cobol structure to system

Page 20: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

Select for a Database File

Select Product-File

Assign to disk-PRODPF.

Select Product-file

Assign to database-PRODPF

Access is Sequential.

Page 21: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

21

ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT PAYROLL-FILE ASSIGN TO DATABASE-PT001F ORGANIZATION IS SEQUENTIAL.

•Data File = Collection of Records

•Each Record = One employee Info

•Records have same length (bytes)

•Records made up of fields

•Sequential means that records are organized in arrival sequence

Page 22: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

Select Statement for a Printer (Spooled) File

Select Product-Report

assign to printer-qprint.

Select Product-Report

assign to printer-qsysprt.

Page 23: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

23

ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT PAYROLL-FILE ASSIGN TO DATABASE-PT001F ORGANIZATION IS SEQUENTIAL. SELECT PRINT-FILE

ASSIGN TO PRINTER.

•PRINT-FILE is just a user-defined name

•PRINTER is a device name

•The Cobol program will refer to PRINT -FILE

Page 24: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

Select Statement for an Externally Described Printer FileSelect Product-Report

assign to formatfile-qsysprt.

Page 25: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

Select Statement for a Interactive-Display-File

Select Display-File

assign to workstation-PRDSPF

organization is transaction.

Select Display-File

assign to workstation-PRDSPF-SI

organization is transaction.

Page 26: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

Data Division

Further divided into sections

Declare / define the structure of ALL data objects used within a program.

Page 27: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

Data Division(Syntax)

[DATA DIVISION.][FILE-SECTION.]

All objects imported into the program from external (Global) sources.

[WORKING-STORAGE SECTION.]All objects required local to the program.

[LINKAGE-SECTION.]All parameter objects (fields) received from another programs or bi-directionally passed between programs.

Page 28: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

Bringing the Outside IN

Page 29: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

Working Storage Section

• Field or Variables needed by the program for internal processing

• Field or variable is not part of any Input or Output.• Some examples: counters, flags, edited fields

(masks), report lines, etc.• Fields are defined the same way as in the file

section• Value clause used to initialize variables or literals

(constants) used within the program.

Page 30: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

30

OUR PRINTING LAYOUT

*...+....1....+....2....+....3....+....4....+....5....+....6....+....7.. Payroll Monthly List Juan Barake 40 $ 400.00 Mary O'Brian 375 $3,125.00 Berns, Andre 275 $1,984.00

HEADER RECORD

HEADER

DETAIL RECORD

Page 31: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

31

WORKING-STORAGE SECTION. 01 HEADING-LINE. 05 FILLER PIC X(12) VALUE 'EMPLOYEE... '. 05 FILLER PIC X(110) VALUE SPACES.

01 DETAIL-LINE. 05 FILLER PIC X(8) VALUE SPACES. 05 DET-NAME PIC X(25). 05 FILLER PIC XX VALUE SPACES. 05 DET-HOURS PIC ZZZ. 05 FILLER PIC XX VALUE SPACES. 05 DET-PAY PIC $Z,ZZZ.99. 05 FILLER PIC X(92) VALUE SPACES.

77 EOF-FLAG PIC X VALUE SPACES.

HEADING-LINE:

Temporary area to hold the print header.

HEADING-LINE:

Temporary area to hold the print header.

DETAIL-LINE:

Temporary area to hold the print fields

DETAIL-LINE:

Temporary area to hold the print fields

EOF-FLAG:

Just a variable used as a ‘loop control’ Used within the logic of the program

EOF-FLAG:

Just a variable used as a ‘loop control’ Used within the logic of the program

Page 32: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

Defining Variables

• Zoned Decimals (or signed integers)

• Packed Decimal

• Alphabetic

• Alphanumeric

• Boolean

• Dates

• Time

• TimeStamp

Page 33: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

Variable Data vs Constant Data

• Variable Data Changes

• Constant Data doesn’t

• Figurative Constants– ZERO, ZEROS, ZEROES– SPACES

Page 34: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

Data Division(Syntax)

[DATA DIVISION.][FILE-SECTION.][FD nice-file-name]

[RECORD CONTAINS 99 CHARACTERS].[01 record-name.]

[05 field-names PIC ????.]

[WORKING-STORAGE SECTION.][01 field-name PIC X(10).]

[LINKAGE-SECTION.][01 ws-prog-parameters.]

[05 ws-parm-1 pic x(5).][05 ws-param-2 pic 9(6).]

Page 35: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

35

DATA DEFINITIONS

• Variable Names:

•1 to 30 characters

•Letters, digits, hyphen

•See rules in page 74 of your textbook

EMPNAME is valid

EMP NAME is invalid

EMP3NAME is valid

Page 36: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

36

DATA DEFINITIONS

• Data can be either a Constant or a Variable

• Constant are known as Literals

• Variables are known as Identifiers or Fields

• Constants can be Numeric (1, 200, -100, 1.5)

• Constants can be Alphanumeric (“CBL344 Course”)

• Variables can be keywords (Cobol defined names such as DATA) or they can be user-defined (user makes up the name such as BALANCE)

Page 37: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

File Section

• Define the files used in the program– File Description– Record Description

• Program Described vs Externally Described.

Page 38: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

File Section FD (file declaration)

• Label Clause– Not used often– Transmitting Data

• Block Clause– Optional– I’ve never seen it used

Page 39: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

File Section - FD

• Records Clause– Optional– Used to double check that the record format

Page 40: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

File Section - Record Description

• Starting at an 01 level and the subsequent field(s) detail(s) in each record

Page 41: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

41

DATA DIVISION. FILE SECTION. FD PAYROLL-FILE RECORD CONTAINS 45 CHARACTERS DATA RECORD IS EMPLOYEE-IN.

01 EMPLOYEE-IN. 05 EMP-NAME PIC X(25). 05 EMP-HOURS PIC 9(03). 05 EMP-RATE PIC 99V9 USAGE COMP-3. 05 EMP-DEP PIC X(15).

Level Numbers indicate field subordination

Level Numbers indicate field subordination

01 indicates the top level Located in pos 8 thru 11

01 indicates the top level Located in pos 8 thru 11

Data Subordination

Page 42: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

42

The Data PICture or PIC

•Data content is internally represented by EBCDIC (IBM internal representation of data equivalent to ASCII)

•It uses a byte (8 bits) to represent a single digit, a single letter, or any special character such as #

•See the EBCDIC table in page 19 of your textbook

•The content acceptable for a variable is defined by the clause PICTURE or simply PIC

Page 43: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

43

The Data PICture or PIC

• PICture describes occurrences of each character:

•Numeric are represented by 9

•Alphanumeric is represented by X

•Alphabetic is represented by A

•S represented the sign

•$ / - represent edited characters (for output)

Page 44: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

44

DATA DIVISION. FILE SECTION. FD PAYROLL-FILE RECORD CONTAINS 45 CHARACTERS DATA RECORD IS EMPLOYEE-IN.

01 EMPLOYEE-IN. 05 EMP-NAME PIC X(25). 05 EMP-HOURS PIC 9(03). 05 EMP-RATE PIC 99V9 USAGE COMP-3. 05 EMP-DEP PIC X(15).

Level Numbers indicate field subordination

Level Numbers indicate field subordination

01 indicates the top level Located in pos 8 thru 11

01 indicates the top level Located in pos 8 thru 11

Page 45: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

45

DATA DIVISION. FILE SECTION. FD PAYROLL-FILE RECORD CONTAINS 45 CHARACTERS DATA RECORD IS EMPLOYEE-IN.

01 EMPLOYEE-IN. 05 EMP-NAME PIC X(25). 05 EMP-HOURS PIC 9(03). 05 EMP-RATE PIC 99V9 USAGE COMP-3. 05 EMP-DEP PIC X(15).

Elements of Data Definition

Level Number

VariableName

PICture

PICtureGroup level

Page 46: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

46

DATA DIVISION. FILE SECTION. FD PAYROLL-FILE RECORD CONTAINS 45 CHARACTERS DATA RECORD IS EMPLOYEE-IN.

01 EMPLOYEE-IN. 05 EMP-NAME PIC X(25). 05 EMP-HOURS PIC 9(03). 05 EMP-RATE PIC 99V9 USAGE COMP-3. 05 EMP-DEP PIC X(15).

FD PRINT-FILE RECORD CONTAINS 132 CHARACTERS DATA RECORD IS PRINT-LINE. 01 PRINT-LINE PIC X(132). 01 pr-header-line-1.

03 hd1-report name pic x(20).03 filler pic x(15).03 hd2-date pic 9999b99b99.03 filler pic x(15).

01 pr-detail-line.03 filler pic x(10).03 dt1-quantity pic z(5)9-.03 filler pic xxx.03 dt1-descr pic x(30).etcetc

THE PRINTER LINE DEFINITION

Record formats

Record formats

Record formats

Page 47: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

Optionally define the record format structure Working-Storage

Page 48: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

48

THE DATA DEFINITIONSThe Payroll File

EACH EMPLOYEE RECORD LAYOUT: Data Field Buffer Buffer Field Column Field Type Length Length Position Usage Heading EMPNAME CHAR 25 25 1 Both NAME Field text . . . . . . . . . . . . . . . : NAME Alternative name . . . . . . . . . . . . : PT001F_NAME Coded Character Set Identifier . . . . . : 37 EMPHOUR ZONED 3 0 3 26 Both HOURS Field text . . . . . . . . . . . . . . . : HOURS Alternative name . . . . . . . . . . . . : PT001F_HOURS EMPRATE PACKED 3 1 2 29 Both RATE Field text . . . . . . . . . . . . . . . : RATE Alternative name . . . . . . . . . . . . : PT001F_RATE EMPDEP CHAR 15 15 31 Both DEPARTMENT Field text . . . . . . . . . . . . . . . : DEPARTMENT Alternative name . . . . . . . . . . . . : PT001F_DEPART Coded Character Set Identifier . . . . . : 37

PT001F = PAYROLL-FILE

Page 49: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

COPYImporting DATA DEFINITIONS into your program

LIBRARY - PF

Page 50: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

Spoolfile printoutNote: +’s on inserted lines

Page 51: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

FFDExternal data record format structure

Page 52: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

Linkage Section

• Define Parameters

Page 53: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

Variable Names

• Up to 30 Characters

• Letters, numbers and hypens (-)

• No blanks

• At least one letter

• Not a COBOL Reserved Word (see text for list of reserved words)

• Meaning full names please

Page 54: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

Variable Name Examples

Variable Name Correct?

Cindy Laurin

BAC344

Zero

Sales%

SOL-IN

Page 55: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

PICture Clauses(Elementary Items)

• 9 - Number

• X – Alphanumeric

• A - Alphabetic

• V - Decimal Point

PACKED-DECIMAL = COMP-3

Page 56: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

PIC Clauses

PIC 9(3) PIC 999

PIC 9(3)V9(3)

PIC X(5)

PIC S99999 COMP-3 PIC 99999 COMP-3

Page 57: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

Date, Time, & TimeStamp fields

01 Todays-date format of date.

01 Todays-time format of time.

01 Todays-timestamp format of timestamp.

Page 58: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

Boolean Variables

01 IN99 pic 1.

Page 59: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

PIC Clauses(Group Items)

• Mix of Elementary Items.

• Used to break a variable into smaller portions

Page 60: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

PIC Clauses(Group Item – Example)

01 Student-Number PIC 9(9).

01 Student-Number-Edited.

05 Student-Number-Part-1 PIC 9(3).

05 Student-Number-Part-2 PIC 9(3).

05 Student-Number-Part-3 PIC 9(3).

Page 61: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

61

Picture Clause Review

• Define Variables

• Define how Variables are displayed

Page 62: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

62

Editing FunctionsFunction CharacterPrinting of a / as a separator /

Printing of Decimal Point in an integer .

Suppress Leading Zeros in an integer Z

Print a Dollar Sign in front of an integer $

Print a Comma in an integer ,

Printing of + or – signs + or -

Printing of ‘Debit’ or ‘Credit’ symbols DB or CR

Printing of Spaces as separators B

Printing of Zeros as separators 0

Print Leading asterix *

Page 63: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

63

Examples

Trans-Amount PIC 9(6)V999 Value 4565.78.

Report-Item Edited Results

PIC 9(6)V999

PIC $999999V999

PIC $ZZZ,ZZZ.99

PIC $ZZZ,ZZZ.9

Page 64: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

64

Examples

Trans-Amount PIC 9(6)V999 Value 4565.78.

Report-Item Edited Results

PIC $ZZZ,ZZZ.99-

PIC $ZZZ,ZZZ.99CR

PIC $ZZZZZZB99

PIC $ZZZZZZ.99+

Page 65: Any Questions? Week 1 - 2nd Lecture Intro to COBOL Programming Defining Files and Processing Data

For Next Week

• Review Reserved words in Text

• Do your assignment!