 ## MODULE 7: COBOL Control statements

### COBOL’s selection statement

• COBOL’s selection statements such as IF, EVALUATE allows you to control the flow of your program’s execution based upon specified conditions.
• Condition is any expression that evaluates to either true or false. The result of condition is used to decide the program flow
• Condition(s) are used in IF, EVALUATE, PERFORM and SEARCH statements.
• Condition can take one of the following form:-
1. Relation condition
2. Sign condition
3. Class condition
4. Condition-name condition
5. Negated single condition
6. Combined condition
• Let's discuss each of them in detail

#### Relation condition

• It is used to compare two operands and has the following form:-

operand-1 [IS][ NOT] [relational-operator] operand-2

• Where,
• operand-1 and operand-2 can be identifier/literal/arithmetic-expression
• relational-operator can be:-
• relational-operator Can also be specified as
GREATER THAN
LESS THAN
EQUAL TO =
GREATER THAN OR EQUAL TO >=
LESS THAN OR EQUAL TO <=
• ‘IS’ is optional to specify
• ‘NOT’ is optional to specify and it is used to negate condition. For example, ’NOT GREATER THAN’ is used to negate the original condition result. This is explained in more detail in upcoming section
• Comparison logic differs for numeric and non-numeric operands
• Comparison of numeric operands:-
• When both the operands are numeric, they are compared irrespective of size and USAGE of the field
• Comparison of non-numeric operands:-
• When both the operands are of equal size
• Each characters are compared from left to right and comparison is done based on EBCDIC/ASCII collating sequence of character set
• If at any position, character being found unequal, the operand containing greater character is considered to be greater
• Two operands are considered equal when all characters are identical and rightmost end has reached
• When operands are of unequal size, the shorter operands is extended to right by filling space to make operands equal in size,
• Comparison of numeric operand to non-numeric operand
• Such comparisons are made by treating numeric operand as non-numeric operand and non-numeric comparison rules are applied for comparison
• Relational condition Example:-

IF A IS GREATER THAN 100 ADD 1 TO COUNT END-IF.

#### Sign condition

• Sign condition is used to check whether given operand is less than(NEGATIVE), greater than (POSITIVE) or equal to zero.
• It has the following form:-

operand-1 [ IS][ NOT] {POSITIVE / NEGATIVE / ZERO}

• Where, operand-1 can be identifier/arithmetic-expression
• Sign condition Example:-

IF DEPOSITE IS POSITIVE PERFORM CALC-PARA END-IF.

#### Class condition

• Class condition is used to check whether given operand is NUMERIC or ALPHABETIC
• We can also define our own classes in SPECIAL-NAMES paragraph and check whether the operand belongs to that class
• It has following form:-

operand-1 [IS] [NOT] {NUMERIC/ ALPHABETIC/ ALPHABETIC-LOWER/ ALPHABETIC-UPPER}

• Where,
• operand-1 can be any identifier
• ‘IS’ and ‘NOT’ are optional to specify
• operand is NUMERIC, when it contains digit or operational sign
• operand is ALPHABETIC, when it contains all alphabetic letters (i.e. a-z, A-Z or space)
• operand is ALPHABETIC-LOWER, when it contains all lower alphabetic letters (i.e. a-z or space)
• operand is ALPHABETIC-UPPER, when it contains all upper alphabetic letters (i.e. A-Z or space)
• This condition is very useful for input data validation. It can help in minimizing the calculation errors. There are many possibilities that program may receive corrupted data and to avoid any calculation error we can use this condition
• Class condition example:-

IF WS-QUANTITY IS NUMERIC ADD WS-QUANTITY TO WS-TOT-QTY ELSE DISPLAY ‘INCORRECT QUANTITY’ END-IF.

• In above example, we are validating if WS-QUANTITY contain numeric value. If it contains numeric value, WS-QUANTITY will be added to WS-TOT-QTY, otherwise we are just displaying error message ‘INCORRECT QUANTITY’

#### Condition-name condition

• Condition-name condition requires special data-item defined as level 88, which we already discussed in DATA DIVISION module. Please refer it here for good understanding and example

#### Negated simple condition

• Any simple relation, sign, class condition’s result can be negated(or reversed) using NOT keyword
• While using NOT with sign test, be careful because it is true that ‘NOT NEGATIVE’ does not mean operand is always POSITIVE (i.e. it can be zero)
• Negated condition example:-

IF EMP-ID IS NOT NUMERIC DISPLAY ‘EMP-ID MUST BE NUMERIC’ GO TO ERROR-PARA END-IF.

• In above example, we are validating EMP-ID to make sure it does not have any alphabets. If it EMP-ID has alphabet then EMP-ID will be considered as non-numeric, thus, error message will be thrown and control will be transferred to error handling routine

#### Combined condition

• Two or more single conditions can combined by the logical operators ‘AND’ or ‘OR’.
• It can be used in following form:-

condition-1 {AND / OR} condition-2

• The following table shows the effect of each logical operation:-
(In below table, ‘A’ represents condition-1 and ‘B’ represents condition-2)
A B A  AND  B A  OR  B
False False False False
True False False True
False True False True
True True True True
• Example 1:-

IF EMP-ID IS NUMERIC AND EMP-NAME IS ALPHABETIC DISPLAY ‘DATA IS CORRECT’ END-IF.

In above example, we are validating that EMP-ID contains numeric value and EMP-NAME contains only alphabets. ‘DATA IS CORRECT’ will be displayed only when both conditions evaluates to true
• Example 2:-

IF EMP-ID = 10 OR EMP-ID = 15 DISPLAY ‘EMPLOYEE IS MANAGER IN COMPANY’ END-IF.

In above example, we are validating whether EMP-ID has value 10 or 15. If EMP-ID contains either 10 or 15, then message will be displayed. OR condition does not mandates that both conditions should be true.
• Implied operand:-
• In combined condition, it is not necessary to mention same operand twice for each condition. IF conditions mentioned in Example-2 above, can also be written as follow and it is valid in COBOL:-

IF EMP-ID = 10 OR 15 DISPLAY ‘EMPLOYEE IS MANAGER IN COMPANY’ END-IF.

• In above code snippet, EMP-ID = 15 is implied operand