Module 4
Module 4
CONDITION STATEMENTS are special group of COBOL statements that use selection
structures to check for specified conditions that control whether a statement or group of
statements will be executed.
The IF Statement
The IF statement is a kind of condition statement that compares two values. If the result
of the comparison is true, a specified statement or set of statements is executed. The format of
the IF statement is
IF condition
Statement(s).
Example:
IF condition
Statement(s)
ELSE
NEXT SENTENCE
Statement(s)
Example:
IF TRANSCODE = “D”
ADD AMOUNT TO BALANCE
ELSE
SUBTRACT AMOUNT FROM BALANCE.
RELATION CONDITIONS
1. IS EQUAL TO (=)
2. IS NOT EQUAL TO (NOT =)
3. IS LESS THAN (<)
4. IS NOT LESS THAN (NOT <)
5. IS GREATER THAN (>)
6. IS NOT GREATER THAN (NOT >)
7. IS GREATER THAN OR EQUAL TO (> =)
8. IS LESS THAN OR EQUAL TO (< =)
The general format of the relation conditions is
EQUAL TO
NOT EQUAL TO
LESS THAN
IF datafield-1 NOT LESS THAN datafield-2
GREATER THAN
NOT GREATER THAN
GREATER THAN OR EQUAL TO
LESS THAN OR EQUAL TO
CLASS CONDITIONS
COBOL can test the class type of a data field to determine whether the contents are
numeric or non numeric. The condition is called CLASS condition.
The general format is
NUMERIC
IF datafield-1 NOT NUMERIC
ALPHABETIC
NOT ALPHABETIC
The numeric test is valuable because it can be used to test the contents of a numeric data
field before using its contents in arithmetic calculations. When numeric data fields containing
non numeric data values are used in arithmetic calculations, errors are generated. The numeric
test prior to using a data field in calculations helps avoid such errors.
SIGN CONDITIONS
The sign of the contents of a numeric data field can be tested using sign condition
statements. A sign condition test can determine whether the contents of a data field are less
than zero, greater than zero, equal to zero, or not equal to zero. This statement has the
following format.
NEGATIVE
NOT NEGATIVE
IF datafield-1 POSITIVE
NOT POSITIVE
ZERO
NOT ZERO
Note that NOT POSITIVE is the same as NEGATIVE, and NOT NEGATIVE is the same as
POSITIVE. Similarly, NEGATIVE is the same as LESS THAN ZERO, and POSITIVE, is the same
as EQUAL TO or MORE THAN ZERO.
CONDITION NAMES
In COBOL, conditions can be defined and names assigned. This is done through the
WORKONG-STORAGE SECTION of the DATA DIVISION, using level 88. Defined conditions can be
referred to by name in the PROCEDURE DIVISION.
Example:
05 END-OF-FILE PIC X.
END-OF-FILE is a condition data field in the WORKING-STORAGE SECTION. This data field
can be given condition names with level number 88, and the condition names can be associated
with particular values of END-OF-FILE.
These level 88 entries are the condition names. The first condition name, NO-MORE-DATA,
is true if END-OF-FILE is equal to “Y”, the second condition name, MORE-DATA, is true if
END-OF-FILE is equal to “N”. Once defined, both condition names can be used in the PROCEDURE
DIVISION of a program.
PERFORM PROCESS-DATA
UNTIL NO-MORE-DATA.
LOGICAL OPERATORS
Compound condition statements evaluate more than one condition. These conditions are
joined using logical operators. LOGICAL operators are operators used with logical conditions, or
conditions that can always be evaluated as true or false.
AND
IF condition-1 OR condition-2
NOT
Examples:
IF COLLEGE-CODE = “CCMIT”
AND YR-LEVEL = 3
AND SEX-CODE = “M”
ADD 1 TO COUNTER.
It is a statement that transfers program control to specific part of the program. This
statement is one of the most powerful and widely used in COBOL statements. On the transferring
control to the specified paragraph, the paragraph executes all the statements in that paragraph,
and then returns to the statement immediately following the PERFORM statement.
1. Unconditional – a statement in which the specified action is taken. The format of the simplest
unconditional PERFORM statement is
a. PERFORM statement
Example:
MAIN-RTN.
PERFORM BSIT-RTN
PERFORM BSCS-RTN
BSIT-RTN.
MOVE STUDENT-NO TO P-STUDENT-NO.
MOVE STUDENT-NA TO P-STUDENT-NA.
BSCS-RTN.
WRITE OUTREC FROM PRT-AREA AFTER 1 LINE.
BSIT-RTN and BSCS-RTN are two paragraph names that will be executed immediately by the
PERFORM statement. The first PERFORM statement transfers control to paragraph
BSOR-RTN in which the two MOVE statements are executed, then the control returns to the
MAIN-RTN to execute the next unconditional PERFORM statement. Control will transfer to
the paragraph BSCS-RTN to execute the WRITE statement and again will return to the
calling routine to perform the next statement.
This statement will execute the paragraph several times depending on the integer
times specified on the statement.
Examples:
BSIT-RTN.
PERFORM CCMIT-RTN 5 TIMES.
CCMIT-RTN.
MOVE ‘COMPUTER’ TO PRT-AREA-COURSE.
WRITE OUTREC FROM DETAIL AFTER 1 LINE.
CCMIT-RTN will be executed five times. This will result to an output displayed below.
COMPUTER
COMPUTER
COMPUTER
COMPUTER
COMPUTER
This statement will perform the two paragraphs immediately, before returning to
the next statement following the PERFORM statement.
CCMIT-RTN.
PERFORM BSIT-RTN THRU BSIT-RTN-END.
BSIT-RTN.
MOVE STUDEN-NO TO PRT-STUDNO.
MOVE STUDEN-NA TO PRT-STUDNA.
MOVE COURSE TO PRT-COUR.
MOVE YEAR TO PRT-YR.
WRITE OUTREC FROM DETAILS.
ADD 1 TO TOTAL.
BSIT-RTN-END.
THRU in some old compilers can be used to replace the reserved word THROUGH.
2. Conditional – a statement in which the specified action may or may not be taken
depending on the result of the stated condition.
The two characteristics of this statement are,
1. It must make a comparison between two values, the result of which must be true or false.
2. It must contain an action. Whether or not this action is taken is dependent upon the
results of the comparison.
ENTRY
STATEMENTS
FALSE
TRUE
EXIT
When the PERFORM statement is used with a conditional clause, the condition is
tested before the specified paragraph is executed. If the condition is true, the paragraph
is not executed.
Example:
MAIN-RTN.
PERFORM BSCS-RTN THRU BSCS-RTN-END.
PERFORM BSIT-RTN UNTIL EOFSW=1.
STOP RUN.
BSCS-RTN.
OPEN INPUT INFILE, OUTPUT OUTFILE.
READ INFILE AT END MOVE 1 TO EOFSW GO TO BSCS-
RTN-END.
PERFORM HEADING-RTN.
BSCS-RTN-END.
BSIT-RTN.
READ INFILE AT END MOVE 1 TO EOFSW.
MOVE STUDEN-NO TO PRT-STUDNO.
MOVE STUDEN-NA TO PRT-STUDNA.
WRITE OUTREC FROM DETAILS AFTER 1.
READ INFILE AT END MOVE 1 TO EOFSW.
after the reserved word UNTIL will be tested. If the EOFSW is false, then the specified
paragraph in the PERFORM will be executed. Otherwise if it is true, then the next
statement will be performed, which is STOP RUN. The format below is also a valid
statement.
…
PERFORM BSIT-RTN THROUGH BSIT-RTN-END UNTIL
EOFSW = 1.
BSIT-RTN.
READ INFILE AT END MOVE 1 TO EOFSW.
MOVE STUDEN-NO TO PRT-STUDNO.
MOVE STUDEN-NA TO PRT-STUDNA.
WRITE OUTREC FROM DETAILS AFTER 1.
READ INFILE AT END MOVE 1 TO EOFSW.
BSIT-RTN-END.
This statement is used in TABLE handling. TABLE is a sequence of data values grouped
together with a common name. Each data item in the table is referred to as an ELEMENT.
The elements in the table are accessed using common table name and a SUBSCRIPT, which
references a specific table element. A subscript is a constant or a data field used to
access a specific element of a table. The syntax used to access the individual element of
a table is
Table-name (subscript)
The table-name is followed by at least one blank space, a left parenthesis, the
subscript, and a right parenthesis.
Tables are defined using OCCURS clause. This clause appears with the data-name
in the FILE SECTION or WORKING-STORAGE SECTION of the DATA DIVISION.
The syntax of the OCCURS clause is
The data-field is the name of the table. The integer values define the size, or the number
of elements in the table. The integer indicates the range of the subscripts that can be
used with this table and must be greater than zero.
1. The integer value in the OCCURS clause indicates the number of elements in the table. It
cannot be zero. The subscript range is from one through a given value.
2. The PIC clause must appear at the elementary data-field level, although the OCCURS
clause can appear either at the elementary or group level.
3. The subscript must follow the table name and be placed in parentheses. There must be
one or more spaces between the table name and the left parenthesis.
4. The subscript must be within the range of the table.
5. The subscript must be and integer. It can be either a constant or a numeric data-field.
6. If a data-field is used as a subscript, the data-field must be defined in the
WORKING-STORAGE SECTION or FILE SECTION as an integer with PIC 9s.
TABLE in COBOL is a one-dimensional array or a multi-dimensional array that holds similar data
items of fixed number of elements. PERFORM statement with VARYING option is a statement
used to execute a specified paragraph repeatedly with different values of a subscript or index.
The execution of this statement proceeds as follows:
1. Move the initial value to data-field.
2. Determine whether the condition is true.
3. If the condition is true, execute the statement following PERFORM statement; if the
condition is false, execute the specified paragraph-name.
4. Add the increment-value to data-field.
5. Go to step 2 and continue.
ENTRY
MOVE INITIAL
VALUE TO
DATA-FIELD
INCREASE
EXECUTE DATA-FIELD
SPECIFIED BY
PARAGRAPH INCREMENT-
VALUE
STATEMENT EXIT
Example
PROCEDURE DIVISION.
CCMIT-RTN.
READ STUDENT AT END MOVE 1 TO EOFSW.
PERFORM BSIT-RTN VARYING I FROM 1 BY 1 UNTIL 1 > 5.
COMPUTE AVE = TQ / 5.
MOVE AVE TO PRT-AVE.
WRITE OUTREC FROM DETAILS AFTER 1 LINE.
BSIT-RTN.
MOVE QUIZ (I) TO PRT-QUIZ (I).
ADD QUIZ (I) TO TQ.
Suppose the set of 5 quizzes are stored from the input file student.
DATA DIVISION .
FILE SECTION .
FD STUDENT
LABEL RECORD IS STANDARD
DATA RECORD IS INREC .
01 QUIZ OCCURS 5 TIMES PIC 99
The PERFORM statement will execute. Since this is a conditional perform statement with a
VARYING option, a data field will be first initialized and be compared with the limit value. If the
condition is false, specified paragraph will be executed and the control will return to the same
calling statement, with the data −field being incremented as specified in the same statement, and
again, to be compared with the limit value repeatedly. Until the final value exceeds the limit
value for the PERFORM statement to become true, then the control will proceed to the next
statement, thus terminating the PERFORM statement.
The table of quizzes is a one-level table and is used to accumulate total quiz. This is one
of the several uses of the table in many of the programming languages like COBOL. In our example
I is the only subscript to reference the table of QUIZ, which is a one-level table. I is being
initialized and compared, incremented and compared until I exceeds the limit value which is 5.
c. PERFORM paragraph name VARYING data –field-1 FROM initial-value BY increment-value UNTIL
condition (s) AFTER data-field-2 FROM initial-value BY increment-vale UNTIL condition(s).
The diagram below illustrates graphically how the PERFORM VARYING AFTER option works.
MOVE INITIAL
VALUE TO
DATA-FIELD-1
MOVE INITIAL
VALUE TO
DATA-FIELD-2
N N EXECUTE ADD
SPECIFIED INCREMENT
PARAGRAPH VALUE TO
Y Y DATA-FIELD-2
This statement is used to process a Two-level table with One-level-table we used only one
subscript and with Two-level table, there must be two subscript that represents the total number
of two-level table. The PERFORM statement with the VARYING AFTER option is used in COBOL to
allow automatic control over two subscript. The first subscript is the major subscript that
represents the total number of rows and the second subscript is the minor subscript that
represents the total number of columns.
DISPLAY Statement is a COBOL statement that is used for printing a low volume of output.
DISPLAY Statement is used to Display the data on Output device. This is a procedure
division statement and very useful to get the required data on output device. Identifier is any
declared variable of the program.
Format:
DISPLAY identifier-1… .
literal-1
DISPLAY identifier-1… LINE integer-value COLUMN integer-value.
literal-1
DISPLAY ( line-no. , column-no ) identifier-1… .
literal-1
Examples:
DISPLAY “ Average Grade is : “ .
DISPLAY AVERAGE .
DISPLAY “ Average Grade is : “ , AVERAGE .
DISPLAY “ Average Grade is : “ , AVERAGE LINE 5 COLUMN 15.
DISPLAY (5, 15) “ Average Grade is : “ , AVERAGE .
ACCEPT Statement is a COBOL statement that request actual data from the keyboard.
ACCEPT statement accepts the data from the outside of the program which may be from input
device or system defined data items. ACCEPT transfers the accepted data to the specified
identifier along with ACCEPT. ACCEPT statement used to receive the data during the program
execution.
Format:
ACCEPT identifier .
ACCEPT identifier LINE integer-value COLUMN integer-value.
ACCEPT (row, column ) identifier.
Example:
ACCEPT NAME.
ACCEPT NAME LINE 7 COLUMN 11 .
ACCEPT (7, 11 ) NAME .
IDENTIFICATION DIVISION.
PROGRAM-ID. ACPTDSP1.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT GRDFILE ASSIGN TO 'C:\COBOL\GRDFILE.TXT'.
DATA DIVISION.
FILE SECTION.
FD GRDFILE.
01 GRDREC.
05 FILLER PIC X(80).
WORKING-STORAGE SECTION.
* INPUT DATA USING *
* ACCEPT & DISPLAY *
01 VARIABLES.
05 NAME PIC X(25).
05 MIDGRD PIC 9V99.
05 FINGRD PIC 9V99.
05 AVE PIC 9V99.
05 ANS PIC X.
05 VALIDANS PIC 9 VALUE 0.
* OUTPUT FILE *
01 HDG.
05 FILLER PIC X(32) VALUE SPACES.
05 FILLER PIC X(25) VALUE "STUDENT'S GRADE".
05 FILLER PIC X(33) VALUE SPACES.
01 COLHDG.
05 FILLER PIC X(14) VALUE SPACES.
05 FILLER PIC X(12) VALUE "STUDENT NAME".
05 FILLER PIC X(28) VALUE SPACES.
05 FILLER PIC X(12) VALUE "FINAL GRADE ".
01 GRDDATA.
05 FILLER PIC X(14) VALUE SPACES.
05 NAME-OUT PIC X(25).
05 FILLER PIC X(20) VALUE SPACES.
05 AVE-OUT PIC 9.999.
PROCEDURE DIVISION.
MAIN-RTN.
OPEN OUTPUT GRDFILE.
WRITE GRDREC FROM HDG AFTER 1.
WRITE GRDREC FROM COLHDG AFTER 1.
PERFORM PROCESS-RTN THRU PROCESS-END
UNTIL VALIDANS = 1.
CLOSE GRDFILE.
STOP RUN.
PROCESS-RTN.
DISPLAY "Enter Name: ".
ACCEPT NAME.
DISPLAY "Enter Midterm Grade: ".
ACCEPT MIDGRD.
DISPLAY "Enter Final Grade: ".
ACCEPT FINGRD.
COMPUTE AVE = (MIDGRD + FINGRD) / 2.
MOVE NAME TO NAME-OUT.
MOVE AVE TO AVE-OUT.
DISPLAY "Average Grade is: ", AVE-OUT.
WRITE GRDREC FROM GRDDATA AFTER 2.
DISPLAY "ENTER ANOTHER [Y/N]? ".
PERFORM ANS-RTN THRU ANS-END.
PROCESS-END.
ANS-RTN.
ACCEPT ANS.
IF ANS = 'Y' OR ANS = 'y'
PERFORM PROCESS-RTN THRU PROCESS-END
ELSE IF ANS = 'N' OR ANS = 'n'
MOVE 1 TO VALIDANS
ELSE
DISPLAY "INVALID INPUT"
DISPLAY "ENTER ANOTHER [Y/N]? "
PERFORM ANS-RTN THRU ANS-END.
ANS-END.
Sample Run 1: Sample Run 2:
Enter Name: Rainah Casandra A. Nayre Enter Name: Mark Jezekiah A. Nayre
Course Materials:
https://www.tutorialbrain.com/mainframe/cobol_divisions/
https://devops.com/the-beauty-of-the-cobol-programming-language-v
https://www.infoworld.com/article/3539057/what-is-cobol-cobol-programming-explained.html
http://www.3480-3590-data-conversion.com/article-reading-cobol-layouts-1.html