Mainframe Module1.3
Mainframe Module1.3
a test.
• Relational
• Sign
• Class
• Negated simple
• Compound
• Condition names
1. Relational
– = EQUAL TO
– NOT
Determines whether or not the algebraic value of an operand is either positive, negative or zero
NEGATIVE
ZERO
3. Class
ALPHABETIC
4. Negated Simple
This condition places a NOT operator before a simple condition to reverse the condition.
5. Compound condition
This is used to connect two simple conditions by logical operators AND or OR to form a compound cond ition.
Condition Names are defined in the DATA DIVISION using the special level number 88. They are always associated
with a data-item and are defined immediately after the definition of the data -item.
A Condition Name is a name given to a specified subset of the values which its associated data-item can hold. Like a
condition, a Condition Name evaluates to True or False. In the below example, rather than checking in the IF condition
01 GENDER PIC 9.
88 MALE VALUE 1.
88 FEMALE VALUE 2.
IF MALE T HEN
DISPLAY ‘MALE’
ELSE
IF FEMALE
DISPLAY ‘FEMALE’
END-IF
END-IF
3.2. Cobol Sequence control statements
1. IF Statement
The IF command lets you conditionally perform a command. You can optionally specify an ELSE clause on the IF
command. If the test expression evaluates to false and an ELSE clause exists, the command associated with the ELSE
clause is performed. END-IF is the scope terminator which marks the logical end of IF stateme nt
Syntax:
IF {Condition}
Statements
ELSE
Statements
[END-IF].
Eg:-
ELSE
END-IF.
If the SAL is less than or equal to 2500, then Bonus will be computed as per the logic else we will move zero to Bonus.
Nested IF
When more than one condition is to be tested, an IF within an IF is used. The flow of control in a NESTED IF is shown
in the Fig 7.
Syntax:
IF {Condition}
IF {Condition}
Statements
ELSE
Statements
END-IF
ELSE
Statements
[END-IF].
Write a program to calculate the bonus of the employees. The criteria for calculating the bonus is that If the salary of
the employee is less than 20000 INR then the bonus should be calculated as 5 % of the salary. If the salary is more
than 20000 INR then it should be 8 % of the salary. Display the salary after adding the bonus to it. The salary of the
2. EVALUATE STATEMENT
Evaluate statement can be used instead of a set of IF statements to test several conditions. The syntax is given below.
END-EVALUATE is the scope terminator which will be the logical end of the Evaluate statement
EVALUATE Identifier-1
WHEN Identifier/Literal
Statement(s)
WHEN Identifier/Literal
Statement(s)
WHEN OTHER
Statement(s)]
END-EVALUATE
Example:
EVALUATE TRUE
WHEN OTHER
MOVE ‘ERROR' TO OUTPUT
END-EVALUATE.
It checks for the STAT field. If the first WHEN is satisfied then it will move 'FULLTIME' to OUTPUT and come out of the
loop. If the first condition is not satisfied, then it checks for the second WHEN and so on. If none of the conditions are
satisfied then it will go to WHEN OT HER and will move 'ERROR' to the output.
Problem statement #4
Write a program to categorize the students of a class into high, low and average performers. If the marks obtained in
the exam are more than 80, then the student should be categorized as a high performer. If the mark is between 60 &
80, then he is an average performer and if the mark is less than 60 then as a low performer. The mark for the subject
need to be accepted from the user and based on the marks, the performance of the student like high, average or low
The perform statement is mainly used to execute a group of consecutive statements written in the program. Perform
statement acts very similar to GO TO statement except that after execution of the paragraph the control will be
transferred to the statement following to the next statement after PERFORM statement. END -PERFORM is the scope
terminator which will be the logical end of the PERFORM statement. Fig 5 depicts the control flow using GOTO &
PERFORM.
PROCEDURE DIVISION.
MAIN-PARA.
PERFORM ADD-PARA
STOP RUN.
ADD-PARA.
In the above example we perform ADD-PARA will take the control to the ADD-PARA and execute the logic inside that
paragraph. Once the logic is performed the control will return to the next statement after PERFORM which is STOP
RUN. STOP RUN will give the control back to the Operating System.
PROCEDURE DIVISION.
MAIN-PARA.
STOP RUN.
ADD-PARA.
ADD-PARA-EXIT.
EXIT.
Here PARA1 and PARA2 are paragraph names. Execution of perform statement causes the control to transfer from the
first statement of the PARA1 to the last statement of PARA2 and then the control returns to the next executable
statement. Execution of perform statement will transfer the control to the ADD-PARA to the last statement of ADD-
PARA-EXIT. The last statement is EXIT statement which indicates the end of p aragraph execution and the control is
PROGRAM-ID. ADD1.
DATA DIVISION.
PROCEDURE DIVISION.
ADD-PARA.
STOP RUN.
In the below program we are doing addition and subtraction operations. The addition and the subtraction operations are
mentioned in 2 paragraphs. When we mention PERFORM ADD-PARA it will go to the ADD-PARA and do the logic
mentioned in the paragraph. Here it is adding 2 numbers. Similarly when we perform SUB-PARA it will go to that
particular paragraph and perform the logic present in that paragraph. Finally we are displaying the RESULT and
RESULT1.
PROGRAM-ID. SUB1.
DATA DIVISION.
PROCEDURE DIVISION.
MAIN-PARA.
PERFORM ADD-PARA
PERFORM SUB-PARA
STOP RUN.
ADD-PARA.
SUB-PARA.
In the above perform, all statements in PARA-1 are executed till the condition specified in the UNT IL becomes true. For
the first iteration the value of WS-I will be 1 (as it is specified after FROM), for second iteration WS-I value increases by
1 and becomes 2 (as it is specified after BY keyword). For each iteration the statements in PARA-1 will be executed.
This iteration continues till the condition becomes true (that is WS-I >10).
Format 4 – PERFORM paragraph-name N TIMES
In the above perform statement, logic in the paragraph PARA-1 will be executed 6 times. This format of the PERFORM
executes a block of code inside PARA-1 6 times before returning control to the statement following the PERFORM.
Problem statement #5
Write a program to find the factorial of a number (N). The number need to be provided as a user input and the factorial
Write a program to display the natural numbers from 1 till 5. The final output that needs to be displayed on the screen is
as shown below.
Problem statement #7
Write a program to print all the even numbers till 10. The final output that n eeds to be displayed on the screen is as
shown below.
10