This watermark does not appear in the registered version - http://www.clicktoconvert.com 1 BACHELOR OF COMPUTER APPLICATIONS (BCA) COBOL CONTENTS Syllabus UNIT –1 PAGE NO Lesson-1 Introduction to COBOL 1 Lesson-2 Divisions of COBOL 9 Lesson-3 Picture clause characteristics 16 Lesson-4 Editing 20 Lesson-5 Level Structure 26 UNIT – 2 Lesson-6 Data Movement verb: MOVE 30 Lesson-7 Arithmetic Verbs 34 Lesson-8 Input and Output Verbs 41 Lesson-9 Corresponding Options 48 Lesson-10 Programs using Arithmetic Verbs 54 UNIT – 3 Lesson-11 Conditions 60 Lesson-12 Conditionals Statements 70 Lesson-13 PERFORM statements 77 Lesson-14 RENAMES & REDEFINES Clauses 82 Lesson-15 Programs 86 UNIT– 4 Lesson-16 Sequential Files 92 Lesson-17 Direct Access Files 98 Lesson-18 Indexed Sequential Files 102 Lesson-19 Sorting and Merging of Files 107 Lesson-20 Programs 114 UNIT – 5 Lesson-21 Table Handling 123 Lesson-22 Indexed Tables & Index Names 128 Lesson-23 Search & Start Verbs 132 Lesson-24 Programs using OCCURS & Screen Section 136 Lesson-25 List of Programs 142 This watermark does not appear in the registered version - http://www.clicktoconvert.com 2 UNIT – I LESSON – 1: INTRODUCTION TO COBOL CONTENTS 1.0 Aims and Objectives 1.1 History of COBOL 1.2 FORMAT FOR COBOL PROGRAMS 1.3 STRUCTURE OF A COBOL PROGRAM 1.4 CHARACTER SET 1.5 COBOL WORDS 1.6 DATA NAMES AND IDENTIFIERS 1.7 LITERALS 1.8 Language Description Notation 1.9 Let us Sum up 1.10 Lesson-end Activities 1.11 Points for Discussion 1.12 References 1.0 AIMS AND OBJECTIVES In this lesson, the learner will be introduced the History of Cobol , Structure of a COBOL program, Character set, words, data names and identifiers and Literals of COBOL. The objective here is to familiarize him the prerequisites to understand the language. 1.1 HISTORY OF COBOL 0In 1959, the new language named COBOL (COmmon Business Oriented Language) was introduced keeping in mind the business purpose applications. The board of directorate which is known as CODASYL (Conference on DATA System Language) COBOL programming language committee established a COBOL maintenance committee to keep COBOL in step with the times. The first COBOL compiler became available in early 1962. The next version with some new additions was published in 1965. In August 1968 a standard version of the language was approved by the American National Standard Institute (ANSI). This standard version was again modified in 1974 and is known as ANSI-74 COBOL or COBOL -74. The revision process has been continuous and in 1985 a revised standard was introduced. This standard was known as COBOL-85. 1.2 FORMAT FOR COBOL PROGRAMS COBOL is a high-level language. Hence, a COBOL program can be executed on a computer for which a COBOL compiler is available. The compiler translates a COBOL This watermark does not appear in the registered version - http://www.clicktoconvert.com 3 source program into the machine language object program. This object program is really executed. COBOL programs are written in coding sheets. There are 80 columns in a line of the coding sheet. The page number is coded in columns 1-3 and the line numbers are coded in columns 4-6. The page and line numbers together is called the sequence number. Depending on the type, the entries are coded both from column 8 or column 12 and in both cases it can be continued up to column 72. Columns 73-80 can be used to write some identification. The compiler ignores anything that is given in columns 73-80 except when a printed copy of the program is provided by the compiler in which case the entries in columns 73-80 are also listed. The use of the sequence number is also optional and can be omitted. However, when sequence numbers are provided they must appear in ascending order. Column Field 1-3 Page Number 4-6 Line Number (1-6 Sequence Number) 7 Continuation / Comment 8-11 A – Margin / Area A 12-72 B- Margin /Area B 73-80 Identification In COBOL there are two types of entries known as margin A and margin B entries. Margin A entries start from column 8, 9, 10 or 11 and margin B entries start from column 12 or anywhere after 12. An asterisk (*) in column 7 indicates a comment line and the entry is not compiled to produce object code. Comment lines are actually some notes revealing the intentions of the programmer. Since the compiler ignores them, anything can be included as comments. Comment lines can appear anywhere after the first line of the COBOL program. A comment line can also be indicated by using the character slash ( / ) in column 7. But in this case the comment line will be printed after causing a page ejection (i.e., after skipping to the top of the next page). 1.3 STRUCTURE OF A COBOL PROGRAM Every COBOL program must have the following 4 divisions in the order in which they are specified below. 1. Identification division 2. Environment Division 3. Data Division 4. Procedure Division In the Identification division the details about the author, date of writing the program etc will be specified. In the Environment division, the details about the computer environment under which the program was written and compiled etc will be notified. In the Data division, the variables that are used by the program will be defined and it is an important division for the program. This watermark does not appear in the registered version - http://www.clicktoconvert.com 4 In the procedure division, all the programming statements (Executable Cobol statements) will be written and it is the most important division. Under the divisions there are various sections intended for specific purposes. To name a few, working-storage section and File section come under Data division. Their purpose will be to allocate memory space for the variables and to notify the files that are to be used with the program. A statement of a COBOL program can be written in one or more coding lines. To continue in the next line one has to use a hyphen (-) in column 7. 1.4 CHARACTER SET To learn any language, first one must know the alphabets of the language and they are known as character set in general. There are 50 different characters in COBOL character set. They are listed below. 0-9 (10 numerals) A-Z (26 English alphabets-only capital letters) – (minus sign or hyphen) + (Plus sign) * (Asterisk) / (Slash) 0= (Equal sign) $ (Currency sign) , (Comma) ; (Semi colon) . (Period or decimal point) “ (Quotation mark) ( (Left Parenthesis ) ) (Right Parenthesis) > (Greater than symbol) < (Less than symbol) The characters 0-9 are called numeric characters or digits. The characters A-Z are called letters and the remaining characters are called special characters. The space or blank character in certain cases is treated as a letter. 1.5 COBOL WORDS A COBOL word can be formed using the following characters: 0-9 A-Z (a-z) - (hyphen) The following rules must be adhered in forming COBOL words. (i) A word cannot begin or end with a hyphen. (ii) A word can have at the maximum 30 characters. (iii) One of the characters must be a letter. Some compilers put the additional restrictions that the first character must be a letter. (iv) Except hyphen (-) no special character allowed. This watermark does not appear in the registered version - http://www.clicktoconvert.com 5 Examples Valid Word Invalid Word & Reason emp-sal -pay ( it starts with a hyphen) TOTAL-MARK TOTAL MARK (blank space embedded) There are 2 types of words in COBOL. A COBOL word can be either a user-defined word or reserved word. The reserved words are used in COBOL statements and entries for specific purposes by the COBOL compiler. Some reserved words are given below: ADD, SUBTRACT, DIVIDE, MULTIPLY, IF, PERFORM etc. To know the complete set of reserved words of COBOL one can refer to the manual supplied with the compiler. Any attempt by the programmer to declare the reserved word will be indicated as an error during the compilation stage of the program. 1.6 DATA NAMES AND IDENTIFIERS A data name gives reference to the storage space in the memory where the actual value is stored. This value takes part in the operation when that particular data name is used in the PROCEDURE DIVISION. Identifier is a general term which means the single data name or a data name qualified, indexed or subscripted. Data names are only one form of identifiers. A data name must be a user-defined word and it cannot be a reserved word. Examples Valid Data Names Invalid Data Names NET-SALARY COMPUTE (Reserved word) TOT-MARK MULTIPLY (Reserved word) N100 23 (No letter) 1.7 LITERALS The actual values can also appear in a program. Such values are known as literals. For Example, the statement MOVE 0 TO TOTAL indicates that the value zero will be moved to the variable TOTAL. This constant 0 which used in the body of the statement is a literal. A data name may have different values at different points of time whereas a literal means the specific value which remains unchanged throughout the execution of the program. For this reason a literal is often called a constant. Moreover the literal is not given a name; it represents itself and does not require to be defined in the DATA DIVISION. There are 3 types of literals a) numeric b) nonnumeric. c) figurative constants This watermark does not appear in the registered version - http://www.clicktoconvert.com 6 (a) Numeric A numeric literal can be formed with the help of digits only. It can have a sign (+ or -) and can have a decimal point also. If no sign is specified the literal will be taken as positive. Negative literals are indicated by – sign at the leftmost end. If no decimal point is used then the literal is obviously an Integer. If a decimal point is used, it must come in between the digits. The maximum number of digits allowed in a numeric literal is compiler dependent. Examples (i) Valid Numeric Literal (ii) Invalid Numeric Literals .123 ‘’123’’(valid as nonnumeric literal but invalid as numeric literal) 12.5 - 23 (there is a blank space between the sign and the first digit 2) (b) Nonnumeric A nonnumeric literal is used in general to output messages or headings. Characters that are enclosed between “ “ constitute nonnumeric literal. The maximum number of characters that are allowed within two quotation marks is compiler dependent. (iii) Valid Nonnumeric Literal “BHARATHIAR” “DATA DIVISION” “100.50” “HOUR/RATE” (iv) Invalid Nonnumeric Literal 7 (valid as numeric literal but invalid as Nonnumeric literal) “nine (Invalid because there is no quotation mark on the right) 12.5” (Invalid because there is no quotation mark on the left) c) Figurative Constants Figurative constants have some fixed names and the compiler recognizes these names and it sets up corresponding values in the object program. Consider the statement given below : MOVE ZERO TO COUNTER Here value 0 will be moved to COUNTER by the compiler, as it recognizes ZERO and sets COUNTER with 0. Given below is the list of figurative constants. This watermark does not appear in the registered version - http://www.clicktoconvert.com 7 Figurative Constant Meaning ZERO ZEROS value 0 ZEROES SPACE SPACES One or more blanks HIGH-VALUE HIGH-VALUES Highest value in the Collating sequence LOW-VALUE LOW-VALUES Lowest value in the Collating sequence QUOTE QUOTES one or more of “ ALL literal one or more of the string characters comprising the literal 1.8 LANGUAGE DESCRIPTION NOTATION The following notations will be used to describe the syntax of COBOL statements. 1. Words in CAPS & underlined à Key words ( Compulsory in statements ) 2. Words in CAPS & NOT underlined à Noise words ( Optional in statements ) 3. [ entries] à can be included or dropped 4. { entries} à only one option to be selected 5. , ; à can be included or dropped 6. … à repetition of previous entry 7. Space character à used as a separator 1.9 LET US SUM UP In the above lesson we have learnt the how the structure of a COBOL program will look like , character set , words, data names & literals of the COBOL language in detail. This will help you to learn the subsequent lessons comfortably. 1.10 LESSON-END ACTIVITIES Try to find the answers for the following exercises on your own. 1. Describe the format of COBOL programs 2. Bring out the character set of COBOL 3. What do you mean by COBOL word? Give examples. 4. Explain about data names and identifiers This watermark does not appear in the registered version - http://www.clicktoconvert.com 8 5. Discuss in detail the Literals of COBOL. 1.11 POINTS FOR DISCUSSION 1. Explain in brief about the structure of a COBOL program. 2. List out the character set of COBOL 3. What are the rules to be followed while forming a COBOL word? Give Examples. 4. Briefly explain about figurative constants. 1.12 REFERENCES 1. COBOL Programming , M.K.Roy & Ghosh Dastidar , Tata McGraw Hill, 2nd Edition,1998 2. COBOL Programming , V. RAJARAMAN, PHI Pub 3. Introduction to COBOL programming – Dr. R.Krishnamoorthy, JJ Publ 4. Structured COBOL , Welburn, TataMcGraw Hill , 4th Edition. This watermark does not appear in the registered version - http://www.clicktoconvert.com 9 LESSON – 2: DIVISIONS OF COBOL CONTENTS 2.0 Aims & Objectives 2.1 Identification & Environment Division 2.2 Identification Division. 2.3 Environment Division 2.4 Configuration Section 2.4.1. Source-Computer 2.4.2 Object-Computer 2.4.3 Special Names 2.5 Input-Output Section 2.5.1 File-Control 2.6 Data Division. 2.7 Let us Sum up 2.8 Lesson-end Activities 2.9 Points for Discussion 2.10 References 2.0 AIMS AND OBJECTIVES In this lesson, the learner will be introduced the various divisions of COBOL and their contents. The objective here is to make the learner aware of the entries under each division and their importance. 2.1 IDENTIFICATION AND ENVIRONMENT DIVISION The IDENTIFICATION AND ENVIRONMENT DIVISION are the first two leading divisions of any COBOL program. These divisions contain entries that are required to either identify the program or describe the computer system to be used for the compilation and execution of the program. Generally, the entries in these divisions are used only for documentation purposes. 2.2 IDENTIFICATION DIVISION The IDENTIFICATION DIVISION is the first division of every COBOL source program. The paragraph PROGRAM-ID is essential in most of the machines. The other paragraphs are optional. This watermark does not appear in the registered version - http://www.clicktoconvert.com 10 The structure of Identification division is given below. IDENTIFICATION DIVISION. PROGRAM-ID. Entry [AUTHOR. entry.] [INSTALLATION. entry.] [DATE-WRITTEN. entry.] Optional [DATA-COMPILED. entry.] [SECURITY. entry.] [REMARKS. entry.] The division heading and paragraph names should be coded as margin A entries. The entries following the paragraph headings must be terminated by a period. The entry in the PROGRAM-ID paragraph contains the program name to be used to identify the object program. The entries in the other paragraphs are normally treated as comments and the programmer is free to write anything for these entries. Only meaningful entries should be included in these places to provide better documentation and clarity. The entry for the AUTHOR paragraph may include the name of the programmer. The entry of the DATE-COMPILED paragraph may contain the date of compilation. If this entry is left blank, the compiler inserts the actual date in the listing of the source program that may be printed during compilation. 2.3 ENVIRONMENT DIVISION The ENVIRONMENT DIVISION is the second division in a COBOL source program. It is the most machine-dependent division. The computer and all peripheral devices required by the program are described in this division. This division contains two sections 1) CONFIGURATION SECTION and 2) INPUT-OUTPUT SECTION. Of these the CONFIGURATION SECTION appears first. The outline of the sections and paragraphs of this division is shown below. ENVIRONMENT DIVISION. CONFIGURATION SECTION. SOURCE-COMPUTER. source-computer-entry. OBJECT-COMPUTER. object-computer-entry. [SPECIAL NAMES. special-computer-entry]. INPUT-OUTPUT SECTION. FILE CONTROL. {file-control-entry}…. [I-O-CONTROL. input-output-control-entry]. For most compilers the COBOL source program must at least include the two section headings and the three paragraphs-SOURCE-COMPUTER, OBJECT-COMPUTER and FILE-CONTROL.
Description: