More on String Types - Single Letter Variables

Exploring Advanced String Types in COBOL

Welcome back! Now that you have a basic understanding of COBOL data types, including simple variables and integers, it's time to delve deeper into the fascinating world of string types. This lesson specifically focuses on single-letter variables and more complex string definitions. Understanding how to effectively define and manipulate strings in COBOL will enhance your ability to handle text data and prepare you for more advanced programming tasks.

What You'll Learn

In this lesson, you will learn how to define and manipulate single-letter variables and more complex string variables using COBOL. We'll cover:

  1. Defining single-letter variables with PIC A.
  2. Using PIC X to define string variables that can hold numbers and special characters.
  3. More complex definitions using PIC A for multi-letter string variables.

Here’s an example to give you a taste of what’s coming:

IDENTIFICATION DIVISION.
PROGRAM-ID. StringsAndLetters.
DATA DIVISION.
WORKING-STORAGE SECTION.

*> Defining a single-letter variable.
01 letterVariable PIC A. *> A single-letter variable.

*> Use PIC X to define a string variable that can hold numbers and special characters.
01 stringVariable PIC X(5) VALUE 'Hey_1'.

*> Use PIC A to define a string variable.
01 anotherStringVariable PIC A(4).

PROCEDURE DIVISION.
    MOVE 'A' TO letterVariable.
    DISPLAY "Letter: " letterVariable. *> A

    MOVE 'long' TO letterVariable.
    DISPLAY "Letter: " letterVariable. *> l

    DISPLAY stringVariable. *> Hey_1

    MOVE 'Halo' TO anotherStringVariable.
    DISPLAY anotherStringVariable. *> Halo

    STOP RUN.

Key Parts of the Code Explained

Let's now dive into the code snippet provided above to understand PIC A, PIC X(), and PIC A() and explain the differences between X() and A().

  • Defining Single-Letter Variables with PIC A: PIC A defines a variable that can hold a single alphabetic character (A-Z).

  • Using PIC X to Define String Variables: PIC X(5) defines a string variable that can hold any character (letters, numbers, and special characters) up to 5 characters in length.

  • Using PIC A for Multi-Letter String Variables: PIC A(4) defines a string variable that can hold up to 4 alphabetic characters (A-Z).

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal