There can be more than one condition names associated to a conditional variable.
Syntax:
Example program:
IDENTIFICATION DIVISION.
PROGRAM-ID. CONDNAMECOND.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 AGE PIC 9(2).
88 A1 VALUE IS 01 THRU 12.
88 A2 VALUE IS 13 THRU 19.
88 A3 VALUE IS 20 THRU 99.
PROCEDURE DIVISION.
PARA-1.
DISPLAY “ENTER AGE”.
ACCEPT AGE.
IF A1
DISPLAY “CHILDHOOD”.
IF A2
DISPLAY “TEEN AGE”.
IF A3
DISPLAY “ADULTS”.
STOP RUN.
NEGATED SIMPLE CONDITION:
Any of the simple described above can be preceded by the logical operator or NOT. The effect of placing the operator NOT before a simple condition is to reverse the value of the condition.
Example program:
IDENTIFICATION DIVISION.
PROGRAM-ID. NEGSIMP.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 A PIC 9(2).
77 B PIC 9(2).
PROCEDURE DIVISION.
READ-PARA.
DISPLAY “ENTER A VALUE”
ACCEPT A.
DISPLAY “ENTER B VALUE”.
ACCEPT B.
CAL-PARA.
IF A IS NOT GREATER THAN B
DISPLAY “A IS SMALLER THAN B”.
STOP RUN.
COMPOUND CONDITON:
Two simple conditions can be connected by the logical operators AND or OR to form a compound condition (also known as combined condition).
When two conditions are combined by AND, the compound condition becomes true only when both the conditions are true.
If OR is used to combine two condition, the compound condition is true if either or both the conditions are true.
General form:
.
Example program:
IDENTIFICATION DIVISION.
PROGRAM-ID. COMPCOND.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 A PIC 9(2).
77 B PIC 9(3).
77 C PIC 9(3).
PROCEDURE DIVISION.
PARA-1.
DISPLAY “ENTER A VALUE”.
ACCEPT A.
DISPLAY “ENTER B VALUE”.
ACCEPT B.
DISPLAY “ENTER C VALUE”.
ACCEPT C.
PARA-2.
IF (A > B AND A > C)
DISPLAY “A IS BIG”
ELSE IF ( B > C AND B > A)
DISPLAY “B IS BIG”
ELSE
DISPLAY “C IS BIG”.
STOP RUN.
IF STATEMENT
The general form of IF statement is as follows:
The condition can be any one of the conditions discussed above. Each of statement-1 and statement-2 represents one or more COBOL statements. When more than one statement is specified, they must be separated by one or more spaces or by an optional semicolon (;) or comma (,).
During execution, if the condition is found to be true, the statements represented by statement-1 are executed. On the other hand, if the condition is found to be false, the statements represented by statement-2 are executed.
Flow chart:
False True
Example Program:
IDENTIFICATION DIVISION.
PROGRAM-ID. IFELSE.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 A PIC 9(2).
77 B PIC 9(3).
PROCEDURE DIVISION.
PARA-1.
DISPLAY “ENTER A VALUE”.
ACCEPT A.
DISPLAY “ENTER B VALUE”.
ACCEPT B.
PARA-2.
IF A > B
DISPLAY “A IS BIG”
ELSE
DISPLAY “B IS BIG”
STOP RUN.
NESTED IF:
One or more IF statements within another IF is called Nested IF.
Flow chart:
False True
False True
Example Program:
IDENTIFICATION DIVISION.
PROGRAM-ID. NESTEDIF
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 A PIC 9(2).
77 B PIC 9(3).
77 C PIC 9(3).
PROCEDURE DIVISION.
PARA-1.
DISPLAY “ENTER A VALUE”.
ACCEPT A.
DISPLAY “ENTER B VALUE”.
ACCEPT B.
DISPLAY “ENTER C VALUE”.
ACCEPT C.
PARA-2.
IF A > B
IF A > C
DISPLAY “A IS BIG”
ELSE
DISPLAY “C IS BIG”
ELSE IF B > C
DISPLAY “B IS BIG“
ELSE
DISPLAY “C IS BIG”.
STOP RUN.
GO TO WITH DEPENDING PHRASE
The GO TO statement with a DEPENDING phrase has the following form
The statement transfer control to one of the procedures named in the procedure names depending on the value of the identifier.
Depending on whether the value of the identifier is 1, 2, … n the control is transferred to procedure-name-1, procedure-name-2, … procedure-name-n respectively. If the value of the identifier is anything other than 1, 2, … n the said GO TO statement is ineffective and the control is transferred to the next statement is sequence. The identifier specified in the statement must be a numeric, integral elementary item.
Example:
GO TO PARA-1, PARA-2, PARA-3 DEPENDING ON CHOICE.
Example Program:
IDENTIFICATION DIVISION.
PROGRAM-ID. DEPGOTO.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 A PIC 9(2).
77 B PIC 9(3).
77 C PIC 9(3).
77 CHOICE PIC 9(3).
PROCEDURE DIVISION.
READ-PARA.
DISPLAY “ENTER A VALUE”.
ACCEPT A.
DISPLAY “ENTER B VALUE”.
ACCEPT B.
DISPLAY “ENTER YOUR CHOICE”.
ACCEPT CHOICE.
CHOICE-PARA.
GO TO ADD-PARA, SUB-PARA, MUL-PARA DEPENDING ON CHOICE.
ADD-PARA.
COMPUTE C = A + B.
GO TO END-PARA.
SUB-PARA.
COMPUTE C = A - B.
GO TO END-PARA.
MUL-PARA.
COMPUTE C = A * B.
GO TO END-PARA.
DIV-PARA.
COMPUTE C = A / B.
GO TO END-PARA.
END-PARA.
DISPLAY “C VALUE =” , C.
STOP RUN.
Condition-name condition
A condition-name condition tests a conditional variable to determine whether its value is equal to any values that are associated with the condition-name.
Format |
---|
|
A condition-name is used in conditions as an abbreviation for the relation condition. The rules for comparing a conditional variable with a condition-name value are the same as those specified for relation conditions.
If condition-name-1 has been associated with a range of values (or with several ranges of values), the conditional variable is tested to determine whether its value falls within the ranges, including the end values. The result of the test is true if one of the values that corresponds to the condition-name equals the value of its associated conditional variable.
Condition-names are allowed for alphanumeric, DBCS, national, and floating-point data items, as well as others, as defined for the condition-name format of the VALUE clause.
The following example illustrates the use of conditional variables and condition-names:
01 AGE-GROUP PIC 99.
88 INFANT VALUE 0.
88 BABY VALUE 1, 2.
88 CHILD VALUE 3 THRU 12.
88 TEENAGER VALUE 13 THRU 19.
AGE-GROUP is the conditional variable; INFANT, BABY, CHILD, and TEENAGER are condition-names. For individual records in the file, only one of the values specified in the condition-name entries can be present.
The following IF statements can be added to the above example to determine the age group of a specific record:
IF INFANT... (Tests for value 0)
IF BABY... (Tests for values 1, 2)
IF CHILD... (Tests for values 3 through 12)
IF TEENAGER... (Tests for values 13 through 19)
Depending on the evaluation of the condition-name condition, alternative paths of execution are taken by the object program.
Condition-name conditions and windowed date field comparisons
If the conditional variable is a windowed date field, then the values associated with its condition-names are treated like values of the windowed date field. That is, they are treated as if they were converted to expanded date format, as described under Semantics of windowed date fields.
For example, given YEARWINDOW(1945), a century window of 1945-2044, and the following definition:
05 DATE-FIELD PIC 9(6) DATE FORMAT YYXXXX.
88 DATE-TARGET VALUE 051220.
then a value of 051220 in DATE-FIELD would cause the following condition to be true:
IF DATE-TARGET...
because the value associated with DATE-TARGET and the value of DATE-FIELD would both be treated as if they were prefixed by "20" before comparison.
However, the following condition would be false:
IF DATE-FIELD = 051220...
because in a comparison with a windowed date field, literals are treated as if they were prefixed by "19" regardless of the century window. So the above condition effectively becomes:
IF 20051220 = 19051220...
No comments:
Post a Comment