Академический Документы
Профессиональный Документы
Культура Документы
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 INPUTOUTPUT 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 X stands for alphanumeric. This is a 3 character alphanumeric 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 Value specifies what must be the initial value when variable is created Picture A stands for alphabetic. All clause in value, will fill the entire variable with that value.
77 var-2 picture xxx. 77 var-3 picture S99.99 77 var-4 picture 9(5) value 100.
77 var-4 picture a(20) value all a. 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 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.
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
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. 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
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-isclosed, 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 10th 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
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 57th 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
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/def ault.htm