COBOL learning 3 syntax and grammar

Source: Internet
Author: User
COBOL syntax and grammar (1)
 

Redefines
Different variables can be used to describe the same memory.
01 part_recode usage display
03 part-type picture...
03 part_type_a.
05 part_number picture...
05 part_cost picture...
03 part_type_ B redefines part_type_a.
05 upc_code picture...
03 part_type_c redefines part_type_a.
05 ISBN picture...
Based on part_type, choose part_type_a, part_type_ B, or part_type_c.
Rules:
The length of part_type_ B and part_type_c cannot be greater than that of part_type_a.
Data items cannot have values.
Redefines cannot be used for layer 01.
Part_type_ B and part_type_c cannot contain occurs.

Picture
Define the length, format, and data type of a basic project. Can be abbreviated as pic.
The format string can contain up to 30 characters and can contain the following characters:
A letter A-Z, A-Z, space
B. Insert spaces.
P scalar character, indicating the decimal place not displayed in the field
S algebra symbol. The actual display depends on the usage of the project.
V decimal point
X any character
Z suppresses the first 0 and converts it to a null character
0 insert 0
9 digits 0 ~ 9
/Insert/
, Insert, (the comma cannot be the last character of the string, and the last comma is a separator)
. Insert.
* Suppress the first 0 and convert it *
+ Positive insert +, negative insert-
-Insert spaces in positive values and insert negative values-
$ Insert dollar number
Two spaces are inserted in the CR positive value, and two spaces are inserted in the negative value.
Insert two spaces at DB positive values and negative values into DB
Example:
03 var pic aaaaa. The state variable is a string of 5 Characters and cannot contain numbers. It is equivalent to 03 state pic a (5 ).
Input asdfg to display asdfg
03 The VaR pic xxxxx. filler variable can contain 5 Arbitrary characters. It is equivalent to 03 filler pic x (5 ).
Enter 123as to display 123as
03 var PIC 99/XXX/9999 length 11, input 05may2004, display 05/May/2004
03 var PIC 0 abxxx/9999 length 11, input 05may2004, display 05 May/2004
03 var PIC 9 (3) length 3, 0 ~ In the range of 999.
03 var PIC 9 (4) v99 length 6, 0.00 ~ In the range of 9999.99.
03 var PIC s9 (5) v99 length 7,-99999.99 ~ In the range of 99999.99.
03 var PIC 9 (6) the length of the PPP is 6, which can be 999999000. Enter 1234, and the value is 1000.
03 var PIC ppp999 3, 0.000001 ~ In the range of 0.000999. Enter 123, and the value is 0.000123.
03 var PIC zzz.99, input 100.50, display 100.50; input-51.50, display 51.50;
Enter 0 to display. 00.
03 var PIC $ zzz. ZZ-, input 100.50, show $100.50; input-51.50, show $51.50 -;
Enter 0 to display
03 var PIC $ ***, ***. 99cr, input 1000, show $ ** 1000.00; input-1000, show $ ** 1000.00cr;
Enter 0, show $ ***, ***. 00; enter 51.5, show $ ***, * 51.50
03 var PIC $, $. 99, input 100.50, show $100.50; input-100.50, show $100.50;
Enter 0.777 to display $. 77; enter 0 to display $. 00
03 var PIC $. $, enter 1.00, show $. 00; enter 0.65, show $. 65;
Enter 0 to display $. 00;
03 var PIC $ --, input 17.7, show $17; input-17.7, show $-17;
Enter-5 to display $-5
03 var PIC $999.99 +, input 100.50, and display $100.50 +; input-100.50, and display $100.50 -;
03 var PIC 9 (3) B9 (3), input 55, display 000 055; input 1000.78, display 001 000;

Usage indicates the type of basic or group data. Binary, computational (COMP), display, index, packed-decimal.

Justified
03 var1 pic x (5) justified right.
03 var2 pic x (5 ).
Input XYZ. var1 shows "XYZ", and var2 shows "XYZ ".

Blank when zero
03 var PIC $999,999.99 blank when zero.
When the field value is 0, this item is set to all spaces.

Value
Assign values when defining.
03 var pic x (5) value "ABCDE ".
01 VaR value all spaces.
03 var1 pic xxx.
03 var2 PIC xx.

Arithmetic statement
In COBOL, operators are executed in the order of positive (+), negative (-), exponential (**), multiplication (*), Division (/), and second, add (+) and subtract.
Rounded rounding operation.
77 a pic 9v9 value 9.1.
77 B PIC 9.
Add 0.5 to a giving B on size error go to proc1.
Add 0.5 to a giving B Rounded on size error go to proc2.
End-add.
On size error is executed when an error occurs in the statement. In this example, the first statement is executed without errors, so on size error
If you do not execute proc1. In the second sentence, when 0.5 is added, 9.1 is obtained. Because there is a rounded, perform the rounding operation and get 10. B has only one digit, so an error occurs. Execute the on size error operation and redirect to execute proc2.
Multiple statements are written when the corresponding (Corr) data item has the same name. Add and substract have this option.
01.
03 B.
05 B1 PIC 999v99.
05 B1 PIC 999v99.
05 B1 PIC 999v99.
03 C.
05 B1 PIC 999v99.
05 B1 PIC 999v99.
05 B1 PIC 999v99.
The statement add Corr B to C. is equivalent to executing the following three statements.
Add B1 of B to B1 of C.
Add B2 of B to B2 of C.
Add B3 of B to B3 of C.
The group items do not have to be in the same format. The content of a group project can be absent in another group project, and the order of data items does not matter. It only corresponds to the data item name strictly.
Add a, B giving C. End-Add. Add A and B to C.
Subtract A, B from C. End-Subtract. Subtract A, B from C.
Multiply A by B giving C. End-multiply. multiply A and B into C.
Divide A into B giving C. End-divide. Place the result of dividing B from a into C.
Divide A by B giving C. End-divide. Place the result of a except B into C.
Divide A by B giving C remainder D. Place the operator A except B in C, and the remainder in D.
D = A-B * C.
77 c pic s99v9.
77 d pic s99v9.
Divide 3 into 7 giving C remainder D.
C is 2.3, and D is 0.1. (D = 7-3*2.3)

Compute
Except for the remainder operation, divide can be used for other operations. This statement Evaluates an arithmetic expression and stores the result in one or more variables.
Compute a rounded, B = x * Y/(n + M) + Z. store the result of x * Y/(n + M) + Z in B, and then round it to.

Move
Move A to B.
You can also use the corr option.
Move Corr A to B.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.