Control-Break Processing
In control-break processing, we have an input file which has been sorted on several key fields. We can think of the group as occurring in large groups of records, each group having the same value of the first key. Within each large group, the records occur in smaller groups according to the value of the second key. This pattern continues, depending on the number of keys.
We are interested in statistics (sum, maximum, minimum, etc.) for the file as a whole, for each large group, for each small group within each large group, etc.
Pseudocode
Main routine:
Zero grand totals
Read first record
Perform Process-One-Large-Group
until end of file
Print grand totals
Process-One-Large-Group routine:
Save large group identifier
Zero large group subtotals
Fill in headings with identifier for large group
Perform Process-Smaller-Group
until new identifier for large group
or end of file
Print subtotals for large group
Add large group subtotals to grand totals
Process-Smaller-Group routine:
Save smaller group identifier
Zero smaller group subtotals
Fill in headings with identifier for smaller group
Perform Process-Smallest-Group
until new identifier for smaller group
or new identifier for large group
or end of file
Print subtotals for smaller group
Add smaller group subtotals to large group subtotals
Process-Smallest-Group routine:
Save smallest group identifier
Zero smallest group subtotals
Fill in headings with identifier for smallest group
Perform Process-One-Record
until new identifier for smallest group
or new identifier for smaller group
or new identifier for large group
or end of file
Print subtotals for smallest group
Add smallest group subtotals to smaller group subtotals
Process-One-Record routine:
Do whatever needs to be done with the record
(depends on the purpose of the program)
Read the next record
Notes
The above pseudocode assumes we are adding up totals. We could easily instead be looking for a maximum value or a minimum value at each point. There may be a lot of variables involved.
If we have more than 3 levels of control, there can be any number of routines between Main and Process-One-Record, but all of them look very similar. If we have only 1 key, control-break processing loses its distinctive meaning.
The priming Read is in Main; the other Read is in Process-One-Record.
All of this is absolutely dependent on the data being sorted in order by the various key fields, which are the identifiers for the large group, smaller group, and smallest group, in that order. If we want other statistics, we need to re-sort the data and write a new program.
There is no assumption here that each large group contains the same number of smaller groups, or that each smaller group contains the same number of smallest groups. If that was the case, we could theoretically put all the data in a multidimensional table--if we could have a table large enough.
Example
Suppose our records look like this:
Salesperson-Name City Month Sales-Total Sales-Data
The data is sorted on 3 keys: 1st Salesperson-Name, 2nd City, 3rd Month.
We want a report like this:
Name: John Jones City: Baltimore Month: June
Sales-Data Sales-Amount
.......... $.......
.......... $.......
.......... $.......
.......... $.......
Total = $........
Each time a control field (a key field) changes, we have a control break. We advance to a new page. (We may end up with a lot of pages of output.) For control breaks higher than the first, we will also print totals for the city, for the salesperson, or (last) for the company as a whole.
Here is pseudocode for this example:
000-Main.
Initialize company total.
Priming read.
Perform 100-Process-One-Salesperson until End-of-File.
Print company total.
100-Process-One-Salesperson.
Save the salesperson name and put it in the heading.
Initialize salesperson total.
Perform 200-Process-One-City until End-of-File or
we have a new salesperson.
Add salesperson total to company total.
Print salesperson total.
200-Process-One-City.
Save the city name and put it in the heading.
Initialize city total.
Perform 300-Process-One-Month until End-of-File or
we have a new salesperson or
we have a new city.
Add city total to salesperson total.
Print city total.
300-Process-One-Month.
Save the month name and put it in the heading.
Advance to a new page and print the heading.
Initialize month total.
Perform 400-Process-One-Sale until End-of-File or
we have a new salesperson or
we have a new city or
we have a new month.
Add month total to city total.
Print month total.
400-Process-One-Sale.
Print a line about the latest sale.
Add sales amount to month total.
Read next record.