Basics


Every programming language is designed with some system-defined character sets, symbols, keywords, terms, and conditions. Similarly, COBOL has its character sets, symbols, keywords, and conditions.

Below are the basic and known terms in the COBOL –

  • Character sets.
  • Character strings.
    • COBOL Words.
    • Variables.
    • Literals.
    • Constants.
    • Figurative Constants.
    • Comments.
  • Separators.

Character sets


  • The character is the basic unit of any programming language, and the set of valid characters creates the character set.
  • Similarly, the COBOL language has its own set of valid characters (78) that contains alphabets(A-Z/a-z), digits(0-9), and special characters.

The list of basic COBOL characters are –

CharacterMeaningCharacterMeaning
Space'Apostrophe
+Plus (Left parenthesis
-Minus or hyphen)Right parenthesis
*Asterisk >Greater than
/Forward slash or solidus<Less than
=Equal sign:Colon
$Currency sign _Underscore
,Comma A - ZAlphabet (uppercase)
;Semicolon a - zAlphabet (lowercase)
.Decimal point or period0 - 9Numeric characters
"Quotation mark

Character Strings


The character string is a set of characters created for a purpose or to name something. Separators delimit character strings.

Character strings are used to form the below -

  • COBOL Words
  • Variables
  • Literals
  • Constants
  • Figurative Constants
  • Comments

COBOL Words


A COBOL word is a set of adjacent characters, and each character is from the following character set -

  • Latin uppercase letters A through Z.
  • Latin lowercase letters a through z.
  • digits 0 through 9.
  • - (hyphen).
  • _ (underscore).

COBOL word minimum length is 1 character and maximum length is 30 characters.

COBOL Word Types -

COBOL words are 2 types and those are -

  • User-defined Words - Any word coded by the developer in the program is considered as a user-defined word. For Example - MTHPROG1, STD-GENDER, ....

  • Reserved Words - A reserved word is a system-defined word with proper meaning or task assigned in COBOL language. For Example - ACCEPT, SKIP1, ZEROS, ....

Data item/Variables -


  • A Variable is an identifier or data name used to hold the value for processing in the program.
  • A Variable is also called as a data item/data name.
  • Every variable should declare in the DATA DIVISION of the COBOL program.

For Example - WS-A, WS-VAR, WS-TOTAL, WS-INPUT, WS_OUTPUT, ....

Literal -


The literal is the data value that is assigned to the variable. The data value is a group of characters or a figurative constant. Literals are always specifying with a VALUE clause during the variable declaration.

Literals are classified into two types –

  • Non-numeric literals - Non-numeric literals are the character strings enclosed by quotation marks(" ") or apostrophes(' '). It can contain any allowed character from the character set (A-Z, a-z, 0-9, and special characters).
    For Example - "HELLO", "THIS ISN'T WRONG"....

  • Numeric literals - A numeric literal is a numeric value that is a combination of digits (0 through 9), a sign character (+ or -), and a decimal point. A numeric literal specifies directly without quotation marks(" ") or apostrophes(' ').
    For Example - 1234, -9.999E-3, ....

Constant


A constant is a variable assigned with a value that does not change throughout the program execution.

The programmer can define a variable with an initial value using the VALUE clause during the variable declaration. If the variable value doesn't change during the program's execution, the variable is considered as a constant variable, and the value is considered as a constant value.

Constants are three types, and those are –

  • Numeric constants – Numeric variables having one value throughout the program execution are called numeric constants. For example - 01 WS-PI PIC 9(2)V9(2) VALUE 3.14.

  • Alphanumeric constants/non-numeric constants – Alphanumeric variables that have only one value throughout the program execution are called as alphanumeric constants. They require literals enclosed with quotes while initializing them. For example - 01 WS-HI PIC X(05) VALUE "HI".

  • Figurative Constants - System-defined constants are predefined in the COBOL language and used as replacements for standard values like spaces, zeroes, etc. For example - 01 WS-VAR PIC 9(5) VALUE ZEROES.

Figurative Constant


Figurative constants are system-defined constants with predefined values. These figurative constants are known to the COBOL language and replaced with predefined values when coded.

The figurative constants in COBOL are -

Figurative constant Description
ZERO, ZEROS, ZEROES Represents one or more occurrences of the numeric value 0. ZERO means a single 0 and ZEROS or ZEROES means two or more occurrences of 0s.
SPACE, SPACES Represents one or more occurrences of the space. i.e., " " or X'40'. SPACE means single SPACE (" "), and SPACES means two or more occurrences of space.
HIGH-VALUE, HIGH-VALUES Represents one or more occurrences of the high-value. i.e., X'FF' (8-bit character code for all binary 1s).
LOW-VALUE, LOW-VALUES Represents one or more occurrences of the low-value. i.e., X'00' (8-bit character code for all binary 0s).
QUOTE, QUOTES Represents one or more occurrences of quotation mark (") or apostrophe (').
ALL Represents one or more occurrences of the characters string or figurative constant.

Comments


A comment is not an executable code written along with code used to provide information about code or requirements. All computer-supported characters are allowed to write as part of the comment. Comments do not affect the execution of the program.

The comments are considered as three types based on their usage and where they are used -

  • IDENTIFICATION DIVISION Comments - The entries with optional paragraphs are comments and their usage is -
         ----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
    [Optional]	AUTHOR. NameOfProgrammer.
    [Optional]	INSTALLATION. Development-center.
    [Optional]	DATE-WRITTEN. mm/dd/yy.
    [Optional]	DATE-COMPILED. mm/dd/yy. HH:MM:SS.
    [Optional]	SECURITY. Program-type.


  • Full line comments (any division) - Any line starting with an asterisk (*) in column 7 (indicator area) is considered as a full-line comment. For example -
    ----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
          * FULL LINE COMMENT WITH * IN COLUMN-7.
    


  • Inline comments (any division) - Comments that code in the middle of any line in between 8-72 columns. An inline comment should start with a floating comment indicator (*>). For example -
    ----+----1----+----2----+----3----+----4----+----5----+----6----+----7-- 
           01 WS-VAR        PIC X(12).    *> INLINE COMMENT   

Separators


A separator is a single or multiple character that separates words or strings. The below table shows the separators list -

SeparatorMeaningSeparatorMeaning
Space:Colon
,Comma"Quotation mark
.Period'Apostrophe
;Semicolon==Pseudo-text delimiter

For example -

IDENTIFICATION DIVISION.
01 WS-VAR PIC X(20).
MOVE "ABC" TO WS-VAR.

Practical Example -


Scenario - Below screenshot describes how the different types of character strings formed in COBOL programming.

Character String Code example