COBOL的字符集
• 数字0 --- 9
• 字母 A--- Z ,a --- z ,
• 专用字符:+ - * / = , . ; :‘ ( ) < > $ 及空格。
COBOL的保留字
• 保留字是具有特定意义的COBOL单字或
特殊字符(用作数学和关系操作符),
只能在指定环境中使用。
• COBOL有300多个保留字
Describing Data
• There are basically three kinds of data
used in COBOL programs;
Variables
Literals
Constants (Figurative Constants)
变量
• 一个变量是一个数据项;它的值能够在程序中被
改变。
• 变量要通过类型和取值范围来进行描述。
• 在COBOL 程序中用到的所有变量都要的数据部
DATA DIVISION进行描述/声明。
变量的使用
01 StudentName PIC X(6) VALUE SPACES.
StudentName
MOVE "JOHN" TO StudentName.
StudentName
J O H N
变量的使用
• DISPLAY "My name is ", StudentName.
My name is JOHN
COBOL 数据类型
• COBOL is not a “typed” language and the distinction
between some of the data types available in the language
is a little blurred.
• For the time being we will focus on just two data types,
– numeric
– text or string
• Data type is important because it determines the
operations which are valid on the type.
• COBOL is not as rigorous in the application of typing
rules as other languages.
For example, some COBOL “numeric” data items may, from
time to time, have values which are not “numeric”!
Quick Review of “Data Typing”
• In “typed” languages simply specifying the type of a data
item provides quite a lot of information about it.
• The type usually determines the range of values the data
item can store.
– For instance an INTEGER between -32,768..32,767
• From the type of the item, the compiler can establish how
much memory to set aside for storing its values.
• If the type is “REAL”, the number of decimal places is
allowed to vary dynamically with each calculation, but the
amount of the memory used to store a real number is fixed.
COBOL 数据描述
• Because COBOL is not typed it employs a
different mechanism for describing the characteristics
of the data items in the program.
• COBOL uses what could be described as a
“declaration by example” strategy.
• In effect, the programmer provides the system with an
example, or template, or PICTURE of what the data
item looks like.
• From the “picture” the system derives the information
necessary to allocate it.
声明数据
• COBOL 里,一个变量声明行包括下列部分:
一个层号(level number)
一个数据名或变量名(data-name or variable)
一个 PICTURE子句
• We can give a starting value to a variable by means
of an extension to the picture clause called the value
clause.
层号
• In COBOL, level numbers are used to decompose a structure into
it’s constituent parts.
• In this hierarchical structure the higher the level number, the lower
the item is in the hierarchy. At the lowest level the data is
completely atomic.
• The level numbers 01 through 49 are general level numbers but
there are also special level numbers such as 66, 77 and 88 (66 for
Renames items, 77 or 88 for condition items).
• In a hierarchical data description what is important is the
relationship of the level numbers to one another, not the actual level
numbers used.
01 StudentDetails.02 StudentName.03 Surname PIC X(8).03 Initials PIC XX.02 StudentId PIC 9(7).02 CourseCode PIC X(4).02 Grant PIC 9(4).02 Gender PIC X.
01 StudentDetails.02 StudentName.03 Surname PIC X(8).03 Initials PIC XX.02 StudentId PIC 9(7).02 CourseCode PIC X(4).02 Grant PIC 9(4).02 Gender PIC X.
01 StudentDetails.05 StudentName.10 Surname PIC X(8).10 Initials PIC XX.05 StudentId PIC 9(7).05 CourseCode PIC X(4).05 Grant PIC 9(4).05 Gender PIC X.
01 StudentDetails.05 StudentName.10 Surname PIC X(8).10 Initials PIC XX.05 StudentId PIC 9(7).05 CourseCode PIC X(4).05 Grant PIC 9(4).05 Gender PIC X.
‘PICTURE’ Clause symbols
• To create the required ‘picture’, the programmer uses a
set of symbols.
• The following symbols are used frequently in picture
clauses;
– 9 (the digit nine) is used to indicate the occurrence of a digit at
the corresponding position in the picture.
– X (the character X) is used to indicate the occurrence of any
character from the character set at the corresponding position in
the picture
– V (the character V) is used to indicate position of the decimal
point in a numeric value! It is often referred to as the “assumed
decimal point” character.
– S (the character S) indicates the presence of a sign and can only
appear at the beginning of a picture.
‘PICTURE’ Clauses
• Some examples
– PICTURE 999 a three digit (+ive only) integer
– PICTURE S999 a three digit (+ive/-ive) integer
– PICTURE XXXX a four character text item or string
– PICTURE 99V99 a +ive ‘real’ in the range 0 to 99.99
– PICTURE S9V9 a +ive/-ive ‘real’
• If you wish you can use the abbreviation PIC.
• Numeric values can have a maximum of 18 (eighteen)
digits (i.e. 9’s).
• The limit on string values is usually system-dependent.
Abbreviating recurring symbols
• Recurring symbols can be specified using a ‘repeat’
factor inside round brackets
– PIC 9(6) = PICTURE 999999
– PIC 9(6)V99 = PIC 999999V99
– PICTURE X(10) = PIC XXXXXXXXXX
– PIC S9(4)V9(4) = PIC S9999V9999
– PIC 9(18) = PIC 999999999999999999
Declaring Data example
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Num1 PIC 999 VALUE ZEROS.
01 VatRate PIC V99 VALUE .18.
01 StudentName PIC X(10) VALUE SPACES.
Num1 VatRate StudentNameNum1 VatRate StudentName
000000 .18.18
DDATAATA
Literal
• A literal is a character string whose value is
given by the characters themselves.
• String/Alphanumeric literals are enclosed in
quotes and may consists of alphanumeric
characters
e.g. "Michael Ryan", "-123", "123.45"
• Numeric literals may consist of numerals, the
decimal point and the plus or minus sign.
Numeric literals are not enclosed in quotes.
e.g. 123, 123.45, -256, +2987
Literals Examples
• Alphanumeric literal
01 WrongName PIC X(20)
…..
MOVE “Name is not valid.” TO WrongName
• Numeric literal
01 Code-number PIC 9(5).
….
IF Code-number = 03519 THEN display “Code number
was found’
常量
• A constant is a data item that has only one value.
For example:
DATA DIVISION.
. . .
01 Report-Header PIC X(50) VALUE “Sales
Report’.
. . .
01 Interest PIC 9V9999 VALUE 1.0265.
COBOL的常量
• 数值常量(如 100,-29)
• 非数值常量(如 ‘ABC’)
•
表
关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf
意常量/象征常量(如 ZERO,
SPACE,HIGH-VALUE,LOW-
VALUE ,QUOTE ,ALL 常量)
Figurative Constants
• COBOL provides its own, special constants as
reserved words called Figurative Constants.
SPACE or SPACES = ‘ ’
ZERO or ZEROS = 0
QUOTE or QUOTES = "
HIGH-VALUE or HIGH-VALUES= Max Value
LOW-VALUE or LOW-VALUES = Min Value
NULL = ‘’
Assignment in COBOL
• In COBOL assignment is achieved using the
MOVE verb.
• The MOVE copies data from the source
identifier or literal to one or more destination
identifiers.
{ }MOVE TO ...Identifier
Literal
Identifier
⎧⎨⎩
⎫⎬⎭
Assign data to variable (MOVE)
01 StudentName PIC X(10) VALUE "MIKE".
MOVE ALL "-" TO StudentName.
StudentName
M I K E M I K E ¨¨ ¨¨ ¨¨ ¨¨ ¨¨ ¨¨
StudentName
-- -- -- -- -- -- -- -- -- --
MOVEing to a numeric item
• When the destination item is numeric, or edited
numeric, then data is aligned along the decimal
point with zero filling or truncation as necessary.
• When the decimal point is not explicitly specified
in either the source or destination items, the item
is treated as if it had an assumed decimal point
immediately after its rightmost character.
Example
01 GrossPay PIC 9(5)V99 VALUE 13.5.
MOVE ZERO TO GrossPay.
0 0 0 1 3 5 0
GrossPay
ñ
z
GrossPay
0 0 0 0 0 0 0
ñ
z
01 CountyPop PIC 999.
01 Price PIC 999V99.
MOVE 1234 TO CountyPop.
CountyPop = ?
MOVE 12.4 TO CountyPop.
CountyPop = ?
MOVE 154 TO Price.
Price = ?
MOVE 3552.75 TO Price.
Price = ?
Assign arithmetic result (COMPUTE)
• When significant left-order digits would be
lost in execution, the COMPUTE statement
can detect the condition and allow you to
handle it.
e. g. COMPUTE z = x + y
Displaying values on a screen or in a
file (DISPLAY)
• Display the value of a variable on a screen or
write it to a file by using the DISPLAY
statement.
e.g. DISPLAY “HELLO WORLD!".
DISPLAY “MY NAME IS “ NAME.
The DISPLAY Verb
• A single DISPLAY can be used to display
several data items or literals or any
combination of these.
• The WITH NO ADVANCING clause
suppresses the carriage return/line feed.
[ ] [ ]
DISPLAY
Identifier
Literal
Identifier
Literal ...
UPON WITH NO ADVANCING
⎧⎨⎩
⎫⎬⎭
⎧⎨⎩
⎫⎬⎭
⎡
⎣⎢
⎤
⎦⎥
Mnemonic-Name
使用内部函数
• A function-identifier is the combination of
the COBOL reserved word FUNCTION
followed by a function name (such as Max),
followed by any arguments to be used in the
evaluation of the function (such as x, y, z).
内部函数事例
WORKING-STORAGE SECTION.
01 X PIC 99 VALUE 2.
01 Y PIC 99 VALUE 4.
01 Z PIC 99 VALUE 0.
. . .
COMPUTE Z = X + FUNCTION SQRT(Y)
. . .
嵌套使用内部函数
• Functions can reference other functions as
arguments as long as the results of the nested
functions meet the requirements for the
arguments of the outer function.
e.g. COMPUTE X = FUNCTION
MAX((FUNCTION SQRT(5)) 2.5 3.5)
COBOL的字符集
COBOL的保留字
Describing Data
变量
变量的使用
变量的使用
COBOL 数据类型
Quick Review of “Data Typing”
COBOL 数据描述
声明数据
层号
‘PICTURE’ Clause symbols
‘PICTURE’ Clauses
Abbreviating recurring symbols
Declaring Data example
Literal
Literals Examples
常量
COBOL的常量
Figurative Constants
Assignment in COBOL
Assign data to variable (MOVE)
MOVEing to a numeric item
Example
Assign arithmetic result (COMPUTE)
Displaying values on a screen or in a file (DISPLAY)
The DISPLAY Verb
使用内部函数
内部函数事例
嵌套使用内部函数