sams_teach_yourself_cobol_in_24_hours_-_hour_2_writing_your_first_program_in_cobol

Sams Teach Yourself COBOL in 24 Hours - Hour 2 Writing Your First Program in COBOL

Return to Teach Yourself COBOL in 24 Hours, COBOL bibliography, COBOL, COBOL DevOps, Awesome COBOL, Awesome IBM Mainframe, IBM Mainframe development, IBM Mainframe bibliography, Fortran

“ (TYCb24H 1998)

Hour 2 - Writing Your First Program in COBOL

In Hour 1, “Getting Started,” you learned about the history of COBOL. You also installed and tested the compiler. In this hour, you learn the basic layout of a COBOL program and write your first program. This hour covers the following basics:

• The divisions of a COBOL program

• How to key a simple program into the editor

Compiling, linking, and running your program

• What to do when the program won't compile

COBOL Program Layout The layout, or format, of a COBOL program follows certain simple rules, which originated long ago when programs were punched onto 80-column punch cards. With COBOL, columns 1–6 are reserved for line numbering. Line numbers are not mandatory, nor do they have to be in sequence.

However, you can imagine how important these line numbers were if someone accidentally dropped a deck of program cards on the floor!

Column 7 is the continuation or indicator area. When a line is to be continued from a previous line, a dash – in column 7 indicates the continued line. Column 7 can also contain either an asterisk (*) to indicate a comment line or a slash (/) to cause a page eject, or new page, when printing a listing of the program.

Image

A comment line contains any comments the programmer wants to put into the program. Commenting a program is important for many reasons. It helps other programmers, or even you, figure out what you are trying to accomplish with the programming statements, or code, that you have written. In addition to a – or *, most compilers support Debugging mode. In this mode, a D in column 7 means that the line is to be included only when the program is compiled in Debugging mode. When Debugging mode is not selected, these lines are treated as comment lines. The compiler ignores any other character that appears in column 7.

Image

Code, line, and programming statement are different names that mean the same thing. The actual programs you write in the COBOL language are considered source code. In other words, they are the main source that you wrote. Your programs must be translated for the computer, which is why they are called code. Writing a program is also referred to as coding. A programmer's job is to code a program.

Columns 8–11 are considered Area A. Area A contains Division, Section, and Paragraph headings. If other statements appear in Area A, the program may or may not compile, depending on your compiler. Having the Paragraph and Section headings appear in Area A creates a more readable program. The statements under these headings appear to be indented. Throughout the lessons, you will see this convention in action.

Area B extends from column 12 through column 72. Some COBOL compilers ignore this right margin. To be safe, you should limit your code to column 72. The main body of your program appears in Area B.

Image

Many modern compilers allow free-form coding. Free-format source, where the column numbers no longer matter, is being considered in the next COBOL standard, and many compiler vendors have implemented this option. However, a number of them have not. If you have source code that is free format, ignoring the limits of Area A and Area B, and you try to move this source to another compiler, the code might not compile. The safest practice is to follow the current standard and keep your code in Area B within columns 12–72.

Columns 73–80 are for program identification]]. When programs were on punch cards, the program name would typically appear here. This book ignores these columns.

In Hour 1, I compared a COBOL program to a recipe. COBOL programs are broken into four divisions. Like a recipe, the first sections contain the ingredients and the last section, the preparation instructions. Each division is further broken into paragraphs. Each division is explained further in the sections that follow. The four divisions of a COBOL program are

Identification Division

Environment Division

Data Division

Procedure Division

Image

The only required division is the Identification Division; the others are optional. If you don't have anything to put under them, you may omit them. However, I suggest that you at least include the division headers, just in case you need to add something later.

Identification Division The Identification Division identifies the program to the compiler. In the current defined standard for the COBOL language, the Identification Division consists of one paragraph: the Program-Id. The Program-Id contains the name of the program. This name is very important, as it controls the ultimate name of the program during execution. Any references the operating system makes to the program depend on this name. In Hour 23, “The Call Interface,” you learn about COBOL programs calling and being called by other programs. The Program-Id is the name that is used when COBOL programs are called. When looking at older COBOL programs, you might see other paragraphs under the Identification Division. Although these paragraphs are accepted by the current COBOL standard, they are slated for removal in the next. The Identification Division is coded as follows:

