Having considered in the previous “lessons” the structure of the program (http://habrahabr.ru/blogs/atnikvariat/115813/), simple arithmetic functions (http://habrahabr.ru/blogs/atnikvariat/115821/) and the description of variables (http: //habrahabr.ru/blogs/atnikvariat/115849/) we will go further and further!
This time we will get acquainted with the goodies of the conditions (and also get an additional small gift for knowledge at the end).
So, I think everyone who is at least a little interested in programming will remember that there is probably almost all YP in such a statement - such as IF (If), there is of course in COBOL as well.
')
How is it recorded?
IF <condition>
<action1>
(optional ELSE)
END-IF.
ATTENTION. It is important:
Each expression in COBOL ends with a “.” (Dot), you already know this, BUT IF is considered as one expression, so the dot here is put ONLY after END-IF.
Be careful!
Possible conditions:
1. Check for the data type in the variable:
numeric (IS NUMERIC) - verifies that the variable contains a number.
Letter (IS ALPHABETIC) - it is checked that the variable contains ONLY letters and spaces.
Alphabetic (capital letters) and alphabetic (capital letters) (IS ALPHABETIC-UPPER and ALPHABETIC-LOWER) - it is checked respectively that only capital or capital letters and spaces are contained.
It was used and is used to check the accuracy of the entered data and the corresponding processing of the detected errors.
For example,
IF WS-A IS NUMERIC
MOVE WS-A TO WS-B
ELSE
DISPLAY “Error!”
END-IF.
2. Comparison:
Operator (full form of record), Short form of record, Meaning
IS GREATER THAN IS> More than
IS NOT GREATER THAN IS NOT> No more than
IS LESS THAN IS <Less than
IS NOT LESS THAN IS NOT <Not less than
IS EQUAL TO IS = Equals
IS NOT EQUAL TO IS NOT = Not equal
IS GREATER THAN OR EQUAL TO IS> = Greater than or equal to
IS LESS THAN OR EQUAL TO IS <= Less than or equal to
And if with the comparison of numbers everything is clear, then the comparison of letter variables should be clarified. COBOL adjusts the compared values to the same size with spaces added to the right end. After that, a character-by-character comparison starts from the leftmost character in accordance with ASCII, EBCDIC (on mainframe) or another encoding specified by the compiler.
IF TEXT-INPUT IS> Orange
DISPLAY "Orange"
DISPLAY TEXT-INPUT
ELSE
DISPLAY TEXT-INPUT
DISPLAY "Orange"
END-IF.
If you enter Apple, it will be “less” than Orange, but apple will be “more”.
3. Check on the sign of the number:
IS POSITIVE - the number is positive;
IS NEGATIVE - the number is negative;
IS ZERO - the number is zero;
IS NOT POSITIVE - the number is NOT positive (0 or negative);
IS NOT NEGATIVE - the number is NOT negative (0 or positive);
IS NOT ZERO - the number is NOT zero.
For example:
MOVE 100 TO WS-RESULT.
DISPLAY "WS-RESULT IS", WS-RESULT.
IF WS-RESULT IS NOT ZERO
DISPLAY "WS-RESULT IS NOT ZERO!"
END-IF.
IF WS-RESULT IS ZERO
DISPLAY "WS-RESULT IS ZERO!"
END-IF.
IF WS-RESULT IS NOT POSITIVE
DISPLAY "WS-RESULT IS NOT POSITIVE!"
END-IF.
IF WS-RESULT IS NOT NEGATIVE
DISPLAY "WS-RESULT IS NOT NEGATIVE!"
END-IF.
IF WS-RESULT IS POSITIVE
DISPLAY "WS-RESULT IS POSITIVE!"
END-IF.
IF WS-RESULT IS NEGATIVE
DISPLAY "WS-RESULT IS NOT ZERO!"
END-IF.
The output in this case will look like:
WS-RESULT IS 000100
WS-RESULT IS NOT ZERO!
WS-RESULT IS NOT NEGATIVE!
WS-RESULT IS POSITIVE!
4. Using a variable with conditions:
In a previous lesson, we mentioned the level of variable 88. Now it's time to take a closer look at it. A variable of this type has no description, it is initialized immediately upon declaration and can only be a sub-level of another variable. It is used as an analogue of CASE from other programming languages and stores the value with which the IF will be checked. For example:
We describe and initialize in DATA DIVISION.
01 NUMBER-CHECK PIC S9.
88 NC-ONE VALUE 1.
88 NC-ZERO VALUE 0.
88 NC-MINUS VALUE -1.
We use in PROCEDURE DIVISION.
ACCEPT NUMBER-CHECK.
IF NC-ONE
DISPLAY “NUMBER-CHECK IS ONE”
END-IF.
IF NC-MINUS
DISPLAY “NUMBER-CHECK IS MINUS”
END-IF.
IF NC-ZERO
DISPLAY “NUMBER-CHECK IS ZERO”
END-IF.
How it works:
NC-ONE, NC-MINUS and NC-ZERO are variables assigned to NUMBER-CHECK, when we use IF NC-ONE, the actual compiler “substitutes”
IF NUMBER-CHECK = 1.
In addition to VALUE, VALUES <value-from> THRU <value-to> can be used; in this case, at 88 NC-ONE VALUES 1 THRU 10, the value stored in NUMBER-CHECK in the range from 1 to 10 will be checked.
In addition to the above, IF supports NOT, AND, and OR, for example, IF NOT WS-A = WS-B or IF ((WS-A = WS-B) AND (WS-A = WS-RESULT)) OR (WS-RESULT = WS-B).
Waiting for questions and suggestions in the comments.
Until next meeting! There is still a lot of interesting ahead)
Vorontsov “nerfur” Vyacheslav. 2011