COBOL – Internal Sort ”; Previous Next Sorting of data in a file or merging of two or more files is a common necessity in almost all business-oriented applications. Sorting is used for arranging records either in ascending or descending order, so that sequential processing can be performed. There are two techniques which are used for sorting files in COBOL − External sort is used to sort files by using the SORT utility in JCL. We have discussed this in the JCL chapter. As of now, we will focus on internal sort. Internal sort is used to sort files within a COBOL program. SORT verb is used to sort a file. Sort Verb Three files are used in the sort process in COBOL − Input file is the file which we have to sort either in ascending or descending order. Work file is used to hold records while the sort process is in progress. Input file records are transferred to the work file for the sorting process. This file should be defined in the File-Section under SD entry. Output file is the file which we get after the sorting process. It is the final output of the Sort verb. Syntax Following is the syntax to sort a file − SORT work-file ON ASCENDING KEY rec-key1 [ON DESCENDING KEY rec-key2] USING input-file GIVING output-file. SORT performs the following operations − Opens work-file in I-O mode, input-file in the INPUT mode and output-file in the OUTPUT mode. Transfers the records present in the input-file to the work-file. Sorts the SORT-FILE in ascending/descending sequence by rec-key. Transfers the sorted records from the work-file to the output-file. Closes the input-file and the output-file and deletes the work-file. Example In the following example, INPUT is the input file which needs to be sorted in ascending order − IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT INPUT ASSIGN TO IN. SELECT OUTPUT ASSIGN TO OUT. SELECT WORK ASSIGN TO WRK. DATA DIVISION. FILE SECTION. FD INPUT. 01 INPUT-STUDENT. 05 STUDENT-ID-I PIC 9(5). 05 STUDENT-NAME-I PIC A(25). FD OUTPUT. 01 OUTPUT-STUDENT. 05 STUDENT-ID-O PIC 9(5). 05 STUDENT-NAME-O PIC A(25). SD WORK. 01 WORK-STUDENT. 05 STUDENT-ID-W PIC 9(5). 05 STUDENT-NAME-W PIC A(25). PROCEDURE DIVISION. SORT WORK ON ASCENDING KEY STUDENT-ID-O USING INPUT GIVING OUTPUT. DISPLAY ”Sort Successful”. STOP RUN. JCL to execute the above COBOL program − //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO //IN DD DSN = INPUT-FILE-NAME,DISP = SHR //OUT DD DSN = OUTPUT-FILE-NAME,DISP = SHR //WRK DD DSN = &&TEMP When you compile and execute the above program, it produces the following result − Sort Successful Merge Verb Two or more identically sequenced files are combined using Merge statement. Files used in the merge process − Input Files − Input-1, Input-2 Work File Output File Syntax Following is the syntax to merge two or more files − MERGE work-file ON ASCENDING KEY rec-key1 [ON DESCENDING KEY rec-key2] USING input-1, input-2 GIVING output-file. Merge performs the following operations − Opens the work-file in I-O mode, input-files in the INPUT mode and output-file in the OUTPUT mode. Transfers the records present in the input-files to the work-file. Sorts the SORT-FILE in ascending/descending sequence by rec-key. Transfers the sorted records from the work-file to the output-file. Closes the input-file and the output-file and deletes the work-file. Example In the following example, INPUT1 and INPUT2 are the input files which are to be merged in ascending order − IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT INPUT1 ASSIGN TO IN1. SELECT INPUT2 ASSIGN TO IN2. SELECT OUTPUT ASSIGN TO OUT. SELECT WORK ASSIGN TO WRK. DATA DIVISION. FILE SECTION. FD INPUT1. 01 INPUT1-STUDENT. 05 STUDENT-ID-I1 PIC 9(5). 05 STUDENT-NAME-I1 PIC A(25). FD INPUT2. 01 INPUT2-STUDENT. 05 STUDENT-ID-I2 PIC 9(5). 05 STUDENT-NAME-I2 PIC A(25). FD OUTPUT. 01 OUTPUT-STUDENT. 05 STUDENT-ID-O PIC 9(5). 05 STUDENT-NAME-O PIC A(25). SD WORK. 01 WORK-STUDENT. 05 STUDENT-ID-W PIC 9(5). 05 STUDENT-NAME-W PIC A(25). PROCEDURE DIVISION. MERGE WORK ON ASCENDING KEY STUDENT-ID-O USING INPUT1, INPUT2 GIVING OUTPUT. DISPLAY ”Merge Successful”. STOP RUN. JCL to execute the above COBOL program − //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO //IN1 DD DSN=INPUT1-FILE-NAME,DISP=SHR //IN2 DD DSN=INPUT2-FILE-NAME,DISP=SHR //OUT DD DSN = OUTPUT-FILE-NAME,DISP=SHR //WRK DD DSN = &&TEMP When you compile and execute the above program, it produces the following result − Merge Successful Print Page Previous Next Advertisements ”;
Category: cobol
COBOL Questions and Answers ”; Previous Next COBOL Questions and Answers has been designed with a special intention of helping students and professionals preparing for various Certification Exams and Job Interviews. This section provides a useful collection of sample Interview Questions and Multiple Choice Questions (MCQs) and their answers with appropriate explanations. SN Question/Answers Type 1 COBOL Interview Questions This section provides a huge collection of COBOL Interview Questions with their answers hidden in a box to challenge you to have a go at them before discovering the correct answer. 2 COBOL Online Quiz This section provides a great collection of COBOL Multiple Choice Questions (MCQs) on a single page along with their correct answers and explanation. If you select the right option, it turns green; else red. 3 COBOL Online Test If you are preparing to appear for a Java and COBOL Framework related certification exam, then this section is a must for you. This section simulates a real online test along with a given timer which challenges you to complete the test within a given time-frame. Finally you can check your overall test score and how you fared among millions of other candidates who attended this online test. 4 COBOL Mock Test This section provides various mock tests that you can download at your local machine and solve offline. Every mock test is supplied with a mock test key to let you verify the final score and grade yourself. Print Page Previous Next Advertisements ”;
COBOL – Conditional Statements ”; Previous Next Conditional statements are used to change the execution flow depending on certain conditions specified by the programmer. Conditional statements will always evaluate to true or false. Conditions are used in IF, Evaluate, and Perform statements. The different types of conditions are as follows − IF Condition Statement Relation Condition Sign Condition Class Condition Condition-Name Condition Negated Condition Combined Condition IF Condition Statement IF statement checks for conditions. If a condition is true, the IF block is executed; and if the condition is false, the ELSE block is executed. END-IF is used to end the IF block. To end the IF block, a period can be used instead of END-IF. But it is always preferable to use END-IF for multiple IF blocks. Nested-IF − IF blocks appearing inside another IF block. There is no limit to the depth of nested IF statements. Syntax Following is the syntax of IF condition statements − IF [condition] THEN [COBOL statements] ELSE [COBOL statements] END-IF. Example Live Demo IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-NUM1 PIC 9(9). 01 WS-NUM2 PIC 9(9). 01 WS-NUM3 PIC 9(5). 01 WS-NUM4 PIC 9(6). PROCEDURE DIVISION. A000-FIRST-PARA. MOVE 25 TO WS-NUM1 WS-NUM3. MOVE 15 TO WS-NUM2 WS-NUM4. IF WS-NUM1 > WS-NUM2 THEN DISPLAY ”IN LOOP 1 – IF BLOCK” IF WS-NUM3 = WS-NUM4 THEN DISPLAY ”IN LOOP 2 – IF BLOCK” ELSE DISPLAY ”IN LOOP 2 – ELSE BLOCK” END-IF ELSE DISPLAY ”IN LOOP 1 – ELSE BLOCK” END-IF. STOP RUN. JCL to execute the above COBOL program − //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO When you compile and execute the above program, it produces the following result − IN LOOP 1 – IF BLOCK IN LOOP 2 – ELSE BLOCK Relation Condition Relation condition compares two operands, either of which can be an identifier, literal, or arithmetic expression. Algebraic comparison of numeric fields is done regardless of size and usage clause. For non-numeric operands If two non-numeric operands of equal size are compared, then the characters are compared from left with the corresponding positions till the end is reached. The operand containing greater number of characters is declared greater. If two non-numeric operands of unequal size are compared, then the shorter data item is appended with spaces at the end till the size of the operands becomes equal and then compared according to the rules mentioned in the previous point. Syntax Given below is the syntax of Relation condition statements − [Data Name/Arithmetic Operation] [IS] [NOT] [Equal to (=),Greater than (>), Less than (<), Greater than or Equal (>=), Less than or equal (<=) ] [Data Name/Arithmetic Operation] Example Live Demo IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-NUM1 PIC 9(9). 01 WS-NUM2 PIC 9(9). PROCEDURE DIVISION. A000-FIRST-PARA. MOVE 25 TO WS-NUM1. MOVE 15 TO WS-NUM2. IF WS-NUM1 IS GREATER THAN OR EQUAL TO WS-NUM2 THEN DISPLAY ”WS-NUM1 IS GREATER THAN WS-NUM2” ELSE DISPLAY ”WS-NUM1 IS LESS THAN WS-NUM2” END-IF. STOP RUN. JCL to execute the above COBOL program − //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO When you compile and execute the above program it produces the following result − WS-NUM1 IS GREATER THAN WS-NUM2 Sign Condition Sign condition is used to check the sign of a numeric operand. It determines whether a given numeric value is greater than, less than, or equal to ZERO. Syntax Following is the syntax of Sign condition statements − [Data Name/Arithmetic Operation] [IS] [NOT] [Positive, Negative or Zero] [Data Name/Arithmetic Operation] Example Live Demo IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-NUM1 PIC S9(9) VALUE -1234. 01 WS-NUM2 PIC S9(9) VALUE 123456. PROCEDURE DIVISION. A000-FIRST-PARA. IF WS-NUM1 IS POSITIVE THEN DISPLAY ”WS-NUM1 IS POSITIVE”. IF WS-NUM1 IS NEGATIVE THEN DISPLAY ”WS-NUM1 IS NEGATIVE”. IF WS-NUM1 IS ZERO THEN DISPLAY ”WS-NUM1 IS ZERO”. IF WS-NUM2 IS POSITIVE THEN DISPLAY ”WS-NUM2 IS POSITIVE”. STOP RUN. JCL to execute the above COBOL program − //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO When you compile and execute the above program it produces the following result − WS-NUM1 IS NEGATIVE WS-NUM2 IS POSITIVE Class Condition Class condition is used to check if an operand contains only alphabets or numeric data. Spaces are considered in ALPHABETIC, ALPHABETIC-LOWER, and ALPHABETIC-UPPER. Syntax Following is the syntax of Class condition statements − [Data Name/Arithmetic Operation>] [IS] [NOT] [NUMERIC, ALPHABETIC, ALPHABETIC-LOWER, ALPHABETIC-UPPER] [Data Name/Arithmetic Operation] Example Live Demo IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-NUM1 PIC X(9) VALUE ”ABCD ”. 01 WS-NUM2 PIC 9(9) VALUE 123456789. PROCEDURE DIVISION. A000-FIRST-PARA. IF WS-NUM1 IS ALPHABETIC THEN DISPLAY ”WS-NUM1 IS ALPHABETIC”. IF WS-NUM1 IS NUMERIC THEN DISPLAY ”WS-NUM1 IS NUMERIC”. IF WS-NUM2 IS NUMERIC THEN DISPLAY ”WS-NUM2 IS NUMERIC”. STOP RUN. JCL to execute the above COBOL program − //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO When you compile and execute the above program, it produces the following result − WS-NUM1 IS ALPHABETIC WS-NUM2 IS NUMERIC Condition-name Condition A condition-name is a user-defined name. It contains a set of values specified by the user. It behaves like Boolean variables. They are defined with level number 88. It will not have a PIC clause. Syntax Following is the syntax of user-defined condition statements − 88 [Condition-Name] VALUE [IS, ARE] [LITERAL] [THRU LITERAL]. Example Live Demo IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-NUM PIC 9(3). 88 PASS VALUES ARE 041 THRU 100. 88 FAIL VALUES ARE 000 THRU 40. PROCEDURE DIVISION. A000-FIRST-PARA. MOVE 65 TO WS-NUM. IF PASS DISPLAY ”Passed with ” WS-NUM ” marks”. IF FAIL DISPLAY ”FAILED with ” WS-NUM ”marks”. STOP RUN. JCL to execute the above COBOL program − //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO When you compile and execute the above program, it produces the following result − Passed with 065 marks Negated Condition Negated condition is given by using the NOT keyword. If a condition
COBOL – Home
COBOL Tutorial PDF Version Quick Guide Resources Job Search Discussion COBOL stands for Common Business Oriented Language. The US Department of Defense, in a conference, formed CODASYL (Conference on Data Systems Language) to develop a language for business data processing needs which is now known as COBOL. COBOL is used for writing application programs and we cannot use it to write system software. The applications like those in defense domain, insurance domain, etc. which require huge data processing make extensive use of COBOL. Audience This tutorial is designed for software programmers who would like to learn the basics of COBOL. It provides enough understanding on COBOL programming language from where you can take yourself to a higher level of expertise. Prerequisites Before proceeding with this tutorial, you should have a basic understanding of computer programming terminologies and JCL. A basic understanding of any of the programming languages will help you understand the concepts of COBOL programming and move fast on the learning track. What is COBOL and Why is it Important in the Business World? Common Business Oriented Language (COBOL) is one of the oldest high-level programming languages. It was developed in the late 1950s for business applications and administrative systems. COBOL is known for its readability and easy-to-understand syntax that resembles natural English. COBOL can run on various platforms including mainframes, Windows, Linux, and Unix systems. The key features of COBOL include its readability, English-like syntax, and strong support for data processing and file handling. COBOL can be integrated with modern technologies such as APIs, web services, and databases. It can also work alongside other programming languages through interoperability features. Is COBOL Still Relevant Today? What are its Modern-Day Applications? COBOL is still extensively used in critical business systems. Many organizations rely on COBOL-based systems for transaction processing, payroll systems, and large-scale batch processing. COBOL plays a significant role in mainframe computing, running critical applications in banking, insurance, and government sectors. It is known for its reliability and efficiency in handling large-scale transaction processing. COBOL continues to be relevant for maintaining and updating legacy systems. To remain relevant, COBOL has been updated with modern programming concepts such as support for structured and object-oriented programming, enhancements in data handling capabilities, and improvements in interoperability with other systems and languages. Today, COBOL applications are not limited to just mainframes; they can run on modern platforms such as Windows, Linux, and cloud environments. COBOL”s adaptability has allowed it to integrate with web services, APIs, and contemporary databases. Its modern-day applications include handling high-volume transactions in banking systems and managing data in healthcare, government, and retail industries. Why should I Learn COBOL? One should learn COBOL because it is still widely used in legacy systems, especially in banking, finance, and government sectors. COBOL expertise can lead to job opportunities in maintaining and modernizing these systems. Key Features of COBOL that Make it Suitable for Business Applications COBOL has been designed specifically for business applications. Its English-like syntax can be easilly understood, even by business managers with no technical background in programming. COBOL can support complex data structures and precise numerical calculations, which is crucial for financial and administrative tasks. COBOL has very impressive file-handling features, which makes it so efficient at processing large volumes of data. COBOL”s compatibility with legacy systems ensures that existing applications can continue to operate seamlessly. Do I need Prior Programming Experience to Learn COBOL? This tutorial on COBOL is meant for beginners. Although prior programming experience can always be helpful, it is not absolutely necessary to start learning COBOL. Starting with COBOL involves understanding its unique syntax and structure, which are quite different from the modern-day programming languages. How do I write a Simple COBOL Program? A simple COBOL program consists of four divisions: IDENTIFICATION, ENVIRONMENT, DATA, and PROCEDURE. You can start by defining the program”s name and structure and then write the necessary code in the PROCEDURE DIVISION. How can I Practice COBOL Programming? You can practice COBOL by setting up a development environment, working on sample projects, participating in coding challenges, and contributing to open-source COBOL projects. We have a wonderful “Online COBOL Compiler” that you can use to execute COBOL programs. FAQs About COBOL There are some very Frequently Asked Questions(FAQ) about COBOL, this section tries to answer them briefly. What is a Data Type in COBOL? Data types in COBOL define the type of data that can be stored in a variable. Common data types include PIC X for alphanumeric, PIC 9 for numeric, and PIC S9 for signed numbers. How do I Define Variables in COBOL? Variables are defined in the DATA DIVISION, specifically in the WORKING-STORAGE SECTION. You use the PIC clause to specify the data type and size. What is a COBOL Paragraph? A paragraph in COBOL is a block of code identified by a name followed by a period. Paragraphs group related instructions and can be executed as a unit. How do I perform arithmetic operations in COBOL? You can perform arithmetic operations using the ADD, SUBTRACT, MULTIPLY, DIVIDE, and COMPUTE verbs. What is a file in COBOL? In COBOL, a file is a collection of records. Files are used for storing data that a program can read from or write to, typically defined in the FILE SECTION of the DATA DIVISION. You can use the OPEN, READ, WRITE, and CLOSE verbs to manage file operations in COBOL. What is a COBOL copybook? A copybook is a reusable code module that contains data definitions. It can be included in multiple programs using the COPY statement. What are COBOL control structures?
COBOL – Environment Setup
COBOL – Environment Setup ”; Previous Next We have set up the COBOL Programming environment online, so that you can compile and execute all the available examples online. It gives you confidence in what you are reading and enables you to verify the programs with different options. Feel free to modify any example and execute it online. IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. PROCEDURE DIVISION. DISPLAY ”Hello World”. STOP RUN. For most of the examples given in this tutorial, you will find a Try it option in our website code sections at the top right corner that will take you to the online compiler. So just make use of it and enjoy your learning. Installing COBOL on Windows/Linux There are many Free Mainframe Emulators available for Windows which can be used to write and learn simple COBOL programs. One such emulator is Hercules, which can be easily installed on Windows by following a few simple steps as given below − Download and install the Hercules emulator, which is available from the Hercules” home site: www.hercules-390.eu Once you have installed the package on Windows machine, it will create a folder like C:/hercules/mvs/cobol. Run the Command Prompt (CMD) and reach the directory C:/hercules/mvs/cobol on CMD. The complete guide on various commands to write and execute a JCL and COBOL programs can be found at: www.jaymoseley.com/hercules/installmvs/instmvs2.htm Hercules is an open-source software implementation of the mainframe System/370 and ESA/390 architectures, in addition to the latest 64-bit z/Architecture. Hercules runs under Linux, Windows, Solaris, FreeBSD, and Mac OS X. A user can connect to a mainframe server in a number of ways such as thin client, dummy terminal, Virtual Client System (VCS), or Virtual Desktop System (VDS). Every valid user is given a login id to enter into the Z/OS interface (TSO/E or ISPF). Compiling COBOL Programs In order to execute a COBOL program in batch mode using JCL, the program needs to be compiled, and a load module is created with all the sub-programs. The JCL uses the load module and not the actual program at the time of execution. The load libraries are concatenated and given to the JCL at the time of execution using JCLLIB or STEPLIB. There are many mainframe compiler utilities available to compile a COBOL program. Some corporate companies use Change Management tools like Endevor, which compiles and stores every version of the program. This is useful in tracking the changes made to the program. //COMPILE JOB ,CLASS=6,MSGCLASS=X,NOTIFY=&SYSUID //* //STEP1 EXEC IGYCRCTL,PARM=RMODE,DYNAM,SSRANGE //SYSIN DD DSN=MYDATA.URMI.SOURCES(MYCOBB),DISP=SHR //SYSLIB DD DSN=MYDATA.URMI.COPYBOOK(MYCOPY),DISP=SHR //SYSLMOD DD DSN=MYDATA.URMI.LOAD(MYCOBB),DISP=SHR //SYSPRINT DD SYSOUT=* //* IGYCRCTL is an IBM COBOL compiler utility. The compiler options are passed using the PARM parameter. In the above example, RMODE instructs the compiler to use relative addressing mode in the program. The COBOL program is passed using the SYSIN parameter. Copybook is the library used by the program in SYSLIB. Executing COBOL Programs Given below is a JCL example where the program MYPROG is executed using the input file MYDATA.URMI.INPUT and produces two output files written to the spool. //COBBSTEP JOB CLASS=6,NOTIFY=&SYSUID // //STEP10 EXEC PGM=MYPROG,PARM=ACCT5000 //STEPLIB DD DSN=MYDATA.URMI.LOADLIB,DISP=SHR //INPUT1 DD DSN=MYDATA.URMI.INPUT,DISP=SHR //OUT1 DD SYSOUT=* //OUT2 DD SYSOUT=* //SYSIN DD * //CUST1 1000 //CUST2 1001 /* The load module of MYPROG is located in MYDATA.URMI.LOADLIB. This is important to note that the above JCL can be used for a non-DB2 COBOL module only. Executing COBOL-DB2 programs For running a COBOL-DB2 program, a specialized IBM utility is used in the JCL and the program; DB2 region and required parameters are passed as input to the utility. The steps followed in running a COBOL-DB2 program are as follows − When a COBOL-DB2 program is compiled, a DBRM (Database Request Module) is created along with the load module. The DBRM contains the SQL statements of the COBOL programs with its syntax checked to be correct. The DBRM is bound to the DB2 region (environment) in which the COBOL will run. This can be done using the IKJEFT01 utility in a JCL. After the bind step, the COBOL-DB2 program is run using IKJEFT01 (again) with the load library and the DBRM library as the input to the JCL. //STEP001 EXEC PGM=IKJEFT01 //* //STEPLIB DD DSN=MYDATA.URMI.DBRMLIB,DISP=SHR //* //input files //output files //SYSPRINT DD SYSOUT=* //SYSABOUT DD SYSOUT=* //SYSDBOUT DD SYSOUT=* //SYSUDUMP DD SYSOUT=* //DISPLAY DD SYSOUT=* //SYSOUT DD SYSOUT=* //SYSTSPRT DD SYSOUT=* //SYSTSIN DD * DSN SYSTEM(SSID) RUN PROGRAM(MYCOBB) PLAN(PLANNAME) PARM(parameters to cobol program) – LIB(”MYDATA.URMI.LOADLIB”) END /* In the above example, MYCOBB is the COBOL-DB2 program run using IKJEFT01. Please note that the program name, DB2 Sub-System Id (SSID), and DB2 Plan name are passed within the SYSTSIN DD statement. The DBRM library is specified in the STEPLIB. Print Page Previous Next Advertisements ”;
COBOL – Overview
COBOL – Overview ”; Previous Next Introduction to COBOL COBOL is a high-level language. One must understand the way COBOL works. Computers only understand machine code, a binary stream of 0s and 1s. COBOL code must be converted into machine code using a compiler. Run the program source through a compiler. The compiler first checks for any syntax errors and then converts it into machine language. The compiler creates an output file which is known as load module. This output file contains executable code in the form of 0s and 1s. Evolution of COBOL During 1950s, when the businesses were growing in the western part of the world, there was a need to automate various processes for ease of operation and this gave birth to a high-level programming language meant for business data processing. In 1959, COBOL was developed by CODASYL (Conference on Data Systems Language). The next version, COBOL-61, was released in 1961 with some revisions. In 1968, COBOL was approved by ANSI as a standard language for commercial use (COBOL-68). It was again revised in 1974 and 1985 to develop subsequent versions named COBOL-74 and COBOL-85 respectively. In 2002, Object-Oriented COBOL was released, which could use encapsulated objects as a normal part of COBOL programming. Importance of COBOL COBOL was the first widely used high-level programming language. It is an English-like language which is user friendly. All the instructions can be coded in simple English words. COBOL is also used as a self-documenting language. COBOL can handle huge data processing. COBOL is compatible with its previous versions. COBOL has effective error messages and so, resolution of bugs is easier. Features of COBOL Standard Language COBOL is a standard language that can be compiled and executed on machines such as IBM AS/400, personal computers, etc. Business Oriented COBOL was designed for business-oriented applications related to financial domain, defense domain, etc. It can handle huge volumes of data because of its advanced file handling capabilities. Robust Language COBOL is a robust language as its numerous debugging and testing tools are available for almost all computer platforms. Structured Language Logical control structures are available in COBOL which makes it easier to read and modify. COBOL has different divisions, so it is easy to debug. Print Page Previous Next Advertisements ”;
COBOL – Data Layout
COBOL – Data Layout ”; Previous Next COBOL layout is the description of use of each field and the values present in it. Following are the data description entries used in COBOL − Redefines Clause Renames Clause Usage Clause Copybooks Redefines Clause Redefines clause is used to define a storage with different data description. If one or more data items are not used simultaneously, then the same storage can be utilized for another data item. So the same storage can be referred with different data items. Syntax Following is the syntax for Redefines clause − 01 WS-OLD PIC X(10). 01 WS-NEW1 REDEFINES WS-OLD PIC 9(8). 01 WS-NEW2 REDEFINES WS-OLD PIC A(10). Following are the details of the used parameters − WS-OLD is Redefined Item WS-NEW1 and WS-NEW2 are Redefining Item Level numbers of redefined item and redefining item must be the same and it cannot be 66 or 88 level number. Do not use VALUE clause with a redefining item. In File Section, do not use a redefines clause with 01 level number. Redefines definition must be the next data description you want to redefine. A redefining item will always have the same value as a redefined item. Example Live Demo IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-DESCRIPTION. 05 WS-DATE1 VALUE ”20140831”. 10 WS-YEAR PIC X(4). 10 WS-MONTH PIC X(2). 10 WS-DATE PIC X(2). 05 WS-DATE2 REDEFINES WS-DATE1 PIC 9(8). PROCEDURE DIVISION. DISPLAY “WS-DATE1 : “WS-DATE1. DISPLAY “WS-DATE2 : “WS-DATE2. STOP RUN. JCL to execute the above COBOL program − //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO When you compile and execute the above program it produces the following result − WS-DATE1 : 20140831 WS-DATE2 : 20140831 Renames Clause Renames clause is used to give different names to existing data items. It is used to re-group the data names and give a new name to them. The new data names can rename across groups or elementary items. Level number 66 is reserved for renames. Syntax Following is the syntax for Renames clause − 01 WS-OLD. 10 WS-A PIC 9(12). 10 WS-B PIC X(20). 10 WS-C PIC A(25). 10 WS-D PIC X(12). 66 WS-NEW RENAMES WS-A THRU WS-C. Renaming is possible at same level only. In the above example, WS-A, WS-B, and WS-C are at the same level. Renames definition must be the next data description you want to rename. Do not use Renames with the level numbers 01 or, 77. The data names used for renames must come in sequence. Data items with occur clause cannot be renamed. Example Live Demo IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-DESCRIPTION. 05 WS-NUM. 10 WS-NUM1 PIC 9(2) VALUE 20. 10 WS-NUM2 PIC 9(2) VALUE 56. 05 WS-CHAR. 10 WS-CHAR1 PIC X(2) VALUE ”AA”. 10 WS-CHAR2 PIC X(2) VALUE ”BB”. 66 WS-RENAME RENAMES WS-NUM2 THRU WS-CHAR2. PROCEDURE DIVISION. DISPLAY “WS-RENAME : ” WS-RENAME. STOP RUN. JCL to execute the above COBOL program − //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO When you compile and execute the above program, it produces the following result − WS-RENAME : 56AABB Usage Clause Usage clause specifies the operating system in which the format data is stored. It cannot be used with level numbers 66 or 88. If usage clause is specified on a group, then all the elementary items will have the same usage clause. The different options available with Usage clause are as follows − Display Data item is stored in ASCII format and each character will take 1 byte. It is default usage. The following example calculates the number of bytes required − 01 WS-NUM PIC S9(5)V9(3) USAGE IS DISPLAY. It requires 8 bytes as sign and decimal doesn”t require any byte. 01 WS-NUM PIC 9(5) USAGE IS DISPLAY. It requires 5 bytes as sign. COMPUTATIONAL / COMP Data item is stored in binary format. Here, data items must be integer. The following example calculates the number of bytes required − 01 WS-NUM PIC S9(n) USAGE IS COMP. If ”n” = 1 to 4, it takes 2 bytes. If ”n” = 5 to 9, it takes 4 bytes. If ”n” = 10 to 18, it takes 8 bytes. COMP-1 Data item is similar to Real or Float and is represented as a single precision floating point number. Internally, data is stored in hexadecimal format. COMP-1 does not accept PIC clause. Here 1 word is equal to 4 bytes. COMP-2 Data item is similar to Long or Double and is represented as double precision floating point number. Internally, data is stored in hexadecimal format. COMP-2 does not specify PIC clause. Here 2 word is equal to 8 bytes. COMP-3 Data item is stored in packed decimal format. Each digit occupies half a byte (1 nibble) and the sign is stored at the rightmost nibble. The following example calculates the number of bytes required − 01 WS-NUM PIC 9(n) USAGE IS COMP. Number of bytes = n/2 (If n is even) Number of bytes = n/2 + 1(If n is odd, consider only integer part) 01 WS-NUM PIC 9(4) USAGE IS COMP-3 VALUE 21. It requires 2 bytes of storage as each digit occupies half a byte. 01 WS-NUM PIC 9(5) USAGE IS COMP-3 VALUE 21. It requires 3 bytes of storage as each digit occupies half a byte. Copybooks A COBOL copybook is a selection of code that defines data structures. If a particular data structure is used in many programs, then instead of writing the same data structure again, we can use copybooks. We use the COPY statement to include a copybook in a program. COPY statement is used in the WorkingStorage Section. The following example includes a copybook inside a COBOL program − DATA DIVISION. WORKING-STORAGE SECTION. COPY ABC. Here ABC is the copybook name. The following data items in ABC copybook can be used inside a program. 01 WS-DESCRIPTION. 05 WS-NUM. 10 WS-NUM1 PIC 9(2) VALUE 20. 10 WS-NUM2 PIC 9(2) VALUE 56. 05 WS-CHAR. 10
COBOL – Program Structure
COBOL – Program Structure ”; Previous Next A COBOL program structure consists of divisions as shown in the following image − A brief introduction of these divisions is given below − Sections are the logical subdivision of program logic. A section is a collection of paragraphs. Paragraphs are the subdivision of a section or division. It is either a user-defined or a predefined name followed by a period, and consists of zero or more sentences/entries. Sentences are the combination of one or more statements. Sentences appear only in the Procedure division. A sentence must end with a period. Statements are meaningful COBOL statements that perform some processing. Characters are the lowest in the hierarchy and cannot be divisible. You can co-relate the above-mentioned terms with the COBOL program in the following example − PROCEDURE DIVISION. A0000-FIRST-PARA SECTION. FIRST-PARAGRAPH. ACCEPT WS-ID – Statement-1 —–| MOVE ”10” TO WS-ID – Statement-2 |– Sentence – 1 DISPLAY WS-ID – Statement-3 —–| . Divisions A COBOL program consists of four divisions. Identification Division It is the first and only mandatory division of every COBOL program. The programmer and the compiler use this division to identify the program. In this division, PROGRAM-ID is the only mandatory paragraph. PROGRAM-ID specifies the program name that can consist 1 to 30 characters. Try the following example using the Live Demo option online. Live Demo IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. PROCEDURE DIVISION. DISPLAY ”Welcome to Tutorialspoint”. STOP RUN. Given below is the JCL to execute the above COBOL program. //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO When you compile and execute the above program, it produces the following result − Welcome to Tutorialspoint Environment Division Environment division is used to specify input and output files to the program. It consists of two sections − Configuration section provides information about the system on which the program is written and executed. It consists of two paragraphs − Source computer − System used to compile the program. Object computer − System used to execute the program. Input-Output section provides information about the files to be used in the program. It consists of two paragraphs − File control − Provides information of external data sets used in the program. I-O control − Provides information of files used in the program. ENVIRONMENT DIVISION. CONFIGURATION SECTION. SOURCE-COMPUTER. XXX-ZOS. OBJECT-COMPUTER. XXX-ZOS. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT FILEN ASSIGN TO DDNAME ORGANIZATION IS SEQUENTIAL. Data Division Data division is used to define the variables used in the program. It consists of four sections − File section is used to define the record structure of the file. Working-Storage section is used to declare temporary variables and file structures which are used in the program. Local-Storage section is similar to Working-Storage section. The only difference is that the variables will be allocated and initialized every time a program starts execution. Linkage section is used to describe the data names that are received from an external program. COBOL Program IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT FILEN ASSIGN TO INPUT. ORGANIZATION IS SEQUENTIAL. ACCESS IS SEQUENTIAL. DATA DIVISION. FILE SECTION. FD FILEN 01 NAME PIC A(25). WORKING-STORAGE SECTION. 01 WS-STUDENT PIC A(30). 01 WS-ID PIC 9(5). LOCAL-STORAGE SECTION. 01 LS-CLASS PIC 9(3). LINKAGE SECTION. 01 LS-ID PIC 9(5). PROCEDURE DIVISION. DISPLAY ”Executing COBOL program using JCL”. STOP RUN. The JCL to execute the above COBOL program is as follows − //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO //INPUT DD DSN = ABC.EFG.XYZ,DISP = SHR When you compile and execute the above program, it produces the following result − Executing COBOL program using JCL Procedure Division Procedure division is used to include the logic of the program. It consists of executable statements using variables defined in the data division. In this division, paragraph and section names are user-defined. There must be at least one statement in the procedure division. The last statement to end the execution in this division is either STOP RUN which is used in the calling programs or EXIT PROGRAM which is used in the called programs. Live Demo IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-NAME PIC A(30). 01 WS-ID PIC 9(5) VALUE 12345. PROCEDURE DIVISION. A000-FIRST-PARA. DISPLAY ”Hello World”. MOVE ”TutorialsPoint” TO WS-NAME. DISPLAY “My name is : “WS-NAME. DISPLAY “My ID is : “WS-ID. STOP RUN. JCL to execute the above COBOL program − //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO When you compile and execute the above program, it produces the following result − Hello World My name is : TutorialsPoint My ID is : 12345 Print Page Previous Next Advertisements ”;
COBOL – Basic Syntax
COBOL – Basic Syntax ”; Previous Next Character Set ”Characters” are lowest in the hierarchy and they cannot be divided further. The COBOL Character Set includes 78 characters which are shown below − Sr.No. Character & Description 1 A-Z Alphabets(Upper Case) 2 a-z Alphabets (Lower Case) 3 0-9 Numeric 4 Space 5 + Plus Sign 6 – Minus Sign or Hyphen 7 * Asterisk 8 / Forward Slash 9 $ Currency Sign 10 , Comma 11 ; Semicolon 12 . Decimal Point or Period 13 “ Quotation Marks 14 ( Left Parenthesis 15 ) Right Parenthesis 16 > Greater than 17 < Less than 18 : Colon 19 ” Apostrophe 20 = Equal Sign Coding Sheet The source program of COBOL must be written in a format acceptable to the compilers. COBOL programs are written on COBOL coding sheets. There are 80 character positions on each line of a coding sheet. Character positions are grouped into the following five fields − Positions Field Description 1-6 Column Numbers Reserved for line numbers. 7 Indicator It can have Asterisk (*) indicating comments, Hyphen (-) indicating continuation and Slash ( / ) indicating form feed. 8-11 Area A All COBOL divisions, sections, paragraphs and some special entries must begin in Area A. 12-72 Area B All COBOL statements must begin in area B. 73-80 Identification Area It can be used as needed by the programmer. Example The following example shows a COBOL coding sheet − 000100 IDENTIFICATION DIVISION. 000100 000200 PROGRAM-ID. HELLO. 000101 000250* THIS IS A COMMENT LINE 000102 000300 PROCEDURE DIVISION. 000103 000350 A000-FIRST-PARA. 000104 000400 DISPLAY “Coding Sheet”. 000105 000500 STOP RUN. 000106 JCL to execute the above COBOL program − //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO When you compile and execute the above program, it produces the following result − Coding Sheet Character Strings Character strings are formed by combining individual characters. A character string can be a Comment, Literal, or COBOL word. All character strings must be ended with separators. A separator is used to separate character strings. Frequently used separators − Space, Comma, Period, Apostrophe, Left/Right Parenthesis, and Quotation mark. Comment A comment is a character string that does not affect the execution of a program. It can be any combination of characters. There are two types of comments − Comment Line A comment line can be written in any column. The compiler does not check a comment line for syntax and treats it for documentation. Comment Entry Comment entries are those that are included in the optional paragraphs of an Identification Division. They are written in Area B and programmers use it for reference. The text highlighted in Bold are the commented entries in the following example − 000100 IDENTIFICATION DIVISION. 000100 000150 PROGRAM-ID. HELLO. 000101 000200 AUTHOR. TUTORIALSPOINT. 000102 000250* THIS IS A COMMENT LINE 000103 000300 PROCEDURE DIVISION. 000104 000350 A000-FIRST-PARA. 000105 000360/ First Para Begins – Documentation Purpose 000106 000400 DISPLAY “Comment line”. 000107 000500 STOP RUN. 000108 JCL to execute above COBOL program − //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO When you compile and execute the above program, it produces the following result − Comment Line Literal Literal is a constant that is directly hard-coded in a program. In the following example, “Hello World” is a literal. PROCEDURE DIVISION. DISPLAY ”Hello World”. There are two types of literals as discussed below − Alphanumeric Literal Alphanumeric Literals are enclosed in quotes or apostrophe. Length can be up to 160 characters. An apostrophe or a quote can be a part of a literal only if it is paired. Starting and ending of the literal should be same, either apostrophe or quote. Example The following example shows valid and invalid Alphanumeric Literals − Valid: ‘This is valid’ “This is valid” ‘This isn’’t invalid’ Invalid: ‘This is invalid” ‘This isn’t valid’ Numeric Literal A Numeric Literal is a combination of digits from 0 to 9, +, –, or decimal point. Length can be up to 18 characters. Sign cannot be the rightmost character. Decimal point should not appear at the end. Example The following example shows valid and invalid Numeric Literals − Valid: 100 +10.9 -1.9 Invalid: 1,00 10. 10.9- COBOL Word COBOL Word is a character string that can be a reserved word or a user-defined word. Length can be up to 30 characters. User-Defined User-defined words are used for naming files, data, records, paragraph names, and sections. Alphabets, digits, and hyphens are allowed while forming userdefined words. You cannot use COBOL reserved words. Reserved Words Reserved words are predefined words in COBOL. Different types of reserved words that we use frequently are as follows − Keywords like ADD, ACCEPT, MOVE, etc. Special characters words like +, -, *, <, <=, etc Figurative constants are constant values like ZERO, SPACES, etc. All the constant values of figurative constants are mentioned in the following table. Figurative Constants Sr.No. Figurative Constants & Description 1 HIGH-VALUES One or more characters which will be at the highest position in descending order. 2 LOW-VALUES One or more characters have zeros in binary representation. 3 ZERO/ZEROES One or more zero depending on the size of the variable. 4 SPACES One or more spaces. 5 QUOTES Single or double quotes. 6 ALL literal Fills the data-item with Literal. Print Page Previous Next Advertisements ”;
COBOL – Loop Statements
COBOL – Loop Statements ”; Previous Next There are some tasks that need to be done over and over again like reading each record of a file till its end. The loop statements used in COBOL are − Perform Thru Perform Until Perform Times Perform Varying Perform Thru Perform Thru is used to execute a series of paragraph by giving the first and last paragraph names in the sequence. After executing the last paragraph, the control is returned back. In-line Perform Statements inside the PERFORM will be executed till END-PERFORM is reached. Syntax Following is the syntax of In-line perform − PERFORM DISPLAY ”HELLO WORLD” END-PERFORM. Out-of-line Perform Here, a statement is executed in one paragraph and then the control is transferred to other paragraph or section. Syntax Following is the syntax of Out-of-line perform − PERFORM PARAGRAPH1 THRU PARAGRAPH2 Example Live Demo IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. PROCEDURE DIVISION. A-PARA. PERFORM DISPLAY ”IN A-PARA” END-PERFORM. PERFORM C-PARA THRU E-PARA. B-PARA. DISPLAY ”IN B-PARA”. STOP RUN. C-PARA. DISPLAY ”IN C-PARA”. D-PARA. DISPLAY ”IN D-PARA”. E-PARA. DISPLAY ”IN E-PARA”. JCL to execute the above COBOL program. //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO When you compile and execute the above program, it produces the following result − IN A-PARA IN C-PARA IN D-PARA IN E-PARA IN B-PARA Perform Until In ‘perform until’, a paragraph is executed until the given condition becomes true. ‘With test before’ is the default condition and it indicates that the condition is checked before the execution of statements in a paragraph. Syntax Following is the syntax of perform until − PERFORM A-PARA UNTIL COUNT=5 PERFORM A-PARA WITH TEST BEFORE UNTIL COUNT=5 PERFORM A-PARA WITH TEST AFTER UNTIL COUNT=5 Example Live Demo IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-CNT PIC 9(1) VALUE 0. PROCEDURE DIVISION. A-PARA. PERFORM B-PARA WITH TEST AFTER UNTIL WS-CNT>3. STOP RUN. B-PARA. DISPLAY ”WS-CNT : ”WS-CNT. ADD 1 TO WS-CNT. JCL to execute the above COBOL program − //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO When you compile and execute the above program, it produces the following result − WS-CNT : 0 WS-CNT : 1 WS-CNT : 2 WS-CNT : 3 Perform Times In ‘perform times’, a paragraph will be executed the number of times specified. Syntax Following is the syntax of perform times − PERFORM A-PARA 5 TIMES. Example Live Demo IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. PROCEDURE DIVISION. A-PARA. PERFORM B-PARA 3 TIMES. STOP RUN. B-PARA. DISPLAY ”IN B-PARA”. JCL to execute the above COBOL program − //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO When you compile and execute the above program, it produces the following result − IN B-PARA IN B-PARA IN B-PARA Perform Varying In perform varying, a paragraph will be executed till the condition in Until phrase becomes true. Syntax Following is the syntax of perform varying − PERFORM A-PARA VARYING A FROM 1 BY 1 UNTIL A = 5. Example Live Demo IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-A PIC 9 VALUE 0. PROCEDURE DIVISION. A-PARA. PERFORM B-PARA VARYING WS-A FROM 1 BY 1 UNTIL WS-A=5 STOP RUN. B-PARA. DISPLAY ”IN B-PARA ” WS-A. JCL to execute the above COBOL program − //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO When you compile and execute the above program, it produces the following result − IN B-PARA 1 IN B-PARA 2 IN B-PARA 3 IN B-PARA 4 GO TO Statement GO TO statement is used to change the flow of execution in a program. In GO TO statements, transfer goes only in the forward direction. It is used to exit a paragraph. The different types of GO TO statements used are as follows − Unconditional GO TO GO TO para-name. Conditional GO TO GO TO para-1 para-2 para-3 DEPENDING ON x. If ”x” is equal to 1, then the control will be transferred to the first paragraph; and if ”x” is equal to 2, then the control will be transferred to the second paragraph, and so on. Example Live Demo IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-A PIC 9 VALUE 2. PROCEDURE DIVISION. A-PARA. DISPLAY ”IN A-PARA” GO TO B-PARA. B-PARA. DISPLAY ”IN B-PARA ”. GO TO C-PARA D-PARA DEPENDING ON WS-A. C-PARA. DISPLAY ”IN C-PARA ”. D-PARA. DISPLAY ”IN D-PARA ”. STOP RUN. JCL to execute the above COBOL program: //SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO When you compile and execute the above program, it produces the following result: IN A-PARA IN B-PARA IN D-PARA Print Page Previous Next Advertisements ”;