000001 Identification Division. 000002 Program-Id. NameOfProgram. Image

Line numbers are indeed optional. In this book, however, they are included in all examples for later reference in the text.

Image

The names of the divisions, paragraphs, and statements in COBOL are not case sensitive. NameOfProgram is exactly the same as NAMEOFPROGRAM and nameofprogram. Note that each line, or sentence, in the code ends with a period.

Prior to the 1985 COBOL standard, COBOL was case sensitive. All COBOL had to be coded with uppercase letters. If you look at older COBOL programs, you are likely to observe this type of coding.

Environment Division The Environment Division contains information relating to the computer on which the program will run. The Environment Division consists of sections and paragraphs under those sections. The sections in the Environment Division are

Configuration Section

Input-Output Section

The Configuration Section contains three paragraphs. The first paragraph concerns the type of computer on which the program is being compiled, that is, the Source-Computer. The compiler vendor for the environment in which you are running defines the name of the computer. The programs in this book use IBM-PC. The Source-Computer Paragraph has one clause, and it is optional. This clause is the With Debugging Mode clause. Including this clause activates the lines of code with a D in the indicator column (column 7) the next time the program is compiled. The word With is optional on the With Debugging Mode clause.

The Object-Computer Paragraph describes the computer on which the program is designed to run. Rarely will you be compiling on one computer type and running on another. Again, use IBM-PC for the Object-Computer Paragraph. Only one clause to the Object-Computer Paragraph is relevant in normal programming, and that is the Program Collating Sequence is clause. The Program Collating Sequence is clause describes the order of the characters for the program. When this clause is omitted, the collating sequence defaults to the collating sequence native for the computer on which the program runs. The programs in this book do not need to code the Program Collating Sequence is clause.

Image

Collating sequence is very important. Even when you use the native collating sequence of the computer, you need to understand it. Another way to think of collating sequence is as a sort sequence, or alphabetic sequence. You know that ABCDEFGHIJKLMNOPQRSTUVXYZ is the proper sequence of alphabetic letters. You know that E is greater than A, and the Z is the highest letter of all. This order describes the alphabet's collating sequence. The character set used by personal computers is ASCII. Each character is assigned a number in ASCII. The ASCII code for the letter A is 65, and the code for the letter E is 69. Therefore, the letter A comes before the letter E in the ASCII collating sequence. The native alphabet and collating sequence is ASCII. For other computers, the alphabet is different. In some cases, programmers working on multiple computers with different alphabets might want to use the native alphabet, but collate on a specific machine's alphabet. That is the purpose of the collating sequence is clause. In typical COBOL programming, the clause is rarely used.

The Special-Names Paragraph can contain numerous clauses. For the most part, this flexibility enables you to program for specific items that are provided either by the compiler being used or by the computer on which the program runs. The command line from the execution of the program is one of the items that Fujitsu COBOL lets you retrieve via a special name. Controlling the cursor position and determining which function keys are pressed are tasks that are accomplished using Special-Names. These tasks are discussed in more detail in Hour 4, “Basic User Interface,” and Hour 6, “Manipulating Data.”

Two useful clauses are Currency-Sign is and Decimal-point is Comma. They do exactly what they appear they do. With the Currency-Sign is clause, you can specify the symbol to be used for currency, and with the Decimal-point is Comma clause, you can use a comma instead of the decimal point to indicate decimal positions. A typical Configuration Section follows.

000001 Identification Division. 000002 Program-Id. NameOfProgram. 000003 Environment Division. 000004 Configuration Section. 000005 Source-Computer. IBM-PC With Debugging Mode. 000006 Object-Computer. IBM-PC. 000007 Special-Names. 000008 Currency-sign is $. Notice the With Debugging Mode clause on the Source-Computer line (line 0005). This clause activates any lines in the program that have a D in column 7. When With Debugging Mode is specified, the compiler uses these marked lines as if they were regular source code entries. Its use here is just to show you how it is turned on. This book does not contain any programs that use Debugging mode.

The Input-Output Section contains two paragraphs: File-Control and I-O Control.File-Control describes the use of data files in the COBOL program and is covered in depth throughout Part 2, “File Handling,” and Part 3, “Business Processing.” I-O Control describes the behavior and internal handling of some of the input and output with the associated files. The I-O Control is not often used.

