580 likes | 599 Views
Learn the basics of COBOL programming, including how to create a program, define files, and process data. Understand the different parts of a COBOL program and the key COBOL verbs. This lecture will also cover how a COBOL compiler works.
E N D
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
Compilers • Translate programs written by humans into Machine Language Code.
COBOL Compiler on the iSeries Compiled Listing (PGM1) Source Code QCBLLESRC (PGM1) COBOL Compiler Program Object (PGM1)
Steps to Create a COBOL Program 1. Create a Source Physical File - QCBLLESRC 2. Create a Source Member in the Source Physical file with the type CBLLE 3. Type in the COBOL code using SEU 4. Exit & Save 5. Compile
What is COBOL? • Designed by the US defense department • Meant to be English like for ease of maintenance COmmon Business Oriented Language
Parts of a COBOL Program • Identification Division • Environment Division • Data Division • Procedure Division (See Page 51 of your text)
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] …]
Identification Division • Required • Name of the Program in the Program-ID paragraph. • Optional • Author • Installation • Date-Written • etc.
Environment Division(Syntax) [ENVIRONMENT DIVISION.] [CONFIGURATION SECTION.] [SOURCE-COMPUTER. AS/400.] [OBJECT-COMPUTER. AS/400.] [INPUT-OUTPUT SECTION.] [FILE-CONTROL.] [SELECT nice-file-name ASSIGN to DISK/PRINTER/WORKSTATION- physical-file-name.]
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
Select for a Database File Select Product-File Assign to disk-PRODPF. Select Product-file Assign to database-PRODPF Access is Sequential.
Select Statement for a Display-File Select Display-File assign to workstation-PRDSPF organization is transaction. Select Display-File assign to workstation-PRDSPF-SI organization is transaction.
Select Statement for a Spooled File Select Product-Report assign to printer-qprint. Select Product-Report assign to printer-qsysprt.
Select Statement for an Externally Described Printer File Select Product-Report assign to formatfile-qsysprt.
File Section • Define the files used in the program • File Description • Record Description • Program Described vs Externally Described.
File Section - FD • Label Clause • Not used often • Transmitting Data • Block Clause • Optional • I’ve never seen it used
File Section - FD • Records Clause • Optional • Used to double check that the record format
File Section - Record Description • Details the fields in each record
Working Storage Section • Variables need by the program that are not described in the files and not parameters • Value clause used to initialize variables.
Linkage Section • Define Parameters
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).]
Defining Variables • Zoned Decimals (or signed integers) • Packed Decimal • Alphabetic • Alphanumeric • Boolean • Dates • Time • TimeStamp
Variable Data vs Constant Data • Variable Data Changes • Constant Data doesn’t • Figurative Constants • ZERO, ZEROS, ZEROES • SPACES
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
PICture Clauses(Elementary Items) • 9 - Number • X – Alphanumeric • A - Alphabetic • V - Decimal Point PACKED-DECIMAL = COMP-3
Date, Time, & TimeStamp fields 01 Todays-date format of date. 01 Todays-time format of time. 01 Todays-timestamp format of timestamp.
Boolean Variables 01 IN99 pic 1.
PIC Clauses(Group Items) • Mix of Elementary Items. • Used to break a variable into smaller portions
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).
Procedure Division(Syntax) PROCEDURE DIVISION. paragraph-name. statements.
Procedure Division • Program Logic • Statements are executed in the order that they appear. • Logic can be divided into paragraphs (sub-routines) • Cindy’s advice: put a period at the end of every line.
Perform Statement • COBOL’s version of the execute sub-routine command. • Works as follows: • Goto the start of a paragraph. • Repeat in sequence, all of the statements in the paragraph as many times as required. • UNTIL Clause • Return to the calling statement.
Coding a COBOL Program • Column based • 3 parts to a COBOL source line • Continuation (-) • Area A • Area B
Continuation (-) • * to start a comment line
Area A • Division Names • Section Names • Paragraph Names
Area B • Everything else
OPEN Statement • Usually the first statement in a program • Open files for processing • Terminator is a period
Sequential READ Statement • Used to Read the next Record from a File • End of File Logic • Not End of File Logic • Terminator - END-READ.
Sequential READ Statement READ Employee-File AT END PERFORM 500-End-Of-File-Routine NOT AT END PERFORM 300-Process-Employee END-READ.
Display File Read • Used to read information from a screen • RECORD keyword is needed because display files usually have more than one record format. Read Display-file RECORD.
WRITE Statement • Writes/Outputs records to a file • Terminator is . Or END-WRITE. • ‘Format is’ clause needed for display files • Format is record format • Needed because display files usually have more than one record format (screen)