Custom Search

mainframe cobol matirial,programes,cobolcoding tips

The "Hello World" Program

The "Hello world" program has become almost trite. Writing a program that prints "Hello world" on-screen usually is the first program you learn in any language. Listing 1.1 is a basic COBOL program that will display "Hello world". The format of a COBOL program is covered in the following sections.

TYPE: Listing 1.1. "Hello world" in COBOL.

000100 IDENTIFICATION DIVISION.

000200 PROGRAM-ID. HELLO.

000300 ENVIRONMENT DIVISION.

000400 DATA DIVISION.

000500 PROCEDURE DIVISION.

000600

000700 PROGRAM-BEGIN.

000800 DISPLAY "Hello world".

000900

001000 PROGRAM-DONE.

001100 STOP RUN.

ANALYSIS: A COBOL program always contains four divisions. These four divisions always have the same names:

· IDENTIFICATION DIVISION

· ENVIRONMENT DIVISION

· DATA DIVISION

· PROCEDURE DIVISION

In Listing 1.1, lines 000100 and 000200 are the IDENTIFICATION DIVISION. This division is used to identify basic information about the program. In this example, the IDENTIFICATION DIVISION contains only the PROGRAM-ID, HELLO.

Line 000300 is the ENVIRONMENT DIVISION, which is used to identify the environment in which the program is running. Remember that COBOL is intended to run on many different types of machines, and this section is used to handle the differences between various computers. In this case, the program has no specialized machine requirements, so the ENVIRONMENT DIVISION is empty.

Line 000400 is the DATA DIVISION, which will contain any data that the program operates on. This program has no data, so the DATA DIVISION is empty.

Lines 000500 through 001100 are the PROCEDURE DIVISION. This is the meat of the program--the part that does the work intended by the programmer. The PROCEDURE DIVISION contains two paragraphs at line 000700 (PROGRAM-BEGIN) and line 001000 (PROGRAM-DONE). The term paragraph has a special definition in COBOL that will be covered a bit later in today's lesson. All the actual work in this program is done by line 000800.

If you have not yet installed your software, review Appendix C, "Editing, Compiling, and Linking," and the installation instructions in your software documentation; then complete the installation procedure. Make sure that you end up with a C> prompt under MS-DOS, and create your working directories as described in Appendix C. Change to your working directory before you begin editing hello.cbl.

It is extremely important that you type (edit), compile, and, if necessary, link this program to produce a running program. If you can't get this program to run, the remainder of this book will be an exercise in theory with no practical application. If you are using some other editor and COBOL combination, consult your local documentation or someone who is experienced with the system. Don't pass over this exercise without getting it to run. It is the simplest program to edit, compile, link, and run.

Before you start typing, you should note a couple of things. The first six character positions of each line are used for line numbering. The seventh character position is always blank. The commands at lines 000800 and 001100 begin at position 12. Everything else starts at position 8.

The editor that comes with Micro Focus Personal COBOL actually skips positions 1 through 7 and leaves the cursor positioned at column 8. If you are using this editor, use the left arrow key to move to column 1 to type the line numbers and the lines. Columns 1 through 7 are traditionally used for line numbering in COBOL. Most larger systems will use line numbers. The practice of skipping or not requiring line numbers is common with PC COBOL compilers and very modern compilers, but at the moment it is the exception rather than the rule. This book uses line numbers in keeping with the practice of most larger systems, and to provide a convenient way to refer to lines of code when a program is being analyzed in the text.

It's preferable to use spaces instead of the Tab key while editing, and you should break the habit of using the Tab key. Many personal computer-based COBOL compilers can handle tab characters, but the language was not designed originally to allow tabs in the source code file, and their presence can cause trouble on larger machines.

If you are using Micro Focus Personal COBOL, start your editor by typing the following line and pressing Enter:

pcobol hello.cbl

The extension .cbl is the default extension for many MS-DOS based COBOL compilers. On the VAX minicomputer manufactured by Digital Equipment Corporation, the default extension is .COB, and you would use a command such as:

EDIT HELLO.COB

DO/DON'T:
DO
type each line exactly as it appears in Listing 1.1. Review your work carefully.

DON'T make typing errors. Some of the typing errors that can cause serious problems when you are compiling include misspelling the name of a DIVISION (for example, INDENTIFICATION DIVISION), adding an unnecessary hyphen (for example, DATA-DIVISION), or omitting any of the periods. Note that everything ends with a period (.); in fact, line 000200 has two periods in it.

New Term: Checking source code created with an editor for typographical and other errors before compiling it is referred to as desk-checking.

New Term: COBOL compilers are prone to produce cascading errors. A cascading error is one or more errors (sometimes hundreds) generated by the compiler when the problem really is one simple error earlier in the program. Code containing a missing period can produce a stream of apparent errors on some compilers that can be traced back in the program to that single error. The missing period itself might not even be mentioned as an error by the compiler, but it causes later problems that do show up.

When you have completed your check, close and save the file. Under Micro Focus Personal COBOL, hold down the Alt key while pressing F4, and then release both keys and press Enter.

Now you are ready to compile your program. Under Micro Focus Personal COBOL, press F2 until check is displayed on the status line (the fifth line from the bottom of the screen) and press Enter.

New Term: Under Micro Focus Personal COBOL, the process of compiling is called checking. The Micro Focus Personal COBOL compiler actually is called the checker.

The program should compile with no errors or warnings. If there are errors or warnings, re-edit your source code file, hello.cbl, desk-check it (compare it to the example in the book), and locate the error. The compiler might tell you the line number at which the error occurred, and you can go straight to that line.

COBOL compilers are dependent on correct punctuation, so bad punctuation can sometimes confuse the compiler. If the compiler says you have an error on a particular line, but you can't seem to find it, look one or two lines earlier to check whether you left out a period or started a line in an incorrect column. You also might want to check Appendix D, "Handling Compiler Errors," to help you track down errors.

After the program compiles cleanly, you are ready to run it. Exit from the COBOL development environment, if you are in one. For Micro Focus Personal COBOL, press Esc. The bottom row of the screen will display the message "Exit from Personal COBOL." Press the Y key to exit.

To run the program, type cls and press Enter to clear the screen. Then type the following and press Enter:

pcobrun hello

The program runs and displays your "Hello World" text, along with some Micro Focus copyright information, as shown in the output that follows.

OUTPUT:

Personal COBOL version 2.0 from Micro Focus

PCOBRUN V2.0.02 Copyright (C) 1983-1993 Micro Focus Ltd.

Hello world

C:>

Congratulations, you've completed your first program. If you are not excited about this, try exclaiming "Wow! My first COBOL program!" a couple of times.

The output display is approximately what you should see on your screen under Microfocus Personal COBOL. Other versions of COBOL will produce different display arrangements and may or may not include copyright notices. The key point is that the message Hello world will be displayed on-screen.

If you did not get the expected results, do not despair. Go back to the beginning of this section and review all the work. Particularly review Appendix C and the installation instructions in your software documentation, and run the tests to make sure that your editor is installed correctly. Check all the spelling in the program and run it through the compiler (checker) again until there are no error messages.