A value can be assigned to a variable with the MOVE statement. It has two modes:
assign mode (in case no position nor length is defined, the content is given to the target, and the rest of the target area is filled)
substring mode (in case the position and length is given, the content of the source is copied into the full area of the target)
COBOL will convert the value to the target type if possible. In case the source value can not be converted to the target variable type (e.g. "abc123" to PIC 9) a runtime error is raised (data exception).
The value can be:
literal
variable
In case an arithmetic expression is used instead of a literal or another variable, use the COMPUTE statement to assign the result to the variable. See COMPUTE in the COBOL OPERATORS page.
EXAMPLE FOR ASSIGNING VALUE TO VARIABLE:
In earlier COBOLs, the basic arithmetic equations were expressed by keywords. The members of such expressions can be numeric variables and literals. Modern COBOL knows the mathematical signs as well.
On overflow error, the flow will enter into the optional ON SIZE ERROR branch.
When an arithmetic expression is used to assign a value to a variable, COMPUTE statement is needed.
In case the variable value is not computed but given (in the form of a constant or a variable), use the MOVE statement instead. See the COBOL VARIABLES page for more.
The COMPUTE executes calculations. It works on single and multiple target variables too. When multiple variables are given to store the result, all of them will receive the result of the same expression. However, the types of the result variables determine the final values.
EXAMPLE FOR USAGE OF ARITHMETIC OPERATORS:
These are not operators but mentioned as operators:
ROUNDED (rounding)
ON SIZE ERROR (overflow handling)
REMAINDER (remainder of division)
When using these arithmetic instructions along with DIVIDE and COMPUTE, the type(s) of the result(s) depend(s) on the combination of input types and the instructions themselves:
DIVIDE GIVING (integer division; integer quotient, remainder is ignored)
DIVIDE GIVING REMAINDER (integer quotient, integer remainder)
COMPUTE (real/decimal division; if receiver field is integer, decimal part of quotient is truncated)
ROUNDED (rounding to the decimal places of receiver field)
EXAMPLE FOR USAGE OF SPECIAL ARITHMETIC INSTRUCTIONS:
Parallel with modern languages but keywords are present as well:
=
<> (not equal)
<
>
<=
>=
IS EQUAL TO
IS NOT EQUAL TO
IS GREATER THAN
IS LESS THAN
IS GREATER THAN OR EQUAL TO
IS LESS THAN OR EQUAL TO
EXAMPLE FOR USAGE OF RELATIONAL OPERATORS:
Parallel with modern languages:
AND
OR
NOT
EXAMPLE FOR USAGE OF LOGICAL OPERATORS:
These are present only in COBOL:
THRU / THROUGH (for defining an interval)
ALSO (used in multivariable switch-case where defined multiple variables then a condition-set corresponding to the variables)
OF (for fields of structure)
IN (for files and arrays)
INTO (for files and array, target-definition)
IS (part of verbal relational operators)
COBOL does not know the RECORD.FIELD formula.
EXAMPLE FOR USAGE OF COBOL-SPECIFIC OPERATORS:
COBOL does not use + and similar operators on strings. Instead:
STRING (concatenation)
UNSTRING (de-mounting or delimiting)
DELIMITED BY: defines a delimiter until which the operation should work on the specific string.
The delimiter can be:
SPACE
SIZE (use the full length of the given string, default therefore same happens when the delimiter is not defined)
multiple characters (e.g. ", ")
LOW-VALUE (null-byte)
HIGH-VALUE (0xFF)
QUOTE (" or ')
The UNSTRING operation slices the source to tokens by the defined delimiter, then tries to fill up the first target field with the first token, then adds the second token to the rest of the first token not fitting into the first field (due to the delimiter on the first target field) and continues to fill up the second target field and so on.
The exception in this process is the actual target field size (or the SIZE delimiter on the actual target field), if the given token does not fit, the rest of that token will be truncated, and the next token will not inherit what was truncated.
EXAMPLE FOR USAGE OF STRING:
EXAMPLE FOR USAGE OF UNSTRING:
The INSPECT can have single or multiple clauses which determine the operation. In case of multiple clauses, the operation flow will follow their order; these clauses define an operation chain.
The operator rewrites the value of the original string.
On DISPLAY variables:
TALLYING (counts the given character how many times can be found in the variable)
REPLACING (replaces the first or all given characters to another)
CONVERTING (transforms characters)
TALLYING:
ALL (counts every appearance)
LEADING (counts only the left-side continuous appearance)
character literal (of what to be counted)
REPLACING:
ALL (counts every appearance of the literal)
LEADING (counts only the left-side continuous appearance of the literal)
TRAILING (counts only the right-side continuous appearance of the literal)
character literal 1 (what should be changed)
character literal 2 (to what should be changed)
CONVERTING (batch version of REPLACING without ALL and LEADING):
string literal 1 (characters which should be changed one-by-one)
string literal 2 (characters to which should be changed one-by-one, correspondingly to the given character of the string literal 1)
BEFORE (global limiter of INSPECT):
INITIAL or FIRST (the operation works before...)
character literal (... this character)
AFTER (global limiter of INSPECT):
INITIAL or FIRST (the operation works after...)
character literal (... this character)
EXAMPLE FOR INSPECTING VARIABLES:
EXAMPLE FOR INSPECTING VARIABLES, SPECIAL CASES: