Sams Teach Yourself COBOL in 24 Hours with CD-ROM

Overview

Sams Teach Yourself COBOL in 24 Hours teaches the basics of COBOL programming in 24 step-by-step lessons. Each lesson builds on the previous one providing a solid foundation in COBOL programming concepts and techniques. Coupled with the source code and the compiler available from Fujitsu, this hands-on guide is the easiest, fastest way to begin creating standard COBOL compliant code. Business professionals and programmers from other languages will find this hands-on, task-oriented tutorial extremely useful for ...

See more details below
Multimedia Set (BK&CD ROM)
$40.49
BN.com price
(Save 10%)$44.99 List Price

Pick Up In Store

Reserve and pick up in 60 minutes at your local store

Other sellers (Multimedia Set)
  • All (25) from $1.99   
  • New (10) from $24.42   
  • Used (15) from $1.99   
Sams Teach Yourself COBOL in 24 Hours

Available on NOOK devices and apps  
  • NOOK Devices
  • Samsung Galaxy Tab 4 NOOK 7.0
  • Samsung Galaxy Tab 4 NOOK 10.1
  • NOOK HD Tablet
  • NOOK HD+ Tablet
  • NOOK eReaders
  • NOOK Color
  • NOOK Tablet
  • Tablet/Phone
  • NOOK for Windows 8 Tablet
  • NOOK for iOS
  • NOOK for Android
  • NOOK Kids for iPad
  • PC/Mac
  • NOOK for Windows 8
  • NOOK for PC
  • NOOK for Mac

Want a NOOK? Explore Now

NOOK Book (eBook)
$19.99
BN.com price
(Save 44%)$35.99 List Price

Overview

Sams Teach Yourself COBOL in 24 Hours teaches the basics of COBOL programming in 24 step-by-step lessons. Each lesson builds on the previous one providing a solid foundation in COBOL programming concepts and techniques. Coupled with the source code and the compiler available from Fujitsu, this hands-on guide is the easiest, fastest way to begin creating standard COBOL compliant code. Business professionals and programmers from other languages will find this hands-on, task-oriented tutorial extremely useful for learning the essential features and concepts of COBOL programming. Writing a program can be a complex task. Concentrating on one development tool guides you to good results every time. There will be no programs that will not compile!

Read More Show Less

Product Details

  • ISBN-13: 9780672314537
  • Publisher: Sams
  • Publication date: 11/28/1998
  • Series: Sams Teach Yourself Series
  • Edition description: BK&CD ROM
  • Edition number: 1
  • Pages: 496
  • Sales rank: 1,470,698
  • Product dimensions: 7.20 (w) x 9.00 (h) x 1.10 (d)

Meet the Author

Thane Hubbell has been a COBOL programmer for 15 years. Thane possesses a working knowledge on a variety of systems, from mainframes to minis to PCs. He has designed and developed full scale systems from the initial concept or idea to the deployed product. Through his role as a flight instructor, Thane has come to understand how people learn and think in high pressure situations – like in a programming environment . Initially a self taught programmer, with later education from IBM, Thane knows what it takes to learn a programming language on your own and how to teach one.

Read More Show Less

Table of Contents

(NOTE: All chapters contain a Summary and all chapters, except for Chapter 24, conclude with Q&A and a Workshop.)

I. COBOL PROGRAM BASICS.

Hour 1. Getting Started.

COBOL Purpose and History. The Origin of COBOL. The COBOL Standard. Installing the Compiler.

Hour 2. Writing Your First Program in COBOL.

COBOL Program Layout. Creating a Simple COBOL Program. Compiling and Linking.

Hour 3. Different Data Types.

The Picture Clause. Numeric Fields. Alphanumeric Fields. Literals. Numeric Edited Fields. Alphanumeric Edited Fields. Group and Elementary Level Items. Using Data Types in a Program.

Hour 4. Basic User Interface.

Interfacing with the User. The Screen Section. Using the Screen Section in a Program.

Hour 5. Procedure Division.

Procedure Division Organization. Arithmetic Statements. Simple Data Manipulation.

Hour 6. Manipulating Data.

The Accept Statement. The Initialize Statement. The Inspect Statement. Reference Modification. Using What You Have Learned in a Program.

Hour 7. Manipulating String Data.

The String Statement. The Unstring Statement.

Hour 8. Conditional Statements.

Conditional Statements in COBOL. The Else Clause. 88 Levels and the Set Statement.

Hour 9. The Evaluate Statement.

When to Use Evaluate. Simple Evaluate Statements. More Complex Evaluate Usage.

Hour 10. Processing Loops.

The Basic Perform Statement. Sections and Paragraphs. Creating Processing Loops Using Perform.

Hour 11. Advanced Perform Statements.

Perform with Varying. The Use of the Inline Perform. The Inline If Statement and Perform. Using the Debugger.

Hour 12. Tables.

Defining a Table. Basic Table Handling. Populating a Table in Working-Storage. The Search Statement. Multidimensional Tables. Variable- Length Tables.

II. FILE HANDLING.

Hour 13. Sequential Files.

Connecting Your Program to a File. Opening the File. Closing the File. Writing to the File. Reading from the File. Updating the File. Variable-Length Records.

Hour 14. Indexed Files.

Defining the Indexed File. Creating an Indexed File from a Sequential File.

Hour 15. Reading Indexed File Records.

Various Access Methods. Random Access. Dynamic Access.

Hour 16. Updating Indexed File Records.