An example of a typical COBOL program Input-Output Section follows.

000001 Identification Division. 000002 Program-Id. NameOfProgram. 000003 Environment Division. 000004 Configuration Section. 000005 Source-Computer. IBM-PC. 000006 Object-Computer. IBM-PC. 000007 Input-Output Section. 000008 File-Control. 000009 Select Input-File assign to “IN.DAT”.

Data Division The Data Division describes the data used by the program. The data can come from input sources such as disk files or from intermediate data fields and working areas in storage. The Data Division is broken into the following sections:

File Section

Working-Storage Section

Linkage Section

Communications Section

Report Section

Screen Section

Each section has fairly detailed entries and is discussed in depth in the appropriate hours, with the exception of the Communications Section and the Report Section. The Report Section is used by a module of COBOL that is optional in the COBOL standard, called Report Writer. The Report Writer is not included in many COBOL implementations and is not included in this book. The Communications Section is used by another optional module, the Communications Facility, and it too is included here only in the interest of presenting a complete picture. Its usage is not discussed.

The File Section describes the files being used by the COBOL program. The entries under the File Section include file descriptions for regular input files, and sort descriptions for sort work files. Sort work files are temporary files used by the sort process within a COBOL program. Sorting is discussed in depth in Hour 17, “Sorting.” One of COBOL's strengths is that it describes the contents of each file in great detail.

The Working-Storage Section describes data areas to be used by the program during its processing. Like the File Section, data areas are described in great detail. All data items referenced by the program are declared in one of the sections of the Data Division.

The Linkage Section passes data between programs.

The Screen Section describes a screen full of input, output, and update data for the user interface. In this book, the Screen Section communicates directly with the users of the programs. Each item is carefully and explicitly defined.

Following is a sample of a typical Data Division in a COBOL program:

000001 Identification Division. 000002 Program-Id. NameOfProgram. 000003 Environment Division. 000004 Configuration Section. 000005 Source-Computer. IBM-PC. 000006 Object-Computer. IBM-PC. 000007 Input-Output Section. 000008 File-Control. 000009 Select Input-File assign to ”IN.DAT”. 000010 Data Division. 000011 File Section. 000012 FD Input-file. 000013 01 Input-Record Pic X(100). 000014 Working-Storage Section. 000015 01 Work-Field Pic X(20).

Procedure Division The Procedure Division is where the program's processing occurs. In the Procedure Division, you tell the program how to assemble and use the ingredients you specified in the other divisions. The Procedure Division is made up of Sections and Paragraphs. Sections may be omitted if they are not required. For the most part, you will have no need to program any Sections in your Procedure Division. However, if you do, please remember that each Section entry must be followed by a Paragraph name. The use of Paragraphs and Sections is discussed in detail in Hour 5.

The Procedure Division must contain at least one Paragraph. The Paragraph name begins in Area A starting in column 8. With COBOL, all data, paragraph, and section names may be up to 30 characters long. You may use any convention you desire. Most COBOL programmers use the convention of separating words within names by dashes. For example: Read-The-File, could be a paragraph name, as could ReadTheFile. The dashed separated words are easier to read and understand.

Programming statements, or sentences, that appear under paragraph headings begin in Area B. Most of this book discusses areas of the Procedure Division.

Creating a Simple COBOL Program Now is the time to put all these pieces together and write your first COBOL program. What would a programming book be without a Hello World program? This first program displays “Hello World” on the screen and then ends; it uses the Display statement.

The Display statement outputs data to an output device. Normally, this device is a CRT (monitor) or printer. The Display statement may use the Upon phrase to specify the device on which the display is to occur. If the Upon phrase is omitted, the default device, as defined by the specific compiler, is used. On IBM mainframes, this device is the printer. With the Fujitsu compiler, it is the console, which is your monitor. The name specified in the Upon phrase can also be a device name specified in the Special-Names clause. An example of the Display statement is

000100 Display ”Hello World” Upon Console. Image

Console is the main operator console. For programming on a PC, it is the regular PC's display. Console is a COBOL reserved word. Any word that makes up the COBOL programming language, or is used for a special extension or enhancement to the language, is considered a reserved word. A reserved word cannot be used as a variable or data item name in your COBOL program. A list of reserved words is available in Appendix A of the Fujitsu COBOL language reference that is on the CD-ROM.

