Michael Coughlan

Home > Other > Michael Coughlan > Page 7


  Listing 2-4 accepts a character from the user and displays a message to say whether the character entered was a vowel, a consonant, or a digit. When CharIn receives a character from the user, the associated condition names are all set to true or false depending on the value contained in CharIn.

  The EVALUATE verb, which is COBOL’s version of switch or case, is shown here at its simplest. It is immensely powerful, complicated to explain, but intuitively easy to use. In this program, the particular WHEN branch executed depends on which condition name is true. See anything familiar, Ruby programmers?

  33

  Chapter 2 ■ COBOL FOundatiOn

  Listing 2-4. Using the EVALUATE Verb

  IDENTIFICATION DIVISION.

  PROGRAM-ID. ConditionNames.

  AUTHOR. Michael Coughlan.

  * Using condition names (level 88's) and the EVALUATE

  DATA DIVISION.

  WORKING-STORAGE SECTION.

  01 CharIn PIC X.

  88 Vowel VALUE "a", "e", "i", "o", "u".

  88 Consonant VALUE "b", "c", "d", "f", "g", "h"

  "j" THRU "n", "p" THRU "t", "v" THRU "z".

  88 Digit VALUE "0" THRU "9".

  88 ValidCharacter VALUE "a" THRU "z", "0" THRU "9".

  PROCEDURE DIVISION.

  Begin.

  DISPLAY "Enter lower case character or digit. Invalid char ends."

  ACCEPT CharIn

  PERFORM UNTIL NOT ValidCharacter

  EVALUATE TRUE

  WHEN Vowel DISPLAY "The letter " CharIn " is a vowel."

  WHEN Consonant DISPLAY "The letter " CharIn " is a consonant."

  WHEN Digit DISPLAY CharIn " is a digit."

  END-EVALUATE

  ACCEPT CharIn

  END-PERFORM

  STOP RUN.

  Chapter Exercise

  Write a version of the ConditionNames program in your favorite language. See if you can convince yourself that your version is as clear, concise, readable, and maintainable as the COBOL version.

  Where to Get a COBOL Compiler

  Now that you’ve seen the basics of COBOL, it’s time to get the software. A couple of years ago, the question of where to get a free COBOL compiler would have been difficult to answer. The policies of COBOL vendors, who were locked into mainframe thought patterns and pricing structures, made it very difficult for interested students to get access to a COBOL compiler. In very recent years, though, and probably in response to the shortage of COBOL programmers, a number of options have become available.

  Micro Focus Visual COBOL

  Micro Focus COBOL is probably the best-known version of COBOL for Windows PCs. Micro Focus Visual COBOL

  is the company’s most recent version of COBOL. It implements the OO-COBOL standard and integrates either

  with Microsoft Visual Studio (where it acts as one of the standard .NET languages) or with Eclipse. It is available on Windows, Linux (Red Hat and SuSE) and Unix (Aix, HP-UX, and Solaris).

  34

  Chapter 2 ■ COBOL FOundatiOn

  A personal edition of Visual COBOL is available that is free for non-commercial use. The Visual Studio version can be installed even if Visual Studio is not available, because in that case the Visual Studio Shell edition is installed.

  www.microfocus.com/product-downloads/vcpe/index.aspx

  OpenCOBOL

  OpenCOBOL is an open source COBOL compiler. The OpenCOBOL web site claims to implement a substantial part

  of the ANS 85 and ANS 2002 COBOL standards as well as many of the extensions introduced by vendors such as Micro Focus and IBM.

  OpenCOBOL translates COBOL into C. The C code can be compiled using the native C compiler on a variety of

  platforms including Windows, Unix/Linux, and Mac OS X.

  The compiler is free and is available from www.opencobol.org/.

  Raincode COBOL

  Raincode is a supplier of programming-language analysis and transformation tools. The company has a version of COBOL available that integrates with Microsoft Visual Studio and generates fully managed .NET code. The COBOL

  compiler is free from www.raincode.com/mainframe-rehosting/.

  Compileonline COBOL

  An online COBOL compiler is available at compileonline.com. Its data input is somewhat problematic, which limits its usefulness, but it can be handy if you just want a quick syntax check. See

  www.compileonline.com/compile_cobol_online.php.

  Fujitsu NetCOBOL

  Fujitsu NetCOBOL is a very well-known version of COBOL for Windows. NetCOBOL implements a version of the

  OO-COBOL standard, compiles on the .NET Framework, and can interoperate with other .NET languages such as

  C# and VB.NET.

  A number of other versions of this COBOL are available, including a version for Linux. A trial version is available for download but there is no free version: www.netcobol.com/product/netcobol-for-net/.

  Summary

  This chapter explored part of the foundational material required to write COBOL programs. Some of the material covered was informational, some practical, and some advisory. You saw how COBOL programs are organized

  structurally and learned the purpose of each of the four divisions. You examined COBOL metalanguage diagrams and the COBOL coding and name construction rules. I offered advice concerning name construction and the proper formatting of program code. Finally, you examined some simple COBOL programs as a preview of the material in coming chapters.

  The next chapter examines how data is declared in COBOL. This chapter is only an introduction, though.

  Data declaration in COBOL is complicated and sophisticated because COBOL is mainly about data manipulation.

  COBOL data declarations offer many data-manipulation opportunities. Later chapters explore many advanced

  data-declaration concepts such as condition names, table declarations, the USAGE clause, and data redefinition using the REDEFINES clause.

  35

  Chapter 2 ■ COBOL FOundatiOn

  References

  1. Sammet J. The early history of COBOL, 2.6: intended purpose and users. ACM SIGPLAN Notices. 1978; 13(8): 121-161.

  2. Kloth RD. Cardpunch emulator. www.kloth.net/services/cardpunch.php

  3. Myres G. Composite/structured design. New York: Van Nostrand Reinhold; 1978.

  4. Constantine L, with Yourdon E. Structured design. Yourdon Press; 1975.

  5. Page-Jones M. Practical guide to structured systems design. 2nd ed. Englewood Cliffs (NJ): Prentice Hall, 1988.

  6. Stevens W, Myers G, Constantine L. Structured design. In Yourdon E, editor. Classics in software engineering.

  Yourdon Press; 1979: 205-232.

  COBOL pUZZLe aNSWer

  Given the description of Birthdate in Listing 2-5, what do you think would be displayed by the COBOL code in Listing 2-6?

  Listing 2-5. BirthDate data description

  01 BirthDate.

  02 YearOfBirth.

  03 CenturyOB PIC 99.

  03 YearOB PIC 99.

  02 MonthOfBirth PIC 99.

  02 DayOfBirth PIC 99.

  Listing 2-6. Code manipulating BirthDate and its subordinate items

  MOVE 19750215 TO BirthDate

  DISPLAY "Month is = " MonthOfBirth

  DISPLAY "Century of birth is = " CenturyOB

  DISPLAY "Year of birth is = " YearOfBirth

  DISPLAY DayOfBirth "/" MonthOfBirth "/" YearOfBirth

  MOVE ZEROS TO YearOfBirth

  DISPLAY "Birth date = " BirthDate.

  COBOL Puzzle Answer

  Month is = 02

  Century of birth is = 19

  Year of birth is = 1952

  15/02/1952

  Birth date = 00000215

  36

  Chapter 3

  Data Declaration in COBOL

  As you explore the COBOL language, you will notice many differences between it and other programming languages.

  Iteration, selection, and assignment are all done differ
ently in COBOL than in languages like C, Java, and Pascal. But on the whole, these differences are fairly minor—more a question of nuance than a radical departure. When you are familiar with how iteration and selection work in other languages, COBOL’s implementation requires only a small mental adjustment. Assignment might create more of a hiccup, but there is nothing too radical even in that. The real difference between COBOL and these other languages lies in how data is declared.

  This chapter explores the different categories of data used in COBOL. It demonstrates how you can create and use items of each category. Because COBOL’s approach to data declaration affects assignment in COBOL, that topic is also examined in this chapter.

  Categories of Program Data

  COBOL programs basically use three categories of data:

  • Literals

  • Figurative constants

  • Data items (variables)

  Unlike other programming languages, COBOL does not support user-defined constants.

  COBOL Literals

  A literal is a data item that consists only of the data item value itself. It cannot be referred to by a name. By definition, literals are constants in that they cannot be assigned a different value.

  There are two types of literal:

  • Alphanumeric (text/string) literals

  • Numeric literals

  Alphanumeric Literals

  Alphanumeric literals are enclosed in quotes and consist of alphanumeric characters. Here are some examples:

  "William Murphy", "1528", "-1528", "1528.95"

  37

  Chapter 3 ■ Data DeClaration in CoBol

  ■ Note enclosing the text in quotes defines the item as an alphanumeric literal even though the literal value may be entirely numeric.

  Numeric Literals

  Numeric literals may consist of numerals, the decimal point, and the plus or minus sign. Numeric literals are not enclosed in quotes. Here are some examples:

  1528, 1528.95, -1528, +1528

  Data Items (Variables)

  A data item can be defined as a named location in memory in which a program can store a data value and from which it can retrieve the stored value. A data name, or identifier, is the name used to identify the area of memory reserved for a data item.

  In addition to the data name, a data item must also be described in terms of its basic type (alphabetic,

  alphanumeric, numeric) and its size. Every data item used in a COBOL program must have a description in the DATA DIVISION.

  Data Type Enforcement

  Languages such as Pascal, Java, and C# may be described as strongly typed languages. In these languages, there are a number of different data types, and the distinction between them is rigorously enforced by the compiler. For instance, if a variable is defined as a float, the compiler will reject a statement that attempts to assign a character value to that variable.

  In COBOL, there are only three types of data: numeric, alphanumeric (text/string), and alphabetic. The

  distinction between these data types is only weakly enforced by the compiler. In COBOL, it is possible to assign a non-numeric value to a data item that has been declared to be numeric.

  The problem with this lax approach to data typing is that COBOL programs crash (halt unexpectedly) if they attempt to do computations on numeric data items that contain non-numeric data. In COBOL, therefore, it is the responsibility of the programmer to ensure that non-numeric data is never assigned to a numeric data item intended for use in a calculation. Programmers who use strongly typed languages do not need to have this level of discipline because the compiler ensures that a variable of a particular type can only be assigned an appropriate value.

  ■ Bug Alert attempting to perform computations on numeric data items that contain non-numeric data is a frequent cause of program crashes for beginning CoBol programmers. this can easily happen if the data item has not been initialized to a valid starting value.

  Figurative Constants

  Unlike most other programming languages, COBOL does not provide a mechanism for creating user-defined, named constants. This is a serious deficiency. Named constants make a program more readable and more maintainable.

  38

  Chapter 3 ■ Data DeClaration in CoBol

  For instance, although a literal value of .06 (representing the current sales tax rate of 6%) could be used throughout a program whenever the sales tax rate was required, the program would be more readable if the value was assigned to the named constant SalesTaxRate. Similarly, using a named constant would make maintenance easier.

  If the actual sales tax rate changed, only the constant definition would have to be updated instead of all the places in the program where the literal sales tax rate value was used.

  In COBOL, a data item can be assigned a value, but there is no way to ensure that, somewhere in the program, some maintenance programmer has not assigned a different value to the data item.

  ■ ISO 2002 although this book adheres to the anS 85 standard, you may be interested to know that this deficiency has been addressed in iSo 2002 CoBol standard by means of the CONSTANT clause entry.

  Code example:

  01 SalesTaxRate CONSTANT AS .06.

  Although COBOL does not allow user-defined named constants, it does have a set of special constants called figurative constants. Figurative constants are special constant values that may be used wherever it is legal to use a literal value. However, unlike a literal, when a figurative constant is assigned to a data item, it fills the entire item, overwriting everything in it. Figurative constants are often used to initialize data items. For instance, MOVE SPACES TO

  CustomerName fills the whole data item with spaces, and MOVE ZEROS TO FinalTotal fills that data item with zeros.

  Table 3-1 shows the COBOL figurative constants.

  Table 3-1. Figurative Constants

  Figurative Constant

  Behavior

  ZERO

  Behaves like one or more instances of the literal value 0. The constants ZERO, ZEROS, and

  ZEROS

  ZEROES are all synonyms. Whichever is used, the effect is exactly the same.

  ZEROES

  SPACE

  Behaves like one or more instances of the space character. SPACE and SPACES are

  SPACES

  synonyms.

  HIGH-VALUE

  Behaves like one or more instances of the character in the highest ordinal position in the

  HIGH-VALUES

  current collating sequence (usually the ASCII character set).

  HIGH-VALUE and HIGH-VALUES are synonyms.

  LOW-VALUE

  Behaves like one or more instances of the character in the lowest ordinal position in the

  LOW-VALUES

  current collating sequence (the null character [hex 00] in the ASCII character set).

  LOW-VALUE and LOW-VALUES are synonyms.

  QUOTE

  Behaves like one or more instances of the quote character. However, it cannot be used to

  QUOTES

  bracket a non-numeric literal instead of the actual quote character. For instance, QUOTE

  Freddy QUOTE cannot be used in place of "Freddy".

  QUOTE and QUOTES are synonyms.

  ALL literal

  Allows an ordinary literal character to behave as if it were a figurative constant.

  39

  Chapter 3 ■ Data DeClaration in CoBol

  Elementary Data Items

  An elementary item is the equivalent of a variable in other languages. It is an atomic data item that is not further subdivided. The type and size of an elementary data item are the type and size specified in its PICTURE clause.

  In COBOL, an elementary data item declaration consists of a line of code containing the following

  mandatory items:

  • A level number

  • A data-name or identifier

  • A PICTURE clause

  The declaration may also take a n
umber of optional clauses. The most common optional clause is the VALUE

  clause, which assigns an initial, or starting, value to a data item.

  Elementary data items that are not a subdivision of a group item must use a level number of 01 or 77. This book uses 01 for these items. A discussion of the role of level 77s is reserved for later in this chapter.

  ■ Note a data item declaration may also take a number of other optional clauses such as USAGE, BLANK WHEN ZERO, and JUSTIFIED.

  Declaring Elementary Data Items

  In typed languages, the data type specified is important because the type determines the range of values that the item can store and governs the operations that can be applied to it. For instance, a Java int data item can store values between -2,147,483,648 and 2,147,483,647 and can only be used in operations that expect an operand of that, or a compatible, type. From the type of the item, the compiler can establish how much memory to set aside for storing its values.

  COBOL is not a typed language, so it employs a very different mechanism for describing its data items. COBOL

  uses what could be described as a “declaration by example” strategy. In effect, you provide the system with an example, or template, or picture of the size and type (alphabetic, numeric, alphanumeric) of the item. From this PICTURE clause, the compiler derives the information necessary to allocate the item.

  PICTURE Clause Symbols

  To create the required picture, you use a set of symbols. The most common symbols used in standard PICTURE clauses are shown in Table 3-2.

  40

  Chapter 3 ■ Data DeClaration in CoBol

  Table 3-2. Common Symbols Used in Standard PICTURE Clauses

  Symbol Meaning

  A

  Indicates an occurrence of any alphabetic character ( a to z plus blank) at the corresponding position in the picture:

 

‹ Prev