Opening for I-O. Writing Records. Rewriting Records. Deleting Records. Relative Files.

Hour 17. Sorting.

Sorting a File. Manipulating Data During the Sort.

III. BUSINESS PROCESSING.

Hour 18. Master File Updating.

Programming for Transaction Entry. Data Validation. Updating a Master File.

Hour 19. Reporting.

Creating Reports.

Hour 20. Advanced Reporting.

Reporting with Control Breaks.

IV. MISCELLANEOUS FUNCTIONS.

Hour 21. Date Manipulation.

Determining the Current System Date. Days Between Dates. Determining the Day of the Week for a Particular Date. Validating Dates. Other Kinds of Dates. Fun with Dates.

Hour 22. Other Intrinsic Functions.

Mathematical Functions. Statistical Functions. Financial Functions. String Functions. Miscellaneous Functions.

V. ADVANCED TOPICS.

Hour 23. The Call Interface.

Calling Other Programs. Simple Program Calling. Passing Data. Between Programs.

Hour 24. The Graphical User Interface.

Different Methods of Achieving the Graphical User Interface. Using sp2 to Create a Graphical User Interface. The Future of COBOL. Summary.

Appendix A. Quiz Exercise Questions and Answers - See CD-ROM.

Index.

Read More Show Less

First Chapter

[Figures are not included in this sample chapter]

Sams Teach Yourself COBOL in 24 Hours
- 3 -
Different Data Types

Before you can write meaningful programs, you must be able to manipulate data. In this hour, you learn about many different types of data and how to declare them in a COBOL program. This hour covers the following topics:

  • The Picture clause
  • Level numbers in COBOL
  • Numeric and alphanumeric fields
  • Defining initial values for data items
  • Editing fields for an attractive presentation

One of the strengths of COBOL is its explicit definition of various types of data. In COBOL (as well as other programming languages), data areas are referred to as fields. A field is a unique and specific piece of data, for example, an address or telephone number. In programming, when you define an area to contain this data, the area is called a field. Another term used to reference a field is data item.

The Picture Clause

In COBOL you must define a field before you can reference it in the program, using an element called the Picture clause. The word is particularly descriptive of what happens in COBOL. The Picture clause paints a picture of how a field looks by defining every detail and characteristic of the field. The Picture clause is abbreviated PIC.

The Meaning of the Different Level Numbers

NEW TERM: When a field is defined in the Data Division, a level number precedes the field. These level numbers separate fields into groups. The higher level is called a Group Level, and the level where the field's Picture clause is coded is called the Elementary Level. A Group Level item contains all the fields under it with higher level numbers.

01  Data-Field.     02  Data-Item-1        Pic X(1).     03  Data-Item-2        Pic X(1).

In the preceding example, Data-Field is a Group Level item. It has the lowest level number. Data-Item-1 and Data-Item-2 are elementary items because they contain the Picture clauses, which define the items.


NOTE: Group Level items are discussed in more detail later this hour in the "Group and Elementary Level Items" section.

Several level numbers have specific meanings in COBOL. Table 3.1 explains when each level is used.

TABLE 3.1  COBOL LEVELS AND THEIR USES

Level Description
01-49
May be used to describe data items.
01
May be used to describe a single field or the start of a group of fields. Level 01 is the only level number that may be used to either begin a group or describe an independent field.
02-49
Must appear only under a higher Group Level. These level numbers may describe further groups or individual fields under a group.
66
Reserved for the Renames clause. The Renames clause is rarely used and is not be covered in any detail in this book. The level is included here for reference.
77
Reserved for individual elementary items that are not part of a group. In practice, a level 77 item is the same as a level 01 that describes an elementary item.
88
Used for condition names. Level 88 is described in detail in Hour 8, "Conditional Statements."


The level numbers and the Picture clause are very closely related. After you examine the Picture clause, you'll have an opportunity to review the meaning of the level numbers and how to put them together in a meaningful fashion.


NOTE: The different level numbers can be coded without their leading digits. In COBOL, 01 levels are the same as 1 levels. However, I have never seen a program that did not use the leading digits. They facilitate code alignment and make the program easier to read.

Numeric Fields

COBOL supports three types of data fields: numeric, alphanumeric, and literal. This section considers numeric fields, which are simply fields containing numbers. Numeric fields are defined in the Data Division as Pic 9 items.

The 9 in the Picture clause defines a field as numeric. In COBOL a numeric field can be up to 18 digits long. When you code a Picture clause, you use a 9 to represent every numeric position of your field. If your field is two digits long, you code Pic 99. If your field is three digits long, you code Pic 999. Very large fields can get confusing, so COBOL allows you to abbreviate by putting the number of digits within parentheses. For example, Pic 99999 could be coded as Pic 9(5).

The following code might be in the Working-Storage Section of the Data Division of your program.

000023 01  Quantity-On-Hand         Pic 9(3). 000024 01  Quantity-On-Order        Pic 9(2). 000025 01  Quantity-Sold-To-Date    Pic 9(12).

Line 23 describes a numeric item that can be from 0 to 999 in value. Line 24, Quantity-On-Order, can contain from 0 to 99, and line 25, Quantity-Sold-To-Date, can contain from 0 to 999,999,999,999.

When you use these fields in the Working-Storage Section, you can initialize them with particular values. These values are set when the program starts. To use this technique, simply add a Value clause immediately following the Picture clause and before the period. For example, to initialize your Quantity-On-Hand to 20, your Quantity-On-Order to 15, and your Quantity-Sold-To-Date to 5021, you would code the following:

000023 01  Quantity-On-Hand         Pic 9(3)  Value 20. 000024 01  Quantity-On-Order        Pic 9(2)  Value 15 000025 01  Quantity-Sold-To-Date    Pic 9(12) Value 5021.

CAUTION: You should always provide an initial value for numeric data items. Most compilers do not place any special value in numeric data items, and if you use them for computations later in the program, they may contain invalid data.

When assigning a value to a numeric field, you need not worry about specifying the leading digits. The computer correctly positions the data in the numeric fields. For example, Value 20 and Value 020 yield exactly the same result.


CAUTION: Numeric fields are right-justified. That is, values proceed from the right side of the field to the left. Therefore, if you have a value of 1000 in a field with a Picture definition of three positions, the actual value the field will contain when run is 000. Most compilers warn you of this condition.



TIP: The formatting of the various lines of field definitions is almost entirely up to you. What you see in the examples is the most common method, but you can line up the clauses any way you desire. Nicely formatted source code is relatively easy to read, and I suggest that you be as consistent as possible. In reality, the Value clause does not have to follow the Picture clause, and even can precede it. Remember to terminate each line of field definition with a period. The field name must always be the first item after the level number. Remember that field names are limited to 30 characters. Try to make the names as descriptive as possible; doing so makes your program that much easier to read and maintain.

Decimal Values

NEW TERM: When working with numbers, especially in business, you often need to work with decimal values. In COBOL specifying the decimal point's position is extremely easy. In the Picture clause, a v represents the decimal point. The symbolic v is called an implied decimal position. The decimal point does not take up any additional storage space.

000026 01  Cost-of-each-item     Pic 9(5)v9(2) value 10.00. 000027 01  Average-cost          Pic 9(3)v9(4) value 10.0000. 000028 01  Overall-dollars       Pic 9(7)      value 10.

Line 26 represents a number that contains two decimal positions. The numbers can range in value from 0 to 99999.99. Line 27 represents a number that contains four decimal positions. Line 28 represents a number that has no decimal positions. All three examples, however, take up exactly the same amount of internal storage and, by using the Value clause, have the same values. Notice how the v splits the Picture clause, and the 9 must be repeated followed by the number of positions desired.


CAUTION: Remember that the maximum size of a numeric data item in COBOL is 18 digits. Regardless of where you place the decimal point, the field must not exceed 18 digits.

Handling the Sign

Under many circumstances, you may want to handle numbers that are both positive and negative, or signed numbers. You specify a signed numeric field by placing an S immediately after the Picture clause and before the 9s that represent the positions of the numbers.

000029 01  Net-Profit     Pic S9(5)v9(2) Value -10.00.

Like the decimal point, unless explicitly stated otherwise, the sign does not take up any storage positions. Notice how the negative value is represented in the Value clause. Different versions of COBOL on different types of computers store the sign with different internal representations. For the most part, the COBOL programmer need not be concerned with this issue. However, if the data is to be shared among different computers or different programming languages, the programmer might want to make the sign of the number a separate character, thus eliminating any problems with differences in internal representation. To do so, add the Sign Separate clause to the definition of the field. With this clause, you must specify the position of the sign in relation to the rest of the number. Both positive and negative signs are represented--the positive with a + character and the negative with a -.

When Sign Separate is used, the sign takes up a position of storage.

000030 01  Monthly-Net-Profit       Pic s9(5)v9(2) Sign is Leading   000031 001 Separate Character. 000032 01  Quarterly-Net-Profit     Pic s9(5)v9(2) Sign Trailing Separate.

In line 30, the sign leads the data value; a positive number is represented by a leading + sign, and a negative with a leading -, for example, +00010.00 and -00010.00.

Line 32 shows an example with the sign trailing. Notice the omission of the words Is and Character. Many COBOL statements may be abbreviated in this fashion.

The Usage Clause

The Usage clause tells the computer how to represent numbers internally. You can realize performance gains in your programs by representing numbers in a way that allows the computer to use numbers without translating them into a more usable (to the computer!) format.

The default usage, when none is specified, is Usage Display. Usage Display works just like it sounds. The numbers are represented in the same format as a normal display of numbers. All the examples so far have utilized Usage Display. Each position of a number takes up a character, or byte, of storage.

000033 01  Yearly-Net-Profit     Pic s9(5)v9(2) Value Zeros. 000034 01  Yearly-Gross-Profit   Pic s9(5)v9(2) Value Zeros Sign Leading 000035 01 Separate.

Line 33 takes 7 bytes of storage: 5 bytes for the leading digits and 2 bytes for the decimal positions. Notice that the sign and the implied decimal do not take up any extra storage positions. By contrast, line 34 takes 8 bytes of storage. The extra byte is used because the definition specifies that the sign is a separate character.

In addition to improving performance efficiency, Usage clauses can save storage space.


WARNING: Compiler vendors determine the actual internal representation associated with Usage clause values. The most common representations and uses are discussed here.

The values of the various Usage clauses are

  • Computational
  • Comp
  • Display
  • Binary
  • Index
  • Packed-Decimal

Computational and Comp are the same thing. In addition to Comp, most compiler vendors provide Comp-1, Comp-2, Comp-3, and so on as values of the Usage clause. Each value represents a different internal storage method for numeric data. The actual storage space used and how each is represented vary with different computers and COBOL compilers. Usage Index passes the value of an index item to other programs or stores an index item in a file. This clause is seldom used, and often discouraged, as different computers represent index values differently. Index values are discussed in more detail in Hour 12, "Tables."

