Title: Programming Logic and Design Fifth Edition, Comprehensive
1Programming Logic and Design Fifth Edition,
Comprehensive
2Objectives
- Learn about control break logic
- Perform single-level control breaks
- Use control data within a heading
- Use control data within a footer
- Perform control breaks with totals
- Perform multiple-level control breaks
3Understanding Control Break Logic
- Control break temporary detour in program logic
- Control break program
- Program in which a change in a variables value
causes special or unusual processing to occur - Helps organize output from processing data
records - When a grouping variable is changed, special
actions such as subtotals and new subheadings may
occur - Control break report report with items in
groups, including subtotals by group
4Figure 8-1 A control break report with totals
after each state
5Understanding Control Break Logic (continued)
- Other examples of control break reports
- Employees listed by department number
- Books for sale in a bookstore by category
- Items sold in order by date of sale
- Two traits of each report
- Records are listed in order by specific variable
- When variable changes, program takes special
action - Starts a new page
- Prints a count
- Switches ink color
6Understanding Control Break Logic (continued)
- Input records must be organized in sequential
order based on the grouping field - Sorting rearranging records in a particular order
7Performing a Single-Level Control Break
- Example employees listed by department
- Line separates each department
- Basic program logic
- Read employees name and department number from
input file - Determine whether employee in same department as
previous employee - If yes, print the employees name
- If no, print separating line and print employee
record - Single-level control break a break in the logic
of the program based on value of single variable
8Figure 8-2 Employees by department
9Performing a Single-Level Control Break
(continued)
- Control break field saves the information needed
to determine when a break should occur - Every time a record is read and printed, save the
part of the record that controls the program
break - Compare new and old values to determine when it
is time to print the break
10Figure 8-3 Main program logic for program that
prints employees by department
11Figure 8-4 The produceReport() module
12Performing a Single-Level Control Break
(continued)
- When a new department differs from the old one
- Department-separating line must be printed
- Control break field must be updated
- If new employee works in same department as
previous - Employees name printed
- Logic returns to the main program, next record
read - Two tasks required for all control breaks
- Processing starts after old group ends and before
new group starts - Control break field is updated
13Using Control Data within a Heading
- Example display the department number in heading
prior to starting each new department - Helpful to use the control data within the
heading - Create a program
- Modify produceReport() module to print a heading
for the new group - Modify main program to print first heading before
entering the main loop
14Figure 8-5 Employees by department with the
department designated before each group
15Figure 8-6 Modified produceReport() module
16Figure 8-7 Main program that prints heading for
the first department before entering the
produceReport() method
17Using Control Data within a Footer
- Example print the department number following
the employee list for the department - Footer message printed at end of page or section
- Headings require information about the next
record - Footers require information about the previous
record
18Figure 8-8 Employees by Department including a
footer after each department
19Figure 8-9 Program that includes a call to a
method that prints a footer
20Figure 8-9 Program that includes a call to a
method that prints a footer (continued)
21Figure 8-9 Program that includes a call to a
method that prints a footer (continued)
22Figure 8-10 The processDeptEnd() method
23Using Control Data within a Footer (continued)
- When a control break occurs, the program must
- Print the footer for the previous group
- Print the heading for the new group
- Update the control break field
- For the very last group, the program must
- Print the footer for the last group
- Not print another heading
- First heading and last footer must be handled
separately from the others
24Performing Control Breaks with Totals
- Example bookstore maintains files with one
record for every book title - Each record has fields that hold
- Book title
- Author
- Category
- Price
- Print a list of all books the store carries
- Total at the bottom of the list
25Figure 8-11 Book file description
26Figure 8-12 Sample Book List report
27Figure 8-13 Flowchart and pseudocode that
produces report in Figure 8-12
28Figure 8-14 Sample report that lists books by
category with category counts
29Figure 8-15 Flowchart and pseudocode for program
that lists books by category with category counts
30Figure 8-15 Flowchart and pseudocode for program
that lists books by category with category counts
(continued)
31Performing Control Breaks with Totals (continued)
- Comparing Figure 8-13 and 8-15
- New variables and constants declared
- Value of category for first book stored in
prevCategory variable - Every time record enters main program loop
- Program checks if record represents new category
- If next category matches previous, process as
usual - If next category does not match, roll up the
totals - After all records processed, print the total
32Performing Control Breaks with Totals (continued)
- Five tasks required in all control breaks that
print totals - Perform processing for previous group
- Roll up the current-level totals to next higher
level - Reset the current levels totals to 0
- Perform processing for new group
- Update the control break field
- When series of steps in a program is identical to
another series of steps, place them in a method
33Figure 8-16 Program in Figure 8-15 revised to
call a module
34Figure 8-16 Program in Figure 8-15 revised to
call a module (continued)
35Performing Multiple-Level Control Breaks
- Summary report group totals, not detail records
- Multiple-level control break breaks occur for
more than one change in condition
Figure 8-17 Report of book sales by city and
state
36Performing Multiple-Level Control Breaks
(continued)
- In this example, break occurs when
- Value of city variable changes
- Value of state variable changes
- Input file must be sorted by city within state
- At end of citys records, print total for city
- At end of states records, print total for state
- Use arrays to store book counts as well as
control break fields
37Performing Multiple-Level Control Breaks
(continued)
- Three levels of totals
- City
- State
- Grand
- Two arrays
- Book totals
- totalCITY, totalSTATE, totalGRAND
- Previous input records
- prevCITY, prevSTATE
38Figure 8-18 Main program logic for the Book
Sales by City and State report program
39Performing Multiple-Level Control Breaks
(continued)
- cityBreak() routine performs standard tasks
- Performs processing for previous group
- Rolls up the current-level totals to next higher
level - Resets current levels totals to 0
- Performs processing for new group
- Updates the control break field
- stateBreak() does the same, starting with
processing cityBreak()
40Figure 8-19 The cityBreak() method for the Book
Sales by City and State report program
41Figure 8-20 The stateBreak() method for the Book
Sales by City and State report program
42Performing Multiple-Level Control Breaks
(continued)
- Main program checks for change in city and state
variables - When city changes, citys name and total printed
- When state changes, states name and total
printed - All city totals within a state print before state
total for same state - Seems logical to check for change in city before
state - Must check for state change first
43Figure 8-21 Sample data for Book Sales report
44Performing Multiple-Level Control Breaks
(continued)
- If two cities with the same name follow each
other - Program will not detect new city name
- Always check major-level break first
- Records sorted by city within state change in
state causes major-level break - If records sorted by city within state, then
change in city causes minor-level break - Change in state implies a change in city
- Even if the cities have the same name
45Performing Multiple-Level Control Breaks
(continued)
- Within each break module, check if you need to
- Perform the lower-level break (if any)
- Perform control break processing for previous
group - Roll up the current-level totals to next higher
level - Reset the current-level totals to 0
- Perform any control break processing for new
group - Update the control break field
46Summary
- Control break a change in a variables value
causes special actions to occur - Control break field holds data from a previous
record to compare to the current record - Control data can be used in a heading or footer
- Control break report contains and prints totals
for each group, as well as grand totals
47Summary (continued)
- For multiple-level control breaks, test for a
major-level break before a minor-level break - In a control break, check if lower-level breaks
need to be processed - Page breaks can be handled based on line counters