COBOL – Conditional Statements

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?