In addition to the Display statement, you need some way to tell the program to end. This is done with a Stop Run statement. The Stop Run behaves just as it sounds. When it is encountered, the program stops running. If you fail to code a Stop Run statement, most compilers insert it for you. However, it is good practice to always code the Stop Run statement where you want your program to stop.

Image

If you are using a compiler other than the Fujitsu COBOL compiler that comes on the CD-ROM, you will need to familiarize yourself with the methods for editing, compiling, and linking your programs with that compiler. Compiler directives may be different when using other compilers, and the procedures for compiling, linking, and running your programs will probably be different.

Image

Before you start the editor and enter the lines of code for the program, you need to understand one more item. In addition to regular COBOL statements, the compiler may have to deal with compiler directives. Different COBOL compilers understand different compiler directives. Compiler directives tell the compiler how to behave when compiling this particular program. They can be used to make the process of compiling and linking your program much easier. When you compile your program, you have to tell the provided Fujitsu compiler whether your program is a Main program or a sub-program. For most of the examples and exercises in this book, the programs are Main programs. You indicate to the compiler that your program is a Main program by entering @OPTIONS MAIN on the first line of the program, before the Identification Division.

You should create a new folder on your computer to hold your source code. There are two easy ways to create this folder. I suggest you call it \TYCOBOL, which is the name used in this book.

One method to create the folder follows.

1. Click the Start button.

2. Select Programs.

3. Click the MS-DOS prompt icon.

4. At the prompt, type MD\TYCOBOL and press Enter.

Another method:

1. Double-click the My Computer icon.

2. Double-click the drive where you want to create the folder.

3. Click the File menu.

4. Select New.

5. Click Folder.

6. The cursor will on the new folder name. Change that name to TYCOBOL and press Enter.

7. Close the open windows.

The following procedure is used to create the TYCOBOL folder under Windows 3.1.

1. Open the Main program group by double-clicking its icon.

2. Open File Manager by double-clicking its icon.

3. Select the File menu option.

4. Choose the Create Directory option.

5. For the Name, type \TYCOBOL

The \ is very important. If you forget it, the TYCOBOL directory is not created under your root directory and may be hard to find.

6. Click the OK button to create the directory.

7. Close File Manager by double-clicking the upper-left corner of the File Manager window.

Start the Fujitsu COBOL development environment, Programming Staff.

1. Click the Start button.

2. Select Programs.

3. Select Fujitsu Cobol 3.0.

4. Click Programming Staff.

Use the following steps under Windows 3.1 to start the Programming Staff development environment.

1. Open the Fujitsu COBOL Family V2 group by double-clicking its icon.

2. Double-click the Programming Staff 16 to start Programming Staff.

Now you need to create your new program.

1. Select the File menu.

2. Click New.

3. When the Editor window appears, again select the File menu.

4. Click New.

5. When the New dialog box appears, use the selection box to change the extension to COB and click the OK button.

The window shown in Figure 2.1 should now be displayed.

Figure 2.1 The new Editor window.

Image

Notice that the cursor is in column 7 of the first line. Fujitsu inserts a space between column 6 and column 7 to separate the line numbers from your programming code. The space does not take up a character position. Before you enter any lines of code, you should change some of the editor settings. Normally, the editor numbers the lines in increments of 100. This convention is from the days when programs were on cards and programmers left a gap in the numbers so that cards could be inserted later without having to renumber the entire deck. The compiler reports errors by their relative line number, so to make finding these errors in your source easier, you should use the Relative line-numbering option. In addition, the compiler will color COBOL reserved words for you. This feature will help you tremendously as you start out programming in COBOL. However, by default, the compiler colors only words that are in all uppercase characters. You should change that option by deselecting the Match Case of Keyword check box. To do so, as well to change the line numbering to Relative, perform the following steps:

1. Select the View menu option.

2. Click Display Format.

3. Select the Relative radio button next to Line Number Type.

4. Deselect the Match Case of Keyword check box, making sure it is not checked.

5. Click the Save Setting check box.

6. Press the OK button.

Image

If you are using the 16-bit version of the compiler, for Windows 3.1, you will not have the options for Match Case and coloring the source code. Simply change the Relative radio button, select the Save Setting check box, and click OK.