Packed-Decimal and Binary may or may not be supported by the different compiler vendors, depending on the target computer's capability to handle these data types.

For example, Packed-Decimal is a way to "pack" numeric values into a smaller area. Each byte of data is made up of two sets of half-bytes, or nibbles. A number can be represented in a single nibble of data. Packed-Decimal reserves the last nibble of the data area representing a number for the sign. Each number position in Packed-Decimal usage takes one nibble.

Table 3.2 shows the internal byte, or character representation, of two Packed-Decimal defined numbers. The first is positive, and the second is negative. Notice how the sign is stored as a C in the last half-byte if the number is positive and as a D if the number is negative. Also, note that five digits are being stored in 3 bytes. If the number had six or seven digits, it would take 4 bytes.

TABLE 3.2  INTERNAL REPRESENTATION OF PACKED DECIMAL

Picture Clause Internal Representation Byte 1, Byte 2, Byte 3
Pic S9(5)
Packed-Decimal
Value 12345.
12 34 5C
Pic S9(5)
Packed-Decimal
Value -12345.
12 34 5D


Comp fields are also packed in a method similar to Packed-Decimal but with slightly different rules. Comp fields take up space in 2-byte increments. A single-digit number, Pic 9 Comp, takes 2 bytes. The sign is stored in the left-most bit of the storage area.


NOTE: Byte is another word for a single character of data. A byte is made up of 8 bits. Each bit has a value of either 1 or 0. A nibble is half a byte. When representing these byte values to humans, the computer uses hexadecimal notation. The binary 1s and 0s are converted into their single-digit base 16 equivalent. These numbers range from 0-9 and then go to A-F.The beginning programmer needs to understand that there is a difference between these representations and needs to know how to determine exactly how much space each number is using. Your compiler's manual has a section on the internal representation of Usage clauses. Each vendor may differ in its representation and the amount of space used by the different Usage types.

Alphanumeric Fields

Alphanumeric fields can contain information other than numbers. An alphanumeric field could contain any data, including numbers. However, when an alphanumeric field does contain numeric data, it cannot be used as a number. In COBOL, alphanumeric fields are indicated in the Picture clause as X items.

000034 01  Customer-Name     Pic X(30).

Line 34 defines an item called Customer-Name that contains 30 characters. Just as with numeric items and the associated placeholder of 9, the X in the Picture clause of an alphanumeric item corresponds to one position of the field.

Value clauses may be applied to alphanumeric items. Values assigned to alphanumeric items are enclosed within quotation marks.

000035 01  Customer-Name     Pic X(30) Value "John Jones".

Line 35 assigns the value John Jones to the field titled Customer-Name.

You need not specify the trailing spaces when assigning a value to an alphanumeric data item because COBOL automatically fills the remaining characters of the field with spaces.


CAUTION: You should remember that, unlike numeric fields where the numbers of the Value clause are correctly positioned in the field, alphanumeric items are left-justified. That is, they start from the left-most position in the field and proceed to the right. If your field is shorter than your value clause, the right-most characters will be truncated.




TIP: If you have an alphanumeric field in which you want to repeat a value, for instance, "*", you can code the field as either Pic X(20) Value All "*" or Pic X(20) Value "********************".

Literals

Literals are items that are specified explicitly by their values. You have already seen literals in action. Any of the Value clause items specified earlier are considered literals. The "Hello World" in your first COBOL program was a literal. Alphanumeric literals are enclosed within quotation marks, whereas numeric literals are not.

The following are some examples of numeric literals:

  • 1

  • 76

  • -12.73

The following are some examples of alphanumeric literals:

  • "Uncle"
  • "Aunt"
  • "Computer"

