COBOL Tutorial Contd
Statements in COBOL program executed one after another. In programming, many times, Based on some condition, program needs to run one or more sets of statements. If condition is true, one set of statements get excuted. If condition is false another set of statements will get executed.
Condition(s) are used in IF, EVALUATE, PERFORM and SEARCH statements.
Class condition determines whether the identifier contains numeric value OR alphanumeric value.
[IS] [NOT] < NUMERIC/ALPHABETIC/ALPHABETIC-LOWER/ALPHABETIC-UPPER >
This condition is very useful to minimize the program errors. there are many chances that program may receive corrupted data. To handle these kind of data errors, we can use this condition.
If we want to check whether the identifier contains the numeric data or not, before doing arthmetic operation on it, use the Class codition as below using IF statement.
IF WS-A IS NUMERIC ADD WS-A TO WS-BANLANCE ELSE DISPLAY 'Value is not numeric' END-IF.
The various other options are
used to check identifier contains all alphabetic letters or not ( i.e., A-Z, a-z and blank )
To check identifier contains only lower alphabetic letters or not. i.e., a-z and blank.
To check identifier contains only upper alphabetic letters or not. i.e., A-Z and blank.
Condition-name is a identifier defined with level number 88. It has only VALUE clause, it does not contain any picture clause, but it must always be associated to a data name called the conditional variable.
01 WS-VAR PIC X. 88 POSITIVE VALUE 'Y'. 88 NEGATIVE VALUE 'N'.
It is like boolean flag. Whenever the variable WS-VAR is populated with Y. POSITIVE flag is set true and when it is populated with N. The NEGATIVE flag is set to true
We can use these flags in the computations like below
IF POSITIVE // IF WS-VAR = 'Y' [do something] ELSE IF NEGATIVE // IF WS-VAR = 'N' [do some other thing] END-IF END-IF.
Relation condition compares two operands either of which can be an idenftifier, literal, arithmetic expression.
The Relational operator specifies the type of comparison to be made as shown in below table.
|IS >||Greater than|
|IS NOT >||Not Greater than|
|IS <||Less than|
|IS NOT <||Not Less than|
|IS =||Equal to|
|IS NOT =||Not equal to|
|IS >=||greater than or equal to|
|IS <=||lesser than or equal to|
The sign condition determines whether the given numeric value is grater than the ZERO (or) less than the ZERO (or) equal to ZERO.
operand-1 [IS] [NOT]
Operand-1 must be defined as a numeric identifier.
IS POSITIVE condition will become true, if the operand-1 value grater than ZERO
IS NEGATIVE condition will become true, if the operand-1 value less than ZERO
IS ZERO condition will become true, if the operand-1 contains ZERO
Negated simple condition
We can negate the simple condition by using keyword NOT. If the condition is true, by using NOT with the condition, overall boolean value of that condition will become false. If the simple condition is flase, by using NOT with the condition, overall boolean value of that condition will become true.
IF A = B DISPLAY 'A is equal to B' END-IF
if A & B contains the same value, COMPUTE statement will get executed.if we use NOT with the condition, COMPUTE wont get executed, when A & B contains the same value.
IF NOT A = B DISPLAY 'A not equal to B' END-IF
Combined conditions contains two or more conditions connected using logical operators AND or OR.
IF (A = B) AND (C = D) [do something] END-IF
In above IF condition, if both conditions are true, then COMPUTE statement inside IF will get executed.