You are ready to start entering your program. Start by typing in the necessary compiler option @OPTIONS MAIN. Make sure that you start in Area A, and that the phrase @OPTIONS MAIN is all in upper case. Do not terminate the line with a period. Line numbers are inserted automatically. Press the Enter key to advance to the next line. Next, type in the Identification Division. Be sure to start at the beginning of Area A (column 8).

On the next line, type the Program-Id Paragraph, again making sure to start in Area A. Immediately after Program-Id, on the same line type the name of your program. Call this one Hello. If you are typing the names correctly, Identification Division and Program-Id will be blue and the rest of the text will be black.

Next, type in the Environment Division and Configuration Section lines. You need to tell the compiler what type of computer will do the compiling and running. Don't forget to end each line with a period. Next type in the Source-Computer and Object-Computer Paragraphs. After each of these, on the same line, type IBM-PC. Be sure to put a period between Source-Computer and IBM-PC.

This program doesn't need anything further in the Environment Division. Next, enter the Data Division. Again, this program does not need anything under the Data Division, so go ahead and enter the Procedure Division.

The Procedure Division is where you tell the program what you want it to do. Every program must have at least one paragraph under the Procedure Division. Title the paragraph Hello-Start. Begin the paragraph title in Area A and make sure to end the title with a period.

The next step is to enter an actual statement telling the program what to do. Start the statement in Area B (column 12). Type Display “Hello World”., making sure to enclose the words Hello World in quotation]] marks.

Image

The Fujitsu editor will help you find Areas A and B. At the bottom of the Editing window, the editor displays the line and column number. Remember that Area A begins in column 8 and Area B begins in column 12. (This feature is not available in the 16-bit Windows 3.1 version.)

Finally, on the next line, in Area B, enter the Stop Run statement telling the program to stop execution.

Save your program into the \TYCOBOL directory by selecting the File menu and the Save As option. Change the filename to Hello.Cob. Use the down arrow next to the Save In box to find and select the \TYCOBOL folder; then click the Save button.

Your program should appear exactly as illustrated in Figure 2.2. Compare your program with the figure and correct any obvious differences.

Figure 2.2 The Hello.Cob program.

Image

Compiling and Linking It's time to compile your program, but before you do, you need to understand the compile process. The compiler is a program that analyzes your source code, which is the program you just entered, performing several functions. The compiler checks your program for syntax errors. A syntax error occurs when the statement entered does not follow the defined rules for the language. The compiler checks for required elements, such as the Identification Division and Program-Id. It checks to make sure that all of your Division headings, Sections, and Paragraphs start in the proper columns. It checks for dependencies, areas where you must define something before you can reference it later. Some basic logic errors are also checked. For example, if you define a file to your program, but never open it, the compiler issues a warning. If your program analyzes correctly, the compiler creates an object module by translating your source code into machine code. The object module contains all the instructions necessary for the computer to run your program. However, the machine addresses for these instructions are not yet assigned—that is the job of the linkage editor or linker.

To compile your program, follow these steps:

1. Click the X in the upper-right corner of the editor window to close the window. (In Windows 3.1, double-click the upper-left corner.)

2. Choose the Tools menu option. (In Windows 3.1, choose Utilities.)

3. Click the WINCOBCompile menu item.

4. Click the Browse button.

5. Change the Look In to the \TYCOBOL folder. (In Windows 3.1, change to Directories.)

6. Select Hello.cob and click Open. (In Windows 3.1, click OK.)

7. Click the Compile button. A countdown clock appears.

If your compilation was not successful, the countdown clock changes momentarily to an exclamation point. If successful, the countdown clock counts down to 1 and then shows the word End. After the compile, an Edit window with the compile results is displayed. If the compile is successful, the window should say:

STATISTICS: HIGHEST SEVERITY CODE=I, PROGRAM UNIT=1 If it says anything else, skip down to the section “When It Won't Compile.”

After your program is compiled, it must be linked. The link edit process assigns the actual internal addressing to the compiler-generated object. In addition, the linker adds any supporting machine code necessary to run your program.

Image

Do not attempt to link your program if the compile was not successful; there will be no object file to link.

Follow these steps to link your program:

1. Close the edit window by clicking the top-right X. (For Windows 3.1, double-click in the upper-left corner.)

