Figure 7-5 A control break report with totals after each state Other examples of control break reports produced by control break programs could include: •All employees listed in order by department number, with a new page started for each department •All books for sale in a bookstore listed in order by category (such as reference or self-help), with a count following each category of book •All items sold in order by date of sale, with a different ink color for each new month Each of these reports shares two traits: •The records used in a report are listed in order by a specific variable: state, department, category, or date. •When the value of that variable changes in a new record, the program takes special action: it starts a new page, prints a count or total, or switches ink color. To generate a control break report, the input records must be organized in sequential order based on the field that will cause the breaks. In other words, to write a program that produces a report of customers by state, such as the one in Figure 7-5, the records must be grouped by state before you begin processing. With some languages, such as SQL (Structured Query Language, which is used with database processing), the details of control break processing are handled automatically. Still, understanding how control break programs work improves your competence as a programmer. Suppose that you have an input file that contains client names, cities, and states, and you want to produce a report like the one in Figure 7-5. The basic logic of the program works like this: •Each time you read a client’s record from the input file, you determine whether the client resides in the same state as the previous client. •If so, you simply output the client’s data, add 1 to a counter, and read another record, without any special processing. If there are 20 clients in a state, these steps are repeated 20 times in a row—read a client’s data, count it, and output it.•Eventually you will read a record for a client who is not in the same state. At that point, before you output the data for the first client in the new state, you must output the count for the previous state. •You also must reset the counter to 0 so it is ready to start counting customers in the next state. Then you can proceed to handle client records for the new state, and you continue to do so until the next time you encounter a client from a different state. This type of program contains a single-level control break, a break in the logic of the program (in this case, pausing or detouring to output a count) that is based on the value of a single variable (in this case, the state). The technique you must use to “remember” the old state so you can compare it with each new client’s state is to create a special variable, called a control break field, to hold the previous state. As you read each new record, comparing the new and old state values determines when it is time to output the count for the previous state. Figure 7-6 shows the mainline logic and getReady() module for a program that produces the report in Figure 7-5In the mainline logic, the control break variable oldState is declared in the shaded statement. In .
You've reached the end of your free preview.
Want to read all 23 pages?
- Summer '16
- Computer file