SIGN Condition
SIGN Condition
- SIGN condition tests the sign (positive, negative, or zero) of the data in 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 outcome.
Syntax -
variable IS [NOT] {POSITIVE | NEGATIVE | ZERO}
Note! All statements coded in [] are optional.
Parameters -
- variable - Specifies 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 variable holds a negative value.
- ZERO - Checks if the value is zero.
- NOT - Checks if the variable holds the value other than coded. For example - 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