2. Close the WINCOB dialog box by clicking the top-right X. (For Windows 3.1, choose the Exit menu option.)

3. Choose the Tools menu option. (For Windows 3.1, choose Utilities.)

4. Select the WINLINKLink menu selection.

5. Click the Browse button.

6. Select the Hello.Obj file and click the Open button. (For Windows 3.1, click OK.)

7. Click the Add button. C:\TYCOBOL\HELLO.EXE appears in the Target field.

8. Click the Link button. (For Windows 3.1, click the Build button.)

9. When the link finishes, close the window.

10. Close the WINLINK window.

Now you are ready to run your first COBOL program! Follow these steps:

1. Choose the Tools menu option again. (For Windows 3.1, choose Utilities.)

2. Select the WINEXECExecute menu item.

3. Click the Browse button.

4. Select Hello (some settings cause Hello.Exe to show) and click Open. (For Windows 3.1, click OK.)

5. Click the Execute button.

6. When the Runtime Environment Setup appears, click OK. (For Windows 3.1, select Run.)

7. Your program will run and display “Hello World”! A message tells you that the Console window is closed. Click OK, and the display window closes. Close the WINEXEC window.

Your screen should look like Figure 2.3.

Figure 2.3 The output from the Hello program.

Image

Image

It might appear that you have to run your programs from within the development system. That is not the case. The WINEXEC tool is there for convenience. You could also run the program by using the Start button, selecting Run, and then entering \TYCOBOL\Hello.Exe.

When It Won't Compile If your program does not compile, you will have to determine the reason by using the compiler diagnostic messages that are displayed. Don't let a large number of compiler error messages discourage you. Sometimes even a single error early on can cascade down through the program. For example, if you don't capitalize @OPTIONS MAIN, you will see the following errors:

** DIAGNOSTIC MESSAGE

  • (NOPRGMID)

C:\TYCOBOL\hello.cob 0: JMN1102I-S IDENTIFICATION DIVISION HEADER IS MISSING. HEADER ASSUMED TO BE CODED. C:\TYCOBOL\hello.cob 1: JMN1000I-S CHARACTER EXCLUDED FROM COBOL CHARACTER SET IS USED. THAT CHARACTER IS IGNORED. C:\TYCOBOL\hello.cob 1: JMN1005I-W CHARACTER STRINGOPTIONS’ MUST START IN AREA B. ASSUMED TO START IN AREA B. C:\TYCOBOL\hello.cob 1: JMN1356I-W INVALID WORDOPTIONS’ IS SPECIFIED IN IDENTIFICATION DIVISION. IGNORED UNTIL NEXT PARAGRAPH OR DIVISION. C:\TYCOBOL\hello.cob 2: JMN1104I-S PROGRAM-ID PARAGRAPH IS MISSING. PROGRAM-NAME GENERATED BY SYSTEM. C:\TYCOBOL\hello.cob 5: JMN1113I-S CONFIGURATION SECTION CANNOT BE SPECIFIED IN INTERNAL PROGRAM. C:\TYCOBOL\hello.cob 6: JMN1113I-S CONFIGURATION SECTION CANNOT BE SPECIFIED IN INTERNAL PROGRAM. C:\TYCOBOL\hello.cob 7: JMN1113I-S CONFIGURATION SECTION CANNOT BE SPECIFIED IN INTERNAL PROGRAM. C:\TYCOBOL\hello.cob 12: JMN1044I-S PROGRAM CONTAINED WITHIN PROGRAM ‘NOPRGMID’ MUST END WITH END PROGRAM HEADER. END PROGRAM HEADER ASSUMED. STATISTICS: HIGHEST SEVERITY CODE=S, PROGRAM UNIT=1 The reason for so many errors is that the compiler expects either valid compiler options or the Identification Division to appear on the first line of the program. The numbers immediately after the name of the file are the line numbers where the compiler found errors. After these are some error message numbers and information that is specific to the compiler being used. If you use a different compiler, you might see different error messages.

Image

If you place the cursor on the first line that is in error, in the first column, and press F11, the editor loads your program and sets the cursor on the first line in error.

