Loading…

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

Like this presentation? Why not share!

Cobol basics 19-6-2010

on

  • 1,277 views

 

Statistics

Views

Total Views
1,277
Views on SlideShare
1,277
Embed Views
0

Actions

Likes
0
Downloads
49
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Cobol basics 19-6-2010 Cobol basics 19-6-2010 Presentation Transcript

  • COBOL
    • Cobol is one of the most robust language in the software field, so far
    • Cobol turned 50, in 2009
    • Cobol has stood the test of time
    • Common Business Oriented Language
    • It is very simple, no confusion
    • English like language – people think better in natural language than in programming language
    • Number of lines coded in COBOL as on date, exceeds the cumulative total number of lines coded in all known programming languages
    • Data maintained by COBOL program will take 10 to 15 years by a super computer, to be migrated to another platform or technology
    • Hence COBOL is here to stay further
    • Demand for COBOL still continues with same or more strength
    • COBOL has adapted well to stay fit in new millennium as well
    • Unfortunately people have the myth in mind, that COBOL is out dated
    • All big time software service companies, invariably work in COBOL and train people in COBOL, year after year
  • Program Structure
    • Each program has 4 divisions
      • Identification division
      • Environment division
      • Data division
      • Procedure division
    • Each division has sections within them
    • The section names are different for each division
    • Each section has definition or logic statements
  • Identification Division
    • This is the primary section that gives an identity to the program
    • COBOL is not case sensitive – we can write code in uppercase or lowercase letters
    • PROGRAM-ID is the main phrase in this section that gives a unique name to programs.
    • DATE-WRITTEN and DATE-COMPILED are used to denote documenting part of the program
    • AUTHOR is the clause to specify the name of the programmer.
    • Identification division.
    • Program-id. Myprog.
    • Author. Naga.
    • Date-written. 12-10-2008.
    • Date-compiled. 13-10-2008.
  • Margins
    • Since cobol evolved from punched cards time, the program lines are restricted to 80 column width
    • In this also, there are column boundaries that play major role
    • Columns 1 to 6 are meant for line numbers
    • Column 7 is meant for comment. A star mark in this column, denotes that line is a comment
    • Column 8 is called A margin. Columns 8 to 11 is called A area
    • Column 12 is called B margin. Columns 12 to 80 is called B area
    • Some statements must start from A area, and some other must start from B area.
    • If margin rules are not followed, the compiler will issue a syntax error
  • Environment Division
    • This is used to specify the computer types, special symbols and files related data.
    • This can have CONFIGURATION SECTION and INPUT-OUTPUT SECTION.
    • Configuration section can have the following.
      • SOURCE-COMPUTER. TDM180.
      • OBJECT-COMPUTER. TDM180.
      • SEPCIAL-NAMES.
        • - DECIMAL POINT IS COMMA, CURRENCY SIGN IS “$”.
    • In some countries the decimal points is not a dot and the currency signs keep varying.
    • Input-output section is mainly for file specifications. This is given at the end of the presentation.
  • Data Division
    • This is used to declare variables, screens, parameters to other programs and file record descriptions.
    • This can have file section, working-storage section, linkage section, screen section.
    • If sections are not used, they can be omitted as well. But usually every program will have at least one variable declaration.
    • Unlike other languages, ALL VARIABLES DECLARED IN A PROGRAM ARE GLOBAL VARIABLES.
  • Variables
    • Variables are to be declared in working-storage section
    • Variables can be scalar, arrays, records, file descriptor records
    • The name of variable can have alphanumeric, with first character as alphabet, and can have only dash (hyphen) in it. Hypen must be embedded
    • The type of variable is determined by the picture clause
    • 77 level variables are scalar variables; arrays and records are explained later in this presentation
    • 77 var-1 picture 999.
      • 9 stands for a single digit number. This is a 3 digit number
    • 77 var-2 picture xxx.
      • X stands for alphanumeric. This is a 3 character alphanumeric
    • 77 var-3 picture S99.99
      • S sign for numeric variable stands for sign + or -. A dot in picture clause with trailing 9s will indicate the number of decimals. Dot stands for explicit decimal point
    • 77 var-4 picture 9(5) value 100.
      • Value specifies what must be the initial value when variable is created
    • 77 var-4 picture a(20) value all “a”.
      • Picture A stands for alphabetic. All clause in value, will fill the entire variable with that value.
    • 9(4) in picture clause is same as 9999, and x(5) is same as xxxxx. The repetition count can be given in brackets.
    • 77 var-5 picture 99V99
      • V in picture clause with trailing 9s will indicate the number of decimals. V stands for explicit decimal point. In storage, the decimal point will not be stored as a character. Hence when storing in disk, it will store 99.78 as 9978, without decimal point. But it will know last 2 digits are for decimal.
    • 77 var-6 picture zzz9.99.
    • If this variable has a value of 59.28, and picture is set as 9999.99, it will display as 0059.28. If we do not want to display leading zeroes, we must use z to suppress zeroes.
  • Procedure Division
    • This is the place where the program logic is coded.
    • This can have user defined sections. Usually, this is segmented as multiple paragraphs.
    • Each paragraph is a set of statements that can act as a function or procedure as used in other languages
    • Since all variables are global, every variable is accessible from every paragraph within that program.
    • Statement delimiter is dot.
    • Procedure division para names must start in A area
    • Procedure division statements must start in B area
    • The program starts from the very first line of the procedure division till the statement STOP RUN is reached.
  • Initializing Variables
    • Move is used to set the value of variables
    • Assume v1 is numeric and s1 is alphanumeric text
      • Move zeroes to n1.
      • Move spaces to s1
    • There is a verb Initialize that can automatically determine the type of variable and move either zeroes or spaces
      • Initialize n1.
      • Initialize s1.
  • Simple User Input
    • Accept is the verb to get values from user
    • Accep1 n1. – This will get the values from user, from the cursor position
    • Accept (10, 15) n1. – This will get the value of the variable at line 10 column 15 on the screen
    • Accept (10, 15) n1 with prompt. – This will do same as above, except it will display dashes so that you the length of the variable
    • Accept n1 from date. – This will give you the current system date into variable
    • Accept n1 from time. – This will give you the current system time into variable
  • Simple Output
    • Display is the verb to display values of variables on screen
    • Display n1. – This will display the values on screen, from the cursor position
    • Display (10, 15) n1. – This will display the value of the variable at line 10 column 15 on the screen
    • Display Blank screen. – This will erase the content of screen. This must be defined in screen section.
  • Arithmetic Verbs
    • Unlike other languages, cobol prefers to explicitly spell out the operations.
      • Add v1 to v2 giving v3.
      • Subtract v1 from v2 giving v3.
      • Multiply v1 by v2 giving v3.
      • Divide v1 by v2 giving v3 remainder v4.
    • Formula can be expressed thru compute verb
      • Compute v1 = (v2 + v3) * (v2 – v3).
  • Conditions
    • If and Evaluate are used for conditional branching
    • The relational operators as well as spelt out phrases can be used
    • If a < b – this can be written as if a is less than b
    • >, <, =, >=, <= are the available relational operators
    • NOT, AND, OR are the logical operators
    • Less than, greater than, equal to are the spelt out phrases
    • If a is less than b then
      • Do some thing
    • Else
    • Do the other way
    • End-if.
    • To check an alphanumeric variable for what type of data it holds, we can use if a is numeric, if a is alphabetic, conditions. Based on the value this will be true or false. We can then take decisions.
  • Multiple Conditions
    • We can use if-then-else if for multi level conditions
    • To make it very simple, we can use evaluate verb
    • Evaluate a
    • When 1 perform para1
    • When 2 perform para2
    • When 3 perform para3
    • When other perform para4
    • End-evaluate
    • Each when statement will be treated as a separate if condition. When 3 here means, if a is equal to 3.
    • If no condition is matched, the other clause is executed. This is similar to the switch statements in other languages.
    • Without checking anything, we can transfer the program flow using go to. GO TO mypara. This will suddenly branch to that para. Go To is not supposed to be used, as it is not a good programming practice.
  • Perform Loop
    • Perform is the loop in cobol
    • There are many variations of this verb
    • Perform para1. This will execute only that para only once
    • Perform para1 5 times. This will repeat the execution
    • Perform para1 thru para5. This will execute any para from para1 to para5, that appear one after the other on the source code
    • Perform para1 until a < b. This will execute the para until the condition becomes true.
    • Perform para1 varying v1 from 1 by 1 until v1 > 10. This will use v1 as the loop counter and increment it by 1, till it reaches 10.
  • Records
    • Records are nothing but collection of related variables
    • These are equivalent to structures in other languages
    • The record variables do have level numbers
    • Level numbers 1 to 76 are used
    • 01 employee-record.
    • 05 employee-code picture 9(5).
    • 05 employee-name picture x(30).
    • 05 dob.
    • 10 yyyy picture 9999.
    • 10 mm picture 99.
    • 10 dd picture 99.
    • 05 designation picture x(3).
    • 05 doj.
    • 10 yyyy picture 9999.
    • 10 mm picture 99.
    • 10 dd picture 99.
    • 05 salary picture 9(6).99.
    • The record can have any levels. They are grouped by level numbers.
      • Yyyy is the sub field of dob. Dob itself is a sub field of employee-record.
    • To denote a field in a record, we need to qualify with OF.
      • Yyyy of doj of employee-record
      • Designation of employee-record
  • Move Corresponding
    • When we have 2 similar records, and we want to move data from one to the other, we can use this
    • We need not write separate move statements for each field of the record
    • 01 rec1.
    • 05 field1 pic 999.
    • 05 field2 pic xx.
    • 05 filed3 pic x.
    • 01 rec2.
    • 05 field2 pic xx.
    • 05 filed3 pic x.
    • 05 field1 pic 999.
    • To move data in a single statements, to respective fields, we can say
      • Move corresponding rec1 to rec2.
    • This will move field1 of rec1 to field1 of rec2, field2 of rec1 to field2 of rec2, field3 of rec1 to field3 of rec2.
    • Though the fields are in different positions in the 2 records, cobol will match the field names and then move values.
  • Condition Names
    • Variables hold values, and based on these values we may have to take decisions in code
    • Usual way is to have an if condition to see whether the variable satisfies a condition
    • This can be easily done in a more readable way using condition names
    • 77 mm pic 99.
    • 88 jan value 01.
    • 88 feb value 02.
    • 88 march value 03.
    • 88 april value 04.
    • We can write if condition to check the month as if mm = 2 then…
    • The same can be written in a better way as if jan then. When mm is set to 1, this name jan is set to true and if condition becomes true.
    • People prefer to write English like conditions, such as, if file-is-closed, if account-is-active, if ticket-is-confirmed etc. This makes programs simple
  • Arrays
    • Arrays are nothing but a set of variables with same name, differentiated by an index number
    • 01 rec1.
      • 05 student-name picture x(30) occurs 100 times.
    • Occurs clause determines the number of elements in an array
    • Move spaces to student-name (10) will access the 10 th element of the array
    • Arrays start with index as 1
    • Any element that is accessed beyond the limit will throw a runtime error and hence we need to be careful
    • We can have multi level arrays also, but they are very rarely used in business applications
  • File Handling
    • Cobol supports sequential files, relative files and indexed files
    • To handle files, we must specify file type and mode in input-output section
    • The following is a standard one when we need to access files - SELECT myfile ASSIGN TO DISK
    • Myfile is just a logical name that we want to use later in program. It can be EmployeeFile, PayFile etc
    • In data division, we must have a FD (file descriptor) part and record definition of the same
    • There are RD and SD definitions (reporting and sorting descriptions). These 2 are not discussed in this presentation
    • We can access disk, tape and printer files. In that case, the assign to clause will change.
    • In procedure division, OPEN, READ, WRITE, START, REWRITE, DELETE, CLOSE are the common verbs that we will use to deal with files.
    • Every file operation must be checked for the status of the operation. COBOL gives the status of last operation in a variable called file-status.
    • In working-storage, we must define a variable ws-fs.
      • 01 ws-fs.
      • 05 s1 pic 9.
      • 05 s2 pic 9.
    • It contains 2 parts, first digit and second digit. A zero in both digits indicates, success; all other codes must be handled specially.
    • Standard practice is to load record with values before WRITE and initialize record before READ operations
  • Defining Sequential Files
    • In sequential files, we can add records one after the other and read them one after the other.
    • We cannot directly jump to any record we want; we need to go from the beginning
    • SELECT myfile assign to disk
    • Organization is sequential
    • Access mode is sequential
    • file status is ws-fs.
    • In the FD part of file section, we must use the standard phrases to attach a physical file to the logical name myfile.
    • FD FD myfile
    • RECORD CONTAINS 62 CHARACTERS
    • LABEL RECORD IS STANDARD
    • DATA RECORD IS employee-record
    • VALUE OF FILE-ID &quot;myfile.DAT&quot;.
    • 01 employee-record.
    • 05 emp-code pic 9(5).
    • 05 emp-name pic x(30).
    • 05 dob pic 9(8).
    • 05 doj pic 9(8).
    • 05 designation pic x(3).
    • 05 ctc pic 9(6)V99.
    • The record character count given in the 01 record, must match with FD description.
    • Label record is to tell the file system of the machine that they must be standard. Other clause can be OMITTED
    • FILE-ID is the one that actually binds a physical file myfile.dat to the logical file.
  • Handling Sequential Files
    • Open statement must have the mode specified.
    • The open modes are input, output, extend
    • We cannot delete a record in a sequential file; however we can even rewrite records, only in certain cobol versions
    • In many to most cobol versions, opening sequential file in I-O mode is not allowed
    • For input mode, the file must exist; in output mode, the previous file data will be erased; in extend mode, the previous file data can be appended with new data
    • Open Input myfile.
      • Open extend myfile.
    • To write data to the file, we must first load valid data to the file record, as given in the FD part
    • Then we need to issue a WRITE command
      • WRITE emp-record
      • END-WRITE
    • To read a record, we must use READ command. The AT END clause, will tell whether we reached end of file or not.
    • READ myfile
    • AT END move 1 to ws-end-of-file
    • END-READ
    • The read will get the record that is fetched from the disk to the FD record.
    • To close the file, use, CLOSE myfile.
    • When program ends, all open files will be closed.
  • Relative Files
    • Relative files are used, to directly jump to any record based on the position of the record
    • E.g. If we want to go to the 57 th record in a file, we can use relative files
    • However, relative files are nowadays very rarely used and they are replaced by indexed files
    • For relative file, we must define organization is relative, access mode is random.
    • Since it is not used practically in current day scenarios, we do not address it in this presentation
  • Defining Indexed Files
    • Indexed files are one of the most used file types as they establish a good DBMS
    • Records in indexed files can be accessed sequentially or randomly as we like
    • Using relative files, based on some key values, we can search for a record and the search is binary search and not sequential search; hence it is faster
    • COBOL can handle billions of records in indexed file itself
    • SELECT myfile assign to disk
    • Organization is indexed
    • Access mode is dynamic
    • Record key is emp-code of employee-record
    • file status is ws-fs.
    • In the FD part of file section, we must use the standard phrases to attach a physical file to the logical name myfile.
    • FD FD myfile
    • RECORD CONTAINS 62 CHARACTERS
    • LABEL RECORD IS STANDARD
    • DATA RECORD IS employee-record
    • VALUE OF FILE-ID &quot;myfile.DAT&quot;.
    • 01 employee-record.
    • 05 emp-code pic 9(5).
    • 05 emp-name pic x(30).
    • 05 dob pic 9(8).
    • 05 doj pic 9(8).
    • 05 designation pic x(3).
    • 05 ctc pic 9(6)V99.
    • In the above file, we can search for any emp-code much faster.
  • Accessing Indexed Files
    • The open mode can be input, i-o, or output.
    • Close command is same for all types of files
    • If the file is opened with sequential access mode, we can use, this type of command to keep on reading the next records
    • READ SupplierFile
    • NEXT RECORD
    • AT END SET EndOfFile TO TRUE
    • END-READ
    • If the access mode is set to direct access, we can use this type of read command, to go to the specific record that has the key.
    • READ SupplierFile
    • INVALID KEY DISPLAY &quot;SUPP STATUS :-&quot;, SupplierStatus
    • END-READ
    • If a record with the key value populated before read, is not found, invalid key statement is executed
    • If the access mode is dynamic, we can use start verb to position the reading point based on some key value.
    • START Oil-Details-File
    • KEY IS GREATER THAN 150
    • INVALID KEY DISPLAY &quot;Start Error”
    • END-START.
    • We can use equal to, less than and other operators in the Key clause of start. If a key is found satisfying the condition, the start is successful; else invalid key statement is executed
    • WRITE command is also used with INVALID KEY clause
    • REWRITE can be used to update a record, provided we have executed a START command before rewriting, so that the correct record is pointed.
    • REWRITE is also used along with INVALID KEY option
  • Program Branching, Termination
    • We can use CALL command to call another compiled cobol program
    • CALL “prog2”.
    • The “prog2” must be the one appearing in program-id of the called program
    • If we define, linkage section, we can use call “prog2” using linkage-entries.
    • Linkage section is not discussed in this presentation.
    • To stop a program from execution, use STOP RUN.
    • To exit from a called program, use EXIT PROGRAM.
    • To exit from a paragraph, use EXIT.
  • Example Programs
    • Refer to this site.
    • You will get a lot of examples.
    • Syntax may slightly vary between IBM, HP and other COBOL versions.
    • But, get the concept, right.
    • http://www.csis.ul.ie/COBOL/examples/default.htm