Michael Coughlan
Page 10
5
1
4
5
G
S
P
o
w
e
M
2
3
4
4
5
6
7
4
5
1
4
5
G
S
F
i
t
z
M
2
3
4
4
5
6
7
4
5
1
4
5
G
S
F
i
t
z
M
0
0
0
3
4
0
0
54
Chapter 4
Procedure Division Basics
The three preceding chapters covered much of the background material you need before you can write useful
programs. Chapter 1 was motivational, Chapter 2 dealt with the structure of COBOL programs, and in Chapter 3 you learned how to define the data storage that dynamic programs require to be useful.
The PROCEDURE DIVISION contains the code used to manipulate data described in the DATA DIVISION. This
chapter examines some of the basic PROCEDURE DIVISION commands. You learn how to get data from the user, how to use the COBOL arithmetic verbs to do calculations on the data, and how to display the results on the computer screen.
Input and Output with ACCEPT and DISPLAY
In COBOL, the ACCEPT and DISPLAY verbs are used to read from the keyboard and write to the screen. Input
and output using these commands is somewhat primitive. The original purpose of these commands was not to
communicate with the end user but for use in a batch-programming environment, to allow interaction with the computer operators. Because computer operators are expert users and the level of their interaction with the program was limited to viewing alerts and action prompts or entering the occasional file name, no great sophistication was required in the ACCEPT and DISPLAY commands.
In recent years, however, many implementers have found a need for more powerful versions of ACCEPT and
DISPLAY in order to allow online systems to be created. These implementers have augmented the ACCEPT and DISPLAY
syntax to allow such things as cursor positioning, character attribute control, and auto-validation of input. In some cases, they have even implemented a special SCREEN SECTION in the DATA DIVISION.
In a real environment, console-based (as opposed to Windows-based) input and output operations would
be handled either by implementer-enhanced versions of ACCEPT and DISPLAY or by calls to forms-management or transaction-processing software such as Terminal Data Management System (TDMS), DECforms, and Customer
Information Control System (CICS).
This book considers only the standard ACCEPT and DISPLAY syntax. If the vendor of your version of COBOL offers extended ACCEPT and DISPLAY syntax, you should read the manual to discover how these extensions work.
The DISPLAY Verb
The DISPLAY verb is used to send output to the computer screen or to a peripheral device. A single DISPLAY can be used to display several data items or literals or any combination of these. The concatenation required by some other languages is not required for the DISPLAY verb.
Metalanguage diagrams are used to describe the syntax of COBOL verbs and other elements. The metalanguage
for the DISPLAY verb is given in Figure 4-1. In case you have forgotten how to interpret these diagrams, see
“Metalanguage Reminder” for a brief refresher on the meaning of the symbols.
55
Chapter 4 ■ proCedure division BasiCs
Figure 4-1. Metalanguage for the DISPLAY verb
MetaLaNGUaGe reMINDer
in the CoBoL syntax diagrams (the CoBoL metalanguage), uppercase words are keywords. if underlined, they are mandatory. in addition
• { } brackets mean one of the options must be selected.
• [ ] brackets mean the item is optional.
• an ellipsis (...) means the item may be repeated at the programmer’s discretion.
the symbols used in the syntax diagram identifiers have the following significance:
• $ indicates a string (alphanumeric) item.
• # indicates a numeric item.
• i indicates that the item can be a variable identifier.
• l indicates that the item can be a literal.
Notes
As the ellipsis (...) in the metalanguage shows, a single DISPLAY can be used to display several data items or literals or any combination of these. The items displayed must be USAGE DISPLAY items. USAGE COMP or INDEX will not display correctly. USAGE IS DISPLAY is the default for COBOL data items; it means the data is held in a displayable format. For efficiency purposes, it is also possible to hold data in a binary format that is not displayable. The USAGE clause, which you examine later in the book, is used when you want to hold a data item in one of the more computationally efficient binary formats. For instance:
01 SaleValue PIC 9(5)V99 USAGE IS COMP.
01 TableSubscript USAGE IS INDEX.
The default display device is the computer screen, but you can use other devices for output by specifying a mnemonic-name with the UPON clause. Mnemonic-names are used to make programs more readable and more maintainable; they are devised by programmers to represent peripheral devices (such as serial ports). A name is connected to an actual device by an entry in the SPECIAL-NAMES paragraph of the CONFIGURATION SECTION in the ENVIRONMENT DIVISION. The actual device to which the mnemonic-name is connected is defined by the language implementer. Consult your COBOL manual to learn what devices your implementer supports.
Ordinarily, after data is displayed on the computer screen, the onscreen cursor moves to the next row.
Sometimes, however, you want the cursor to remain on the same row. In these cases, you can use the WITH NO
ADVANCING clause to ensure that the cursor does not move to the next row.
56
Chapter 4 ■ proCedure division BasiCs
DISPLAY Examples
This section gives some illustrative DISPLAY examples. The DISPLAY in eg1 sends the data in PrinterSetupCodes to the device represented by the mnemonic-name PrinterPort1. The output from the remaining examples is shown
in the Display Results diagram. Note that in eg4, the separator spaces inserted between the statement operands have no effect on the output. In a COBOL statement, you can insert separator commas, spaces or semicolons wherever you want to make a statement more readable. Also note that in eg5, the figurative constants SPACE and SPACES are synonyms; they both insert only a single space. Note too that no concatenation operator is required to bind the data items and figurative constants into a single string
eg1. DISPLAY PrinterSetupCodes UPON PrinterPort1
eg2. MOVE 3456 TO FinalTotal
DISPLAY "The final total is " FinalTotal
eg3. DISPLAY "One, " WITH NO ADVANCING
DISPLAY "two, " WITH NO ADVANCING
DISPLAY "three."
eg4. DISPLAY 1 "," 2 "," 3 "."
eg5. MOVE 10 TO DayOfBirth
MOVE 12 TO MonthOfBirth
MOVE 1975 TO YearOfBirth
DISPLAY "Date of birth is - "
DayOfBirth SPACES MonthOfBirth SPACE YearOfBirth
The ACCEPT Verb
There are two formats for the ACCEPT verb:
• The first gets data from the keyboard or a peripheral device.
• The second lets you access the system date and time (that is, the date and ti
me held in the
computer’s internal clock) by using certain system variables.
The metalanguage for the two formats of the ACCEPT are shown in Figure 4-2.
57
Chapter 4 ■ proCedure division BasiCs
Figure 4-2. Metalanguage for the ACCEPT verb
Rules
When you use the first format, ACCEPT inserts the data typed on the keyboard into the receiving data item. If the FROM option is used, the data inserted into the receiving data item comes from the device indicated by the mnemonic-name. Data is sent to the ReceivingItem according to the rules for alphanumeric moves. If the
ReceivingItem is too small to hold the data, the rightmost characters that do not fit are lost. If the ReceivingItem is too large, there is space-filling on the right.
The default input device is the computer keyboard, but you can use other devices by specifying a
mnemonic-name with the FROM clause. The mnemonic-name is connected to the actual device by an entry
in the SPECIAL-NAMES paragraph, CONFIGURATION SECTION, ENVIRONMENT DIVISION.
When you use the second format, ACCEPT moves the data from one of the system variables (DATE, DAY, DAY-OF-WEEK, TIME) into the receiving data item. Two of the system variables also have optional syntactic elements that allow you to specify that the date be supplied with a four-digit year.
Required Format for System Variables
The declarations and comments that follow show the format required for the data items that ACCEPT values from each of the system variables:
01 CurrentDate PIC 9(6).
* Receiving data item for DATE system variable: Format is YYMMDD
01 DayOfYear PIC 9(5).
* Receiving data item for DAY system variable: Format is YYDDD
01 Day0fWeek PIC 9.
* Receiving item for DAY-OF-WEEK: Format is D (1=Monday)
01 CurrentTime PIC 9(8).
* Receiving item for TIME: Format is HHMMSSss s = S/100
01 Y2KDate PIC 9(8).
* Receiving item for DATE YYYYMMDD system variable: Format is YYYYMMDD
01 Y2KDayOfYear PIC 9(7).
* Receiving item for DAY YYYYDDD system variable: Format is YYYYDDD
58
Chapter 4 ■ proCedure division BasiCs
Example Program: ACCEPT and DISPLAY
Listing 4-1 gives some examples of how to use the ACCEPT and DISPLAY verbs. The examples use both formats of ACCEPT. The first form of ACCEPT is combined with DISPLAY to prompt for and receive a username. The second form gets data from some of the date and time system variables. Finally, all the gathered information is displayed on the computer screen. The results of running the program are shown in the results diagram.
Listing 4-1. ACCEPT and DISPLAY Examples
IDENTIFICATION DIVISION.
PROGRAM-ID. Listing4-1.
AUTHOR. Michael Coughlan.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 UserName PIC X(20).
*> Receiving data item for DATE system variable: Format is YYMMDD
01 CurrentDate.
02 CurrentYear PIC 99.
02 CurrentMonth PIC 99.
02 CurrentDay PIC 99.
*> Receiving data item for DAY system variable: Format is YYDDD
01 DayOfYear.
02 FILLER PIC 99.
02 YearDay PIC 9(3).
*> Receiving item for TIME: Format is HHMMSSss s = S/100
01 CurrentTime.
02 CurrentHour PIC 99.
02 CurrentMinute PIC 99.
02 FILLER PIC 9(4).
*> Receiving item for DATE YYYYMMDD system variable: Format is YYYYMMDD
01 Y2KDate.
02 Y2KYear PIC 9(4).
02 Y2KMonth PIC 99.
02 Y2KDay PIC 99.
*> Receiving item for DAY YYYYDDD system variable: Format is YYYYDDD
01 Y2KDayOfYear.
02 Y2KDOY-Year PIC 9(4).
02 Y2KDOY-Day PIC 999.
PROCEDURE DIVISION.
Begin.
DISPLAY "Please enter your name - " WITH NO ADVANCING
ACCEPT UserName
DISPLAY "**********************"
ACCEPT CurrentDate FROM DATE
ACCEPT DayOfYear FROM DAY
ACCEPT CurrentTime FROM TIME
ACCEPT Y2KDate FROM DATE YYYYMMDD
59
Chapter 4 ■ proCedure division BasiCs
ACCEPT Y2KDayOfYear FROM DAY YYYYDDD
DISPLAY "Name is " UserName
DISPLAY "Date is " CurrentDay "-" CurrentMonth "-" CurrentYear
DISPLAY "Today is day " YearDay " of the year"
DISPLAY "The time is " CurrentHour ":" CurrentMinute
DISPLAY "Y2KDate is " Y2kDay SPACE Y2KMonth SPACE Y2KYear
DISPLAY "Y2K Day of Year is " Y2KDoy-Day " of " Y2KDOY-Year
STOP RUN.
■ Note the example programs in this book were compiled by using Micro Focus visual CoBoL and capturing the output results. in most cases, the programs were also compiled and run using the web-based open source CoBoL
compiler at www.compileonline.com/compile_cobol_online.php. if you want to use this compiler, be aware that interactivity is limited and you must enter keyboard input via the site’s stdin input box. some tweaking may be required.
Arithmetic in COBOL
Most procedural programming languages perform computations by assigning the result of an arithmetic expression (or function) to a variable. In COBOL, the COMPUTE verb is used to evaluate arithmetic expressions, but there are also specific commands for adding (ADD), subtracting (SUBTRACT), multiplying (MULTIPLY), and dividing (DIVIDE).
Common Arithmetic Template
With the exception of COMPUTE, DIVIDE with REMAINDER, and some exotic formats of ADD and SUBTRACT, most COBOL
arithmetic verbs conform to the template metalanguage shown in Figure 4-3. It is useful to review this metalanguage template because it allows me to discuss the clauses and issues that apply to all the arithmetic verbs.
Figure 4-3. Metalanguage for a common arithmetic template
60
Chapter 4 ■ proCedure division BasiCs
Arithmetic Template Notes
All the arithmetic verbs move the result of a calculation into a receiving data item according to the rules for a numeric move: that is, with alignment along the assumed decimal point and with zero-filling or truncation as necessary. In all the arithmetic verbs except COMPUTE, the result of the calculation is assigned to the rightmost data item(s).
All arithmetic verbs must use numeric literals or numeric data items (PIC 9) that contain numeric data. There is one exception: data items that receive the result of the calculation but are not themselves one of the operands (do not contribute to the result) may be numeric or edited numeric.
Where the GIVING phrase is used, the item to the right of the word giving receives the result of the calculation but does not contribute to it. Where there is more than one item after the word giving, each receives the result of the calculation.
Where the GIVING phrase is not used and there is more than one OperandResult#i, Operand#il is applied to each OperandResult#i in turn, and the result of each calculation is placed in each OperandResult#i.
The maximum size of each operand is 18 digits (31 in ISO 2002 COBOL).
Examples of COBOL Arithmetic Statements
Here are a number of examples, each followed by an explanation of the operation:
ADD Takings TO CashTotal
* Adds the value in Takings to the value in CashTotal and puts the result in CashTotal
ADD Males TO Females GIVING TotalStudents
* Adds the value in Males to the value in Females and overwrites
* the value in TotalStudents with the result.
ADD Sales TO ShopSales, CountySales, CountrySales
* Adds the value of Sales to ShopSales and puts the result in ShopSales.
* Adds the value of Sal
es to CountySales and puts the result in CountySales
* Adds the value of Sales to CountrySales and puts the result in CountrySales
SUBTRACT Tax FROM GrossPay
* Subtracts the value in Tax from the value in GrossPay and puts the result in GrossPay.
SUBTRACT Tax FROM GrossPay GIVING NetPay
* Subtracts the value in Tax from the value in GrossPay and puts the result in NetPay.
DIVIDE Total BY Members GIVING MemberAverage ROUNDED
* Divides the value in Total by the value in Members and puts
* the rounded result in MemberAverage.
DIVIDE Members INTO Total GIVING MemberAverage
* Divides the value in Members into the value in Total and puts the result in MemberAverage.
MULTIPLY 10 BY Magnitude
* Multiplies 10 by the value in Magnitude and puts the result in Magnitude.
MULTIPLY Members BY Subs GIVING TotalSubs
* Multiplies the value of Members by the value of Subs and puts the result in TotalSubs.
61
Chapter 4 ■ proCedure division BasiCs
Note that when separating contiguous operands, you may insert commas for clarity. They have no semantic
effect, as you will see if you use the following example:
DISPLAY "Date of birth = " DayOB, SPACE, MonthOB, SPACE, YearOB
ADD Sales TO ShopSales, CountySales, CountrySales
The ROUNDED Phrase
If you use the ROUNDED phrase, then, after decimal point alignment, if the result of the calculation must be truncated on the right side (least significant digits) and the leftmost truncated digit has an absolute value of five or greater, the rightmost digit is increased by one when rounded. That sounds complicated, but it isn’t. Let’s look at some examples, as shown in Table 4-1.
Table 4-1. ROUNDED Examples. Digits in the Actual Result column that will be truncated are not in bold Actual Result
Receiving Data Item
Truncated Result