Direct naar de hoofdinhoud
eLearner.app
Module 1 · Les 2 van 22/10 in de cursus~12 min
Modulelessen (2/2)

Variabelen en BEELD

In COBOL, all variables used must be declared inside the DATA DIVISION. Specifically, temporary variables in memory used for program logic reside inside the WORKING-STORAGE SECTION.

Level Numbers

COBOL variable declaration makes use of level numbers to structure and organize data hierarchically:

  • 01: Defines a main variable or group item (the highest level).
  • 05, 10, 15: Define subordinate fields within a level 01 group item.
  • 77: Historically indicated independent elementary variables that could not be subdivided further.

In modern practice, level 01 is commonly used to declare independent elementary variables.

The PICTURE (or PIC) Clause

The PICTURE (abbreviated PIC) clause specifies the type of data and the character length of the variable. The three fundamental format characters are:

  1. X (Alphanumeric): Can contain letters, numbers, and special characters.
    • PIC X(10) declares a string of exactly 10 characters.
  2. 9 (Numeric): Can only contain numeric digits.
    • PIC 9(3) declares an integer composed of 3 digits (up to 999).
  3. V (Implicit Decimal Point): Used in decimal numbers to represent where the fractional part begins (the decimal point is not stored physically).
    • PIC 9(3)V99 declares a number with 3 integer digits and 2 decimal places.

The VALUE Clause

To assign an initial value to a variable at declaration time, use the VALUE clause.

Code
01 WS-PROJECT-NAME PIC X(15) VALUE "ELEARNER".
01 WS-USER-AGE     PIC 9(3)  VALUE 25.

All variable definitions must end with a period ..


Try it yourself

Oefening#cobol.m1.l2.e1
Pogingen: 0Laden…

Declare a level 01 alphanumeric variable named WS-USER-NAME of length 20 characters and set its initial value to 'ALICE'.

Editor laden…
Toon hint

Use the format: 01 WS-USER-NAME PIC X(20) VALUE 'ALICE'.

Oplossing beschikbaar na 3 pogingen

Oefening#cobol.m1.l2.e2
Pogingen: 0Laden…

Declare a level 01 numeric variable named WS-USER-AGE of 3 digits with an initial value of 25.

Editor laden…
Toon hint

Use the syntax: 01 WS-USER-AGE PIC 9(3) VALUE 25.

Oplossing beschikbaar na 3 pogingen