SIGN Condition
SIGN Condition
The SIGN condition is used to test the sign (positive, negative, or zero) of numeric variables. It is mainly useful when we need to check the nature of the value (positive, negative, or zero) and then perform specific operations based on that.
Syntax -
variable IS [NOT] {POSITIVE | NEGATIVE | ZERO}
Note! All statements coded in [ ] are optional.
Parameters -
- variable - The numeric variable we want to test.
- POSITIVE - Checks if the variable holds a positive value. Zero is not considered as positive.
- NEGATIVE - Checks if the data item holds a negative value.
- ZERO - Checks if the value is zero.
- NEGATIVE - This can be used to reverse the condition, i.e., to check if the data in the variable is not positive, not negative, or not zero.
Examples -
Scenario1 - Testing for Positive Value.
WORKING-STORAGE SECTION.
01 WS-SALARY PIC S9(5)V99 VALUE 10000.
PROCEDURE DIVISION.
IF WS-SALARY IS POSITIVE
DISPLAY 'The salary is positive'
ELSE
DISPLAY 'The salary is not positive'
END-IF.
Output -
The salary is positive
Scenario2 - Testing for Negative Value.
WORKING-STORAGE SECTION.
01 WS-BALANCE PIC S9(5)V99 VALUE -1000.
PROCEDURE DIVISION.
IF WS-BALANCE IS NEGATIVE
DISPLAY 'The balance is negative'
ELSE
DISPLAY 'The balance is not negative'
END-IF.
Output -
The balance is not negative
Scenario3 - Testing for Zero Value.
WORKING-STORAGE SECTION.
01 WS-COUNT PIC 9(3) VALUE 0.
PROCEDURE DIVISION.
IF WS-COUNT IS ZERO
DISPLAY 'No items to process'
ELSE
DISPLAY 'Processing ', COUNT, ' items'
END-IF.
Output -
The balance is not negative