COBOL provides some special-use literals to make programming easier. The values of these literals are exactly as they sound:

  • Spaces. Spaces are blank characters and are part of the alphabetic portion of the character set used by the computer. Space may be used instead of Spaces.
  • Zeros. Zeros specifies a numeric literal of the value zero. When used with an alphanumeric field, all characters in that field are changed to a zero. Zeroes or Zero may be substituted for Zeros.
  • Quote. The Quote literal specifies a quotation mark. Most compilers will accept """", to indicate a single quotation mark, but this provided literal is much clearer. Quotes may be substituted for Quote.
  • Low-Value. Low-Value is the lowest value of a storage item in the computer's collating sequence. It is valid only with alphanumeric fields. When compared to any other field, Low-Value is always less. The internal representation of Low-Value in most computers is that of all bits in a byte set to zero. Low-Value is NOT equal to either zeros or spaces. Low-Values may be substituted for Low-Value.
  • High-Value. In contrast to Low-Value, High-Value is the highest value in the computer's collating sequence. It is valid only with alphanumeric fields. When compared to any other field, High-Value is always greater. The internal representation of High-Value in most computers is that of all bits in a byte set to one. High-Value is not equal to either the letter Z or the number 9 unless those characters are the highest characters in the computer's collating sequence. High-Values may be substituted for High-Value.

Literals are used throughout the COBOL language. In this book, you will see numerous examples of their use.

Numeric Edited Fields

When the computer uses numeric fields internally, their representation does not much matter to the programmer. However, when these numbers are reported to the user in output from the program, their appearance becomes very important. COBOL provides some very powerful tools for editing numeric fields for either printing or display.

Numbers that are edited are much easier to read than numbers that aren't. For example, 123999873.32 is not as easy to read as 123,999,873.32. With computers, if you don't edit the numbers, a large numeric field might appear to the user as 0000000019.99. In COBOL you can edit this field to appear as 19.99.

When a number is moved to a numeric edited field, the computer treats that number as an alphanumeric field. You cannot reference the numeric edited field as a number within your program except as the object of a move or compute statement (as discussed in Hour 5, "Procedure Division"). Numbers moved to numeric edited items remain right justified. This feature is particularly useful because columns of numbers should remain aligned on a printed report.

Table 3.3 shows the difference between edited and unedited numbers.

TABLE 3.3  EDITED AND UNEDITED NUMBERS

Edited Numbers Unedited Numbers
123,456.78
00012345678
1,000.00
000100000
12.99
000001299


Table 3.4 shows how much easier aligned numbers are to read in a column compared to nonaligned numbers.

TABLE 3.4  ALIGNED AND NONALIGNED NUMBERS

Aligned Numbers Nonaligned Numbers
123,456.78
123,456.78
1,000.00
1,000.00
12.99
12.99


Several Picture clause values can be used to edit a number.

.
Inserts a decimal point at the position of the implied decimal point
Z
Indicates zero suppression
*
Indicates zero suppression and replaces the zero with an *
- or +
Indicates negative or positive sign
CR or DB
Indicates credit or debit balances
$
Indicates the currency symbol
B
Indicates a blank fill character
/
Inserts a slash character in the representation of a numeric field
0
Inserts a zero character
,
Inserts a comma character


The . Picture item shows the decimal point in a numeric field. When coded, the . takes the place of the implied decimal point in a numeric field.

The Z Picture item indicates zero suppression of digits. When a Z is used and the number in that position is a zero, a blank or space is placed in the field instead. Once the first nonzero value is encountered, no further Z characters are replaced with blanks.

000036 01  Edited-Number      Pic ZZZZZ. 000037 01  Edited-Number-Also Pic Z(5).

Lines 36 and 37 have the same representation. Like the X and 9 items of the Picture clause, the Z item may be repeated by using the parenthetical notation for the number of positions to occupy with the Z.

For example, if a numeric field contained 000010.01 and it was moved to a field defined as Pic ZZZZZZ.ZZ, the actual value in the field would be " 10.01". Note the four leading spaces and the fact that the 0s after the initial 1 are not replaced with spaces. Instead of coding ZZZZZZ.ZZ, you could code Z(6).ZZ.

The * performs a similar zero suppression. The only difference is that instead of replacing the zeros with spaces, they are replaced with *. An item with a numeric value of 10.00 and a picture of *(5).** would have a value of ***10.00.

The - item indicates the position of the sign. If the number is negative, then the - appears. If the number is positive, the - is not displayed and a space appears instead. You may use multiple - characters in a single picture clause. This notation will both zero suppress and place the - sign in the right-most position that contains a - sign. Consider the following:

000038 01  Edited-Number       Pic -9(5). 000039 01  Edited-Number-Also  Pic ----9. 000040 01  Edited-Number-Too   Pic -(4)9. 000041 01  Edited-Number-Again Pic 9(5)-.

If the number 10 were placed in these fields, line 38 would appear as " 00010". Note the leading space. Line 39 would appear as " 10".

If negative 10 were in these fields, line 38 would appear as "-00010" and line 39 as " -10". Line 40 depicts exactly the same representation as line 39. Line 41 shows the sign trailing the field. If the field contained a negative 10, it would appear as "00010-".

The + item works similarly to the - item. The + displays a - sign if the field is negative and a + sign if the field is positive.

The CR and DB items are related. Each takes two positions and must be specified either at the beginning or at end of the data item. In either case, if the number is negative, the CR or DB appears. If the number is positive, the CR or DB does not appear. This feature is especially useful for credit or debit balances. If a person owes a negative amount, he or she has a credit, or CR. A transaction is applied to that account might be a negative number and show as a debit, or DB.

Table 3.5 illustrates some examples of debit and credit usage.

TABLE 3.5  CREDIT AND DEBIT EDITED FIELDS

Value Edited Field Appearance
12345.00
Pic 9(5).99DB
12345.00
12345.00-
Pic 9(5).99DB
12345.00DB
12345.00
Pic 9(5).99CR
12345.00
12345.00-
Pic 9(5).99CR
12345.00CR


The $ Picture item indicates currency. Like the Z, *, and -, the $ performs zero suppression. You may repeat the character at the start of a field to "float" the dollar sign along with the numbers, or you may code a single $ and have it fixed in position.

000042 01  Dollar-Field     Pic $$$$$.99. 000043 01  Dollar-Field-Too Pic $9999.99.

If each of these fields contained 10.00, the first would appear as " $10.00" and the second as "$0010.00". The $ sign appears by default, but if your country uses a different symbol for its currency, you may use the Special-Names paragraph to change the character that appears. The $ is still used to indicate that currency in your picture clause.


CAUTION: When using the $, realize that the field is always displayed with at least one leading $. Consequently, if you define a field as $$$.00 and move 100 into the field, the 1 does not display and the field appears as $00.00.

The B (blank), / (slash), comma (,), and 0 characters all behave in the same manner. They are insertion characters (see Table 3.6) and appear in your numeric field exactly where coded. They are not replacing values in your numeric field, but are instead inserting characters.

TABLE 3.6  INSERTION CHARACTERS IN NUMERIC FIELDS

Numeric Value Picture Appearance
12311999
99/99/9999
12/31/1999
123456
999,999
123,456
1234
999900
123400
4095551212
9(3)B9(3)B9(4)
409 555 1212

Alphanumeric Edited Fields

It is also often useful to apply edit patterns to alphanumeric fields. COBOL provides several edit patterns to make that job easy.

  • B to insert a blank character
  • / to insert a slash
  • 0 to insert a zero

Just as in the numeric data fields, the B, /, and 0 insert these characters wherever they are encountered. See Table 3.7.

TABLE 3.7  INSERTION CHARACTERS IN ALPHANUMERIC FIELDS

Alphanumeric Value Picture Appearance
MMDDYYYY
XX/XX/XXXX
MM/DD/YYYY
ABCDEFG
XBXBXBXBXBXBX
A B C D E F G
ABCDEF
X0X0X0X0X0X
A0B0C0D0E0F

Group and Elementary Level Items

These levels are basically two methods of referencing items that are defined in the Data Division. You may reference either an Elementary or Group level item. Group Level items have subordinate Elementary Level items. Any item that has items with subordinate level numbers under it is a Group Level item. The compiler treats Group Level items as alphanumeric variables. Any item with a final definition, with no further subordinate items, is an Elementary Item.

000044 01  Numeric-Fields. 000045     03  Field-1            Pic 9(5). 000046     03  Field-2            Pic 9(5).

In this example, line 44 represents the Group Level item. It is made up of two elementary items: Field-1 and Field-2. Either the Group Level item or the elementary items may have a Value clause, but not both. The rules for the Value clause at the Group Level are the same as those for alphanumeric items.


CAUTION: Using the Value clause at the Group Level is strongly discouraged. It is an easy way to get nonnumeric data into numeric data fields that appear under the Group Level. No examples or exercises in this book assign a value to a Group Level item.

Notice that subordinate items have higher level numbers. Consider this example:

000047 01  Numeric-Fields. 000048     03  Amount-Fields. 000049         05  Amount-1         Pic 9(5)v99. 000050         05  Amount-2         Pic 9(5)v99. 000051     03  Quantity-Fields. 000052         05  Quantity-1       Pic 9(5). 000053         05  Quatity-2       Pic 9(5).

Line 47 is a group field composed of four elementary items. Lines 48 and 51 are also group fields: They are made up of two elementary fields each.


TIP: In the preceding example, the level numbers are aligned and indented. This common practice is highly recommended. The compiler is perfectly capable of figuring out the levels based solely on the level numbers; however, the programmer would have trouble reading the program if all the level numbers started in the same column.

Listing 3.1 demonstrates the use of Group and Elementary Levels.

LISTING 3.1  DEMONSTRATE GROUP AND ELEMENTARY LEVELS

000001 @OPTIONS MAIN 000002 Identification Division. 000003 Program-Id.  Chapt03a. 000004 Environment Division. 000005 Configuration Section. 000006 Source-Computer.  IBM-PC. 000007 Object-Computer.  IBM-PC. 000008 Data Division. 000009 Working-Storage Section. 000010 01  Hello-Text. 000011     03  Part-One Pic X(6) Value "Hello ". 000012     03  Part-two Pic X(5) Value "World". 000013 Procedure Division. 000014 Chapt2a-Start. 000015     Display Part-One. 000016     Display Part-Two. 000017     Display Hello-Text. 000018     Stop Run.

In Listing 3.1, when the elementary item Part-One is displayed, the word "Hello " appears. When the elementary item Part-Two is displayed, the word "World" appears. But when the Group Level item, Hello-Text is displayed, you see the entire group, "Hello World", displayed. Level numbers 02-49 must define elements under a Group Level. Each can be its own subgroup level if it has further subordinate elementary items under it. You may skip any level numbers you desire, so long as each subordinate item starts with a higher level number than the group that contains it.

FIGURE 3.1 The output from Listing 3.1.

Level 01 is unique among the first 49 level numbers in that it can start a group definition or it may be an elementary item on its own. The first examples earlier in this hour used level number 01 as an elementary item.

Level 77 items are the same as level 01 elementary items. Level 77 items must be elementary items, may not be part of a group, and may not define a group. They must stand alone.

When you need to define a data item, but you do not need to directly reference it, or when you need to just reserve some space for future expansion, COBOL allows you to use the reserved word Filler. Filler is essentially what it sounds like. It is an area that is defined and takes up space, but has no associated data name. If you wanted to define a Group Level item that contained a first and last name separated by a space, you could use a Filler item.

000020 01  Full-Name. 000021     03  First-Name         Pic X(20). 000022     03  Filler             Pic X Value Spaces. 000023     03  Last-Name          Pic X(30).

Note that you may assign a value to a Filler item, just as with any other elementary item.


CAUTION: When defining a Filler item, the word Filler is optional. However, I suggest that you code the word whenever you define Filler area. I find programs that omit the word very hard to read.

Using Data Types in a Program

The next program uses some of the data types described in this lesson. To use these items, you must first define them in the Working-Storage Section of the Data Division.

Working-Storage is an area defined in your COBOL program for use by your program, only while your program is running. Any data that you want to reference internally in your program that does not come from an outside source, such as a file, is defined in Working-Storage.


TIP: Fields defined in Working-Storage can be as organized or disorganized as you allow them to be. However, programming is easier if you place like fields into groups. Having similarly used fields scattered about Working-Storage makes the program harder to maintain later.

Edited fields may have a Value clause associated with them. However, these are rarely used. Edited fields are treated as alphanumeric items, and thus your Value clause must consist of an alphanumeric item. If your value does not match the pattern of the edit, the program will still use the "invalid" Value clause contents.

The edit patterns defined for an edited numeric or alphanumeric data item are applied when data is moved into the fields with a Move statement. The Move statement is discussed in detail later in the book. This session uses simple Move statements.

The Move statement causes the first data item to be moved into the second data item.

000050      Move Field-1 To Field-2.

In this example, the contents of Field-1 are moved into Field-2. If Field-2 is an edited data item, then the edit pattern specified in your picture clause is applied.

Open the Fujitsu COBOL editor, following the same steps outlined in Hour 2, "Writing Your First Program in COBOL." Enter the following program shown in Listing 3.2.

LISTING 3.2  DEMONSTRATE EDITED FIELDS

000001 @OPTIONS MAIN 000002 Identification Division. 000003 Program-Id.  Chapt03b. 000004 Environment Division. 000005 Configuration Section. 000006 Source-Computer.  IBM-PC. 000007 Object-Computer.  IBM-PC. 000008 Data Division. 000009 Working-Storage Section. 000010 01  Group Level-Item. 000011     05  Elementary-Numeric       Pic 9(7)        Value 12345. 000012     05  Elementary-Numeric-Dec   Pic 9(5)v99     Value 123.45. 000013     05  Elementary-Numeric-Sign  Pic S9(5)v99    Value -123.45. 000014 01  Edited-Group-Item. 000015     05  Elementary-Zero-Sup      Pic Z(6)9. 000016     05  Elementary-Aster-Sup     Pic ******9. 000017     05  Elementary-Edited        Pic Z,Z(3),Z(3). 000018     05  Elementary-Edited-Dec    Pic Z,Z(3),Z(3).99. 000019 01  Group-Alphanumeric-Item. 000020     05  Elementary-Alphanum      Pic X(20) 000021         Value "ABCDEFGHIJKLMNOPQRST". 000022     05  Elementary-Alphanum-A    Pic X(6) 000023         Value "UVWXYZ". 000024 01  Group-Alphanumeric-Edited. 000025     05  Edited-Alphanumeric      Pic X(3)/X(3)/X(3). 000026 Procedure Division. 000027 Chapt03b-Start. 000028     Move Elementary-Numeric     to Elementary-Zero-Sup. 000029     Move Elementary-Numeric     to Elementary-Edited. 000030     Move Elementary-Numeric     to Elementary-Aster-Sup. 000031     Move Elementary-Numeric-Dec to Elementary-Edited-Dec. 000032     Move Elementary-Alphanum    to Edited-Alphanumeric. 000033     Display "1 Group Alphanumeric="      Group-Alphanumeric-Item. 000034     Display "2 Elementary Alpha="        Elementary-Alphanum. 000035     Display "3 Elementary Alpha A="      Elementary-Alphanum-A. 000036     Display "4 Edited Alphanumeric="     Edited-Alphanumeric. 000037     Display "5 Group Level Item="        Group Level-Item. 000038     Display "6 Elementary Numeric="      Elementary-Numeric. 000039     Display "7 Elementary Numeric Dec="  Elementary-Numeric-Dec. 000040     Display "8 Elementary Numeric Sign=" Elementary-Numeric-Sign. 000041     Display "9 Elementary Zero Sup="     Elementary-Zero-Sup. 000042     Display "10 Elementary Aster Sup="   Elementary-Aster-Sup. 000043     Display "11 Elementary Edited="      Elementary-Edited. 000044     Display "12 Elementary Edited Dec="  Elementary-Edited-Dec. 000045     Stop Run.

Notice the spacing to align the data names. This type of source formatting is entirely up to you. In this example, the data items are aligned to make the source easier to read. The number of spaces between the display literal and the data item does not affect the actual display. Although at least one space must separate items, the compiler ignores any other spaces.

Each Display statement in this example actually displays two items. The first is an identifying alphanumeric literal, and the second is the data item.

Save the program as CHAPT03B.COB in your TYCOBOL folder. You might have to reselect that folder. The name is very important. Make sure that the Program-Id is CHAPT03B. After saving the program, close the editor and then compile and link the program as you did in Hour 2. If you have any compiler error messages, remember that you can position the cursor at the start of the line in error and press F11 to position the editor on the actual source line in error. When you get a clean compile and link, run the program.

When The console window is closed message appears, move it down and to the right so you can examine the output of your program.

FIGURE 3.2 Output from Listing 3.2.

Refer to your output and take note of the following:

  • Line 1 shows the entire alphanumeric group item Group-Alphanumeric-Item made up of the two elementary items Elementary-Alphanum and Elementary-Alphanum-A. As you can see from the display, the Group Level is treated as a single alphanumeric variable.
  • Lines 2 and 3 show the individual elementary items that make up the alphanumeric group: Elementary-Alphanum and Elementary-Alphanum-A.
  • Line 4 demonstrates the insertion of the / characters by the edit pattern. Note that the / does not replace any letters in Elementary-Alphanum. Also, notice that the entire elementary item that was moved to the edit pattern is not displayed. The move operation stopped when the field you were moving to, also known as the receiving field, was full.
  • Line 5 is perhaps the most interesting. Notice how all the numbers appear on one line. The group item is made up entirely of numeric elementary items. No decimal points appear because the decimal position is implied by the v. Another interesting observation is that the last character is a U. The computer stores the negative sign within the same byte as the last number in the numeric item, and when this strange value is translated into a display character, it ends up being a U. Consequently, you should be very careful with your references to numeric items and to the groups that might contain them.
  • Line 6 shows the first elementary numeric item. The leading zeros are displayed, even though they were not specified in the Value clause. The computer handles that for you.
  • Line 7 shows the second elementary numeric item. This item was specified with a decimal point, and yet none is displayed. In this case, the decimal point position is implied and does not take up a storage position.
  • Line 8 is the display of the field with a negative value. The Fujitsu compiler converts the item, and the sign is displayed. Other compilers may not be this forgiving. Some would display the same thing you saw in the display of the Group Level item containing the numeric fields.
  • Line 9 demonstrates your first use of a numeric edited field. The leading zeros are suppressed, that is, replaced by spaces.
  • Line 10 also is an example of a numeric edited field, but this time the leading zeros are replaced by the * character. Refer to your source code and see why.
  • Line 11 shows the insertion of the , edit character. Although you specified other commas, those that would have appeared between leading zeros have been replaced with spaces by the compiler. This capability is a very powerful editing feature of COBOL.
  • Line 12 shows the combination of zero suppression and the placement of the decimal point. If you had specified the picture clause to be Pic ZZZZZ.999, the number would have been displayed as " 123.450". The compiler automatically aligns the decimal point at the position of the implied decimal in the item being moved to the edited field.

Summary

In this hour, you learned many things that will be the foundation for your future COBOL programming.

  • The meaning of the Picture clause
  • The various types of data items: numeric and nonnumeric literals, numeric fields, alphanumeric fields, numeric edited fields, and alphanumeric edited fields
  • The Value and Usage clauses
  • The meaning of different level numbers
  • The difference between an Elementary and a Group Level item
  • How to apply powerful editing to numeric and alphanumeric fields
  • How to handle signs and decimal points

Q&A

Q What is the purpose of the Picture clause?
A The Picture clause describes the type of data item to be used in a COBOL program.

Q Can Group Level items be numeric?
A No, Group Level items are always handled as alphanumeric items by the compiler.

Q What is the maximum size of a numeric item in COBOL?
A Cobol is limited to 18 digits. It does not matter on which side of the decimal point these numbers appear. The total number of digits may not exceed 18.

Q Where are data items defined in a COBOL program?
A Data items are always defined in the Data Division. Any item referenced by your COBOL program must either be a special variable defined by the COBOL language or declared in the Data Division.

Workshop

To help reinforce your understanding of the material presented in this hour, refer to the section "Quiz and Exercise Questions and Answers" that can be found on the CD. This section contains quiz questions and exercises for you to complete, as well as the corresponding answers.

Read More Show Less

Customer Reviews

Be the first to write a review
( 0 )
Rating Distribution

5 Star

(0)

4 Star

(0)

3 Star

(0)

2 Star

(0)

1 Star

(0)

Your Rating:

Your Name: Create a Pen Name or

Barnes & Noble.com Review Rules

Our reader reviews allow you to share your comments on titles you liked, or didn't, with others. By submitting an online review, you are representing to Barnes & Noble.com that all information contained in your review is original and accurate in all respects, and that the submission of such content by you and the posting of such content by Barnes & Noble.com does not and will not violate the rights of any third party. Please follow the rules below to help ensure that your review can be posted.

Reviews by Our Customers Under the Age of 13

We highly value and respect everyone's opinion concerning the titles we offer. However, we cannot allow persons under the age of 13 to have accounts at BN.com or to post customer reviews. Please see our Terms of Use for more details.

What to exclude from your review:

Please do not write about reviews, commentary, or information posted on the product page. If you see any errors in the information on the product page, please send us an email.

Reviews should not contain any of the following:

  • - HTML tags, profanity, obscenities, vulgarities, or comments that defame anyone
  • - Time-sensitive information such as tour dates, signings, lectures, etc.
  • - Single-word reviews. Other people will read your review to discover why you liked or didn't like the title. Be descriptive.
  • - Comments focusing on the author or that may ruin the ending for others
  • - Phone numbers, addresses, URLs
  • - Pricing and availability information or alternative ordering information
  • - Advertisements or commercial solicitation

Reminder:

  • - By submitting a review, you grant to Barnes & Noble.com and its sublicensees the royalty-free, perpetual, irrevocable right and license to use the review in accordance with the Barnes & Noble.com Terms of Use.
  • - Barnes & Noble.com reserves the right not to post any review -- particularly those that do not follow the terms and conditions of these Rules. Barnes & Noble.com also reserves the right to remove any review at any time without notice.
  • - See Terms of Use for other conditions and disclaimers.
Search for Products You'd Like to Recommend

Recommend other products that relate to your review. Just search for them below and share!

Create a Pen Name

Your Pen Name is your unique identity on BN.com. It will appear on the reviews you write and other website activities. Your Pen Name cannot be edited, changed or deleted once submitted.

 
Your Pen Name can be any combination of alphanumeric characters (plus - and _), and must be at least two characters long.

Continue Anonymously

    If you find inappropriate content, please report it to Barnes & Noble
    Why is this product inappropriate?
    Comments (optional)