CIS133C CLASS 8
CONDITION STATEMENTS are a 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 test element that determines whether or not an action is taken is called the condition.
An unconditional statement causes an action to be taken no matter what (sequence). A conditional statement causes an action to be taken depending on some type of condition (selection).
- A relation condition uses symbols or words to compare one value with another value. The IF statement is a relation condition. Two data quantities are linked by relational operators that define the type of comparison to be made and the result of evaluating the relational condition is either true or false.
The element before the operator is the subject of the relation and the element after the operator is the object. Usually the subject is specified as an identifier (field-name) and the object is either an identifier or a literal.
IF SUBJECT OPERATOR OBJECT . . . .
IF IDENTIFIER IS LESS THAN IDENTIFIER . . . .
IF IDENTIFIER IS NOT GREATER THAN LITERAL . . . .
Relational condition testing compares the subject and object values. The data class of the object of the relation condition should be consistent with the subject. (If the subject is numeric, the object should be numeric.) When both subject and object are defined as numeric data-items, comparison of the two values is made according to their algebraic values. The relative length of the two values does not matter. The result of alphanumeric comparisons depends upon the collating sequence of the computer used. Comparison begins at the leftmost character position and proceeds to the right, byte by byte. When the two fields are of different lengths, the shorter field is internally extended on the right with spaces until it is the same length as the longer field so the comparison can apply to fields of equal length.
- Nested IF statement can be replaced by an EVALUATE statement.
- Class condition tests whether a field contains only numeric digits or only alphabetic characters. NUMERIC class tests can be made on PIC X and PIC 9 fields but the field must contain only the digits 0 - 9 and an optional sign for the condition to be found true. ALPHABETIC class tests can be made on alphanumeric, alphabetic, and group fields but may contain uppercase A - Z, lowercase a - z, and space for the condition to be found true.
- Sign condition tests whether the value of a numeric field is POSITIVE, NEGATIVE, or ZERO.
- To help the readability of code, you can use a condition-name condition or 88-level. This is where you code 88 entries in the Data Division under the data-item with the value you want the data-item to have for the condition test.
LOGICAL OPERATORS:
- When the AND logical operator is used, both conditions must be true for the combined condition to be considered true. If either one or both of the conditions is false, the combined condition is considered false. You can use multiple AND's but if any one of the conditions is false the combined condition is considered false. Also note that different condition types can be combined.
- When the OR operator is used, the combined condition is considered true if any one or more of the conditions is/are true. Only if all conditions are false is the combined condition considered to be false.
- A combined IF statement is one that contains multiple conditions connected with the reserved-word logical operators AND and/or OR. Complex conditions have both AND and OR within the same IF statement. This gets confusing so apply the AND association first then the OR association. Unless, of course, you use parentheses to change the order to which the operators are applied. In fact, you should always use parentheses when writing complex conditions.
- To use the NOT operator, just insert NOT immediately before the condition to reverse the truth value of the condition to which it is applied. The NOT is evaluated first in complex conditions using NOT, AND, and OR unless overridden by parentheses. Don't confuse a negated condition (where NOT precedes the condition) with the word NOT used within the condition such as NOT EQUAL TO.
Background gif from . . .
|