Mainframes 360
The one stop destination for System Z professionals

Friday, January 1, 2010

Program Flow Control


Q. What does one mean by ‘Program Flow Control’? Why is it important?
COBOL allows programmers to write computer programs in a systematic manner. You can divide and organize your programs into sections and paragraphs. Each section or paragraph is dedicated to perform a specific task or function. While writing a particular section or paragraph, you can focus and put all your energy, towards the task you want to perform, without bothering about the rest of the program.

When the control first enters into the Program, depending upon what task is to be performed, you accordingly just direct/transfer the control to the appropriate section or paragraph, jump to the relevant para. This idea is called Program Flow Control.

You have written the program, in a well-organised manner. If an error occurs in particular task/functionality, you only need to search and rectify the error in that section or paragraph corresponding to this functionality(You don’t need to search the whole program).
Q. How do you write SECTION’s and PARAGRAPH’s in COBOL?
Just as DIVISION’s start in Area A(Columns 8-11), Section names and Paragraph names should begin in Area A. Every Section name and paragraph name should end with a period.

Moreover, it is customary to follow a naming-convention for paragraph-names. Generally, the naming convention for COBOL Para’s used by most programmers(in different shops) is to prefix the paragraph name with a number. For example, if your program has 2 paragraphs INDIA and CHINA, then name them as 0100-INDIA and 0200-CHINA. This way, it becomes easier to identify them.

Given below is simple example of a typical COBOL Program that is divided into two sections READ-FILE-SECTION and WRITE-FILE-SECTION, each of them containing one paragraph.

Example:

Image52[1]

Think what if, you executed this COBOL Program. The starting point of the program is the PROCEDURE DIVISION. Each statement is executed by the computer one-by-one sequentially. It is terminated at STOP RUN. Thus, the output on the screen(SYSOUT Dataset) should be

Image53[1]
Q. What is PERFORM Statement used for? How does it control the program flow?
One of the statements in COBOL, to control the flow of the program is PERFORM Statement. The PERFORM Statement basically transfers control(jumps) to the specified paragraph or section.

The general syntax of PERFORM Statement is :
PERFORM <procedure-name>

The procedure-name refers to the name of the paragraph or section that, you want jump to.

Example:
Consider the following COBOL Program, that consists of a 0100-MAIN-PARA and 0200-INDIA-PARA.

Image50[1]

The starting point is PROCEDURE DIVISION.
1. The DISPLAY ‘START OF PROGRAM’ statement causes START OF PROGRAM to be displayed.
2. PERFORM 0200-INDIA-PARA causes transfers the control(jumps) to the 0200-INDIA-PARA. All the statements in this paragraph are executed one-by-one. DISPLAY ‘INDIA’ prints INDIA to the output. After the 0200-INDIA-PARA is complete, the control returns back to the point(0100-MAIN-PARA) from where it had left off.
3. The next statement DISPLAY ‘END OF PROGRAM’ causes END OF PROGRAM to be printed to the output. The program terminates at STOP RUN.

Upon running the above COBOL Program, you should get the following output -

Image51[2]

The procedure to which the PERFORM statement transfers control, can be a 1 single para, or multi-paragraph procedure.

The general syntax to perform a multi-paragraph procedure is:
PERFORM paragraph-A THRU paragraph-Z

Suppose you have written a sequence of paragraphs : PARAGRAPH-A, PARAGRAPH-B, PARAGRAPH-C,...,upto PARAGRAPH-Z. The above PERFORM Statement transfers control(jumps) to the starting point – PARAGRAPH-A, then all the intermediate paragraphs B,C,D,...,Y between A to Z are also executed. Then, the last paragraph PARAGRAPH-Z is executed, before the control returns back to the point from where it had left off.

Example:

Image54[1]

Upon running the above COBOL Program, you should get the following output -

Image55[1]

The PERFORM statement can also be used to execute a complete SECTION consisting of several paragraphs.

Example:

Image56[2] 
Upon executing the above COBOL Code, you should get the following output -

Image57[1] 
Q. Is it possible to execute a procedure repetitively over and over again? Can PERFORM statement be used to loop through a procedure?
PERFORM Statement can be used to execute a procedure(set of paragraphs) 1 time, 2 times, 3 times, and so on.. repetitively. Performing the same task over and over again, can be done using PERFORM Statement.

When you write a PERFORM Statement to loop/repeat a task again and again, you need to specify, how many times you want to perform the task. Now, specifying how many times, you want to do something – once, twice,thrice, 5 times, 10 times... is sometimes difficult.

For example, assume that you want to read data from a file.

Read 1st record
Read 2nd record
Read 3rd record
...
Read last record

To Read a record is the task to be performed again and again. So, you can write a PERFORM Statement to do the task - READ a record, again and again till, you reach the end of the file(all the data-records have been read).

PERFORM as (i=1,2,3,..,??) 
  READ ith record

How many read’s would you do? You don’t know this upper-bound, as you don’t know the exact no. of records in the file before-hand. Instead, you could type a boundary-condition, saying that, Stop reading when you reach End-of-file.

Thus, you can specify exactly how many no. of times to perform a loop, or you may specify a boundary(termination) condition.

1. To specify the no. of times, you want to perform a loop, you use TIMES option.
2. To specify a boundary(termination) condition for a loop, you use UNTIL option.

The general syntax for the PERFORM Statement used to do a task again and again many times is,

PERFORM <procedure-name> <n> TIMES

Example-

Image58[1]

Upon executing the above COBOL Code, you should get the following output -

Image59[2]

To many people who are thrown to work at a mainframe computer on their first job, they feel lost. Mainframe people seem to speak a completely different language and that doesn't make life easy. What's more, the books and manuals are incredibly hard to comprehend.

"What on earth is a Mainframe?" is an absolute beginner's guide to mainframe computers. We'll introduce you to the hardware and peripherals. We'll talk about the operating system, the software installed on a mainframe. We'll also talk about the different people who work on a mainframe. In a nutshell, we'll de-mystify the mainframe.

Readers based in India, can buy the e-book for Rs. 50 only or the print book. International readers based in the US and other countries can click here to purchase the e-book.