Chapter 4 Assignment Page 2

Header record loops

A header record logic loop uses a header record to tell the computer system when it is time to exit the loop. A header record is the first record in an input file. The header record is set to a number. The number indicates the total number of records to process. The computer system will read the header record and stores the number in a variable in the computer’s memory. Each time the loop steps are performed, the header record value is decremented by 1 and compared to 0. Once the header record value equals 0, the loop is exited.

For example: This input file contains as the first record in the file, a header record set to 5. This indicates to the computer system that there are 5 input records that will follow the header record. The computer system will read the first record in file and place it in a memory location named Count. Each time the loop is performed, the Count (the header record value) is decremented by 1. Once the header record value equals 0, the loop is exited. NOTE: The problem below walks through an example of a header record loop that will simply read a record and print the output. This will allow you to focus on the loop and not processing the problem. Walk through the steps the computer will perform using a memory diagram.

Input File

  5 wpeC.gif (864 bytes)

wpe5.gif (1073 bytes)

Pseudocode Example:

wpeA.gif (1073 bytes)

This type of loop is used when the number of times to perform the loop is known. It is a preferred method to use if the different data files will be used and the data files will have a different amount of input records. With the header record method, the program statements will remain the same.
 
Exercises:

Problems 20 & 21 – You will redesign problems using a header record loop.