跳转到主要内容
eLearner.app
模块 1 · 第 2 课(共 2)课程中的2/10~12 min
模块课程(2/2)

变量和图片

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

锻炼#cobol.m1.l2.e1
尝试:0加载中...

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

正在加载编辑器...
显示提示

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

3 次尝试后可用的解决方案

锻炼#cobol.m1.l2.e2
尝试:0加载中...

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

正在加载编辑器...
显示提示

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

3 次尝试后可用的解决方案