In this small program, only a limited number of things could go wrong. Check to see whether you put dashes between the words where they are required; for example, Program-Id. Make sure you haven't inadvertently mistaken column 7 for Area A (column 8). Check to make sure you included the necessary division headers. Make sure you enclosed Hello World in quotation]] marks. Make sure that the word Display began in Area B. Make sure that you included a period after the divisions, sections, and paragraph headings. Make sure you have a period after your Stop Run statement.

Correct your problems, comparing your program to Figure 2.1 if necessary, and compile it again. Once you get a clean compile, link the program and run it! Don't be the least bit upset; fixing these problems is all part of being a COBOL programmer!

Image

When correcting your errors and recompiling your program, make sure to close all Edit windows before compiling the program again. The compiler will not be allowed to open the program or create the error message file properly if an old one is still open on your desktop.

Debugging Your Program Sometimes your programs don't behave the way you think they should. A “broken” program usually has a bug in it. A bug is something wrong with the program's logic. The only bug that could really appear in the first program is if “Hello World” does not display when you run the program. Perhaps you forgot to put in the Display statement.

Perhaps, instead of displaying “Hello World,” the program displayed “Hello Wrld.” Both of these are examples of bugs. When a bug appears, you need to edit your program and correct the bug. After fixing your source code, you can't just run the program again and expect to have the change in effect. If you change anything in your source code, you have to recompile and relink your program. Hour 11, “Advanced Perform Statements,” discusses more advanced debugging procedures.

Summary In this hour, you learned the following:

• The general layout of a COBOL program, including the different divisions and the purpose of each

• What a compiler is and how it works

• How to use the editor

• How to compile, link, and run a program

• How to correct problems in your programs if they won't compile or run properly

Q&A Q How many divisions make up a COBOL program, and what are they called?

A A COBOL program has four divisions: Identification, Environment, Data, and Procedure.

Q What is the minimum item that the Procedure Division must contain?

A At least one paragraph heading.

Q What is the purpose of the compiler?

A The compiler checks your program for COBOL language syntax errors, missing or extra items, and basic logic errors. If everything passes the edit, then the compiler creates the basic machine code that is linked, using the link edit program, or linker, to create your actually running program.

Q Do all programs work correctly the first time?

A Of course not! According to an old programmers' superstition, any program that compiles without errors the first time must have a bug! The compiler is designed to catch these coding errors and allow you to fix them.

Workshop To help reinforce your understanding of the material presented in this hour, refer to the sectionQuiz 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.

[[Fair Use]] [[Source]]s

COBOL: COBOL Fundamentals, COBOL Inventor - COBOL Language Designer: 1959 by Howard Bromberg, Norman Discount, Vernon Reeves, Jean E. Sammet, William Selden, Gertrude Tierney, with indirect influence from Grace Hopper, CODASYL, ANSI COBOL, ISO/IEC COBOL; Modern COBOL - Legacy COBOL, IBM COBOL, COBOL keywords, COBOL data structures - COBOL algorithms, COBOL syntax, Visual COBOL, COBOL on Windows, COBOL on Linux, COBOL on UNIX, COBOL on macOS, Mainframe COBOL, IBM i COBOL, IBM Mainframe DevOps, COBOL Standards, COBOL Paradigms (Imperative COBOL, Procedural COBOL, Object-Oriented COBOL - COBOL OOP, Functional COBOL), COBOL syntax, COBOL installation, COBOL containerization, COBOL configuration, COBOL compilers, COBOL IDEs, COBOL development tools, COBOL DevOps - COBOL SRE, COBOL data science - COBOL DataOps, COBOL machine learning, COBOL deep learning, COBOL concurrency, COBOL history, COBOL bibliography, COBOL glossary, COBOL topics, COBOL courses, COBOL Standard Library, COBOL libraries, COBOL frameworks, COBOL research, Grace Hopper, COBOL GitHub, Written in COBOL, COBOL popularity, COBOL Awesome list, COBOL Versions. (navbar_cobol)


© 1994 - 2024 Cloud Monk Losang Jinpa or Fair Use. Disclaimers

SYI LU SENG E MU CHYWE YE. NAN. WEI LA YE. WEI LA YE. SA WA HE.


sams_teach_yourself_cobol_in_24_hours_-_hour_2_writing_your_first_program_in_cobol.txt · Last modified: 2024/04/28 03:37 by 127.0.0.1