chapter 07 control breaks. understanding control break logic control break – a temporary detour in...

47
Chapter 07 Chapter 07 Control Breaks Control Breaks

Upload: augustine-harvey

Post on 27-Dec-2015

279 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

Chapter 07Chapter 07

Control BreaksControl Breaks

Page 2: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

Understanding Control Break LogicUnderstanding Control Break Logic

Control BreakControl Break – a temporary detour in the logic of a – a temporary detour in the logic of a program.program.

Control Break ProgramControl Break Program – a program in which a – a program in which a change in the value of a variable initiates special change in the value of a variable initiates special actions or causes special or unusual processing to actions or causes special or unusual processing to occur.occur.

Page 3: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

When do you use a Control Break Program?When do you use a Control Break Program?

When you want to organize output for programs When you want to organize output for programs that handle data records.that handle data records.

How do Control Break Programs Work?How do Control Break Programs Work?

They examine the same field in each record and They examine the same field in each record and when a different value from the one that when a different value from the one that preceded it is encountered, they perform a preceded it is encountered, they perform a special action. special action.

Page 4: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

Control Break ReportControl Break Report – a report that lists items in – a report that lists items in groups with each group followed by a subtotal.groups with each group followed by a subtotal.

Examples of Control Break ReportsExamples of Control Break Reports

All employees listed in order by department number, in All employees listed in order by department number, in which a new page starts for each departmentwhich a new page starts for each department

All company clients listed in order by state of residence, All company clients listed in order by state of residence, with a count of clients after each state’s client listwith a count of clients after each state’s client list

All books for sale in a bookstore in order by category, with a All books for sale in a bookstore in order by category, with a dollar total for the value of all books following each dollar total for the value of all books following each category of bookcategory of book

All items sold in order by date of sale, switching ink color All items sold in order by date of sale, switching ink color for each new monthfor each new month

Page 5: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

Two Shared Traits of Control Break ReportsTwo Shared Traits of Control Break Reports

1.1. The records in each report are listed in order by a specific The records in each report are listed in order by a specific variable, i.e., department, state, category or date.variable, i.e., department, state, category or date.

2.2. When the variable changes, the program takes special When the variable changes, the program takes special action, i.e., starts a new page, prints a count or total, or action, i.e., starts a new page, prints a count or total, or switches ink color.switches ink color.

To generate a Control Break Report, your input To generate a Control Break Report, your input recordsrecords

must be organized in sorted order based on the must be organized in sorted order based on the fieldfield

that will cause the breaks.that will cause the breaks.

Page 6: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

Performing Single-level Control BreaksPerforming Single-level Control Breaks

Suppose you want to print a list of employees, advancing to a Suppose you want to print a list of employees, advancing to a new page for each department. (Figure 7-3 through Figure new page for each department. (Figure 7-3 through Figure 7-7)7-7)

File name: EMPSBYDEPTFile name: EMPSBYDEPT

Sorted by: DepartmentSorted by: Department

FIELD DESCRIPTIONFIELD DESCRIPTION POSITIONSPOSITIONS DATA TYPEDATA TYPEDECIMALSDECIMALS

DepartmentDepartment 1-21-2 NumericNumeric 00

Last NameLast Name 3-143-14 CharacterCharacter

First NameFirst Name 15-2615-26 CharacterCharacter

Figure 7-1, page 247Figure 7-1, page 247

Page 7: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

EMPLOYEES BY DEPARTMENTEMPLOYEES BY DEPARTMENT

LAST NAMELAST NAME FIRST NAMEFIRST NAME

XXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXFigure 7-2, page 247Figure 7-2, page 247

empRecempRec (declare variables) (declare variables)numnum empDeptempDeptcharchar empLastempLastcharchar empFirstempFirstcharchar head1 = “EMPLOYEES BY DEPARTMENT”head1 = “EMPLOYEES BY DEPARTMENT”char head2 = “LAST NAME FIRST NAME”char head2 = “LAST NAME FIRST NAME”numnum oldDeptoldDept

Page 8: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

Basic Logic of the AlgorithmBasic Logic of the Algorithm

1.1. Read the first employee record from the input fileRead the first employee record from the input file

2.2. Determine whether the employee belongs to the same Determine whether the employee belongs to the same departmentdepartment

as the previous employeeas the previous employee

3.3. TrueTrue

Print the employee and read the next recordPrint the employee and read the next record

False (the employee does not belong to the same department)False (the employee does not belong to the same department)

Print the headings on the top of the new pagePrint the headings on the top of the new page

4.4. Finally, you proceed to print the employees who belong to the Finally, you proceed to print the employees who belong to the newnew

departmentdepartment

Page 9: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

There is a Slight Problem in this Algorithm.There is a Slight Problem in this Algorithm.What Is It?What Is It?

( Think About How Variables are Stored in Memory)( Think About How Variables are Stored in Memory)

After you read in a new record, there is no way to look back at After you read in a new record, there is no way to look back at the previous record to determine whether that record had a the previous record to determine whether that record had a different department number – the previous record’s data different department number – the previous record’s data has been replaced by the new record’s data.has been replaced by the new record’s data.

Page 10: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

How Do You Solve this Problem?How Do You Solve this Problem?

Control Break FieldControl Break Field – a special variable that is created to – a special variable that is created to “remember” the last value that was stored in a particular “remember” the last value that was stored in a particular variable location.variable location.

How Does a Control Break Field Work?How Does a Control Break Field Work?

Every time you read in a record and print it, you also can save Every time you read in a record and print it, you also can save the crucial part of the record that will signal the change or the crucial part of the record that will signal the change or control the program break. control the program break.

Page 11: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

start

housekeeping()

eof mainLoop()

finish()

stop

F

T

Figure 7-3page 248

housekeeping()

declare variables

open files

print head1

print head2

read empRec

oldDept = empDept

return

Figure 7-4page 249

finish()

close files

return

Figure 7-7page 252

Page 12: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

mainLoop()

empDept = oldDept

newPage()

print empLast, empFirst

read empRec

return

Figure 7-5page 250

newPage()

print head1

print head2

oldDept = empDept

return

Figure 7-6page 251

Page 13: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

Why would it be incorrect to initialize oldDept to the value of Why would it be incorrect to initialize oldDept to the value of empDept when you declare oldDept?empDept when you declare oldDept?

Because you have not yet read in the first record, therefore, Because you have not yet read in the first record, therefore, empDept does not yet have any usable value.empDept does not yet have any usable value.

Page 14: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

The First Two Tasks Required by all Control Break The First Two Tasks Required by all Control Break RoutinesRoutines

1.1. Performs any necessary processing for the new groupPerforms any necessary processing for the new group

2.2. Updates the control break fieldUpdates the control break field

Page 15: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

Using Control Data within the Control Using Control Data within the Control Break ModuleBreak Module

In the Employees by Department Report program example, In the Employees by Department Report program example, the control break routine printed constant headings at the the control break routine printed constant headings at the toptop of each new page; but sometimes you need to use of each new page; but sometimes you need to use Control Data within a Control Break module.Control Data within a Control Break module.

EMPLOYEES FOR DEPARTMENT 99EMPLOYEES FOR DEPARTMENT 99

LAST NAMELAST NAME FIRST NAMEFIRST NAME

XXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXX

XXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXX

XXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXX

Figure 7-8, page 252Figure 7-8, page 252

Page 16: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

The Heading Contains Two PartsThe Heading Contains Two Parts

1.1. a constant beginning, (“EMPLOYEES BY a constant beginning, (“EMPLOYEES BY DEPARTMENT”)DEPARTMENT”)

2.2. a variable ending (the department number)a variable ending (the department number)

Page 17: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

newPage()

print head2

oldDept = empDept

return

Figure 7-9page 253

print “EMPLOYEES

FOR DEPARTMENT”,

empDept

Page 18: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

Suppose you have a report where the department Suppose you have a report where the department prints prints followingfollowing the employee list for the the employee list for the

department.department.

EMPLOYEES FOR DEPARTMENTEMPLOYEES FOR DEPARTMENT

LAST NAMELAST NAME FIRST NAMEFIRST NAME

XXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXX

XXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXX

XXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXX

END OF DEPARTMENT 99END OF DEPARTMENT 99

Figure 7-10, page 254Figure 7-10, page 254

Page 19: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

FooterFooter – a message that prints at the end of a page. – a message that prints at the end of a page.

Two Basic Rules:Two Basic Rules:

1.1. Headings usually require information about the Headings usually require information about the NextNext Record Record

2.2. Footers usually require information about the Footers usually require information about the PreviousPrevious RecordRecord

Page 20: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

newPage()

print “END OF DEPARTMENT”,

oldDept

print head1

oldDept = empDept

return

Figure 7-11page 255

print head2

finish()

close files

return

Figure 7-11page 255

print “END OF DEPARTMENT”,

oldDept

Page 21: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

Three Tasks Required in All Control Break RoutinesThree Tasks Required in All Control Break Routines

1.1. Performs any necessary processing for the previous groupPerforms any necessary processing for the previous group

(i.e., writes the footer)(i.e., writes the footer)

2.2. Performs any necessary processing for the new groupPerforms any necessary processing for the new group

(i.e., writes the heading)(i.e., writes the heading)

3.3. Updates the control break fieldUpdates the control break field

(i.e., oldDept)(i.e., oldDept)

The finishUp() module for the new program containing footers The finishUp() module for the new program containing footers also requires an extra step.also requires an extra step.

Page 22: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

Two Things to NoteTwo Things to Note

1.1. The very first heading prints separately from all others at The very first heading prints separately from all others at thethe

beginningbeginning

2.2. The very last footer must print separately from all others at The very last footer must print separately from all others at the endthe end

Page 23: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

How to Perform Control Breaks with How to Perform Control Breaks with

TotalsTotals Suppose you run a bookstore, and one of the files you maintain Suppose you run a bookstore, and one of the files you maintain

is called BOOKFILE, which has one record for every book title is called BOOKFILE, which has one record for every book title that you carry. Each record has fields such as that you carry. Each record has fields such as bookTitle, bookTitle, bookAuthor, bookCategory, bookPublisher, and bookPrice.bookAuthor, bookCategory, bookPublisher, and bookPrice.

File Name: BOOKFILEFile Name: BOOKFILESorted by: CategorySorted by: CategoryFIELD DESCRIPTIONFIELD DESCRIPTION POSITIONSPOSITIONS DATA TYPEDATA TYPE DECIMALSDECIMALSTitleTitle 1-301-30 CharacterCharacterAuthorAuthor 31-3631-36 CharacterCharacterCategoryCategory 47-5647-56 CharacterCharacterPublisherPublisher 57-7257-72 CharacterCharacterPricePrice 73-7773-77 NumericNumeric 22Figure 7-12, page 257Figure 7-12, page 257

Page 24: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

Suppose you want to print out a list of all the books that your Suppose you want to print out a list of all the books that your store carries with a total number of books at the bottom of store carries with a total number of books at the bottom of the list.the list.

BOOK LISTBOOK LIST

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Total number of book titles: 999Total number of book titles: 999

Figure 7-13, page 258Figure 7-13, page 258

Page 25: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

start

startUp()

eof bookListLoop()

closeDown()

stop

F

T

startUp()

declare variables

open files

print heading

read bookRec

return

bookListLoop()

print bookTitle

add 1 to grandTotal

read bookRec

return

Figure 7-14page 258

closeDown()

print “Total number of book

titles”, grandTotal

close files

return

Page 26: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

The bookListLoop() Module Performs Three Major Tasks:The bookListLoop() Module Performs Three Major Tasks:

1.1. Prints a book titlePrints a book title

2.2. Adds 1 to the grandTotalAdds 1 to the grandTotal

3.3. Reads in the next book recordReads in the next book record

Page 27: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

Suppose that you decide you want a count for each category Suppose that you decide you want a count for each category of book rather than just one grand total.of book rather than just one grand total.

You need two new variables:You need two new variables:

previousCategorypreviousCategory

categoryTotalcategoryTotal

Rolling up the TotalsRolling up the Totals – adding a total to a higher-level total – adding a total to a higher-level total

Page 28: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

start

startUp()

eof bookListLoop()

closeDown()

stop

F

T

startUp()

declare variables

open files

print heading

read bookRec

return

Figure 7-15page 260

previousCategory = bookcategory

Page 29: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

bookListLoop()

print bookTitle

add 1 to categoryTotal

read bookRec

return

Figure 7-15page 260

bookCategory = previousCategory

categoryChange

F T

categoryChange

print “CategoryCount”,

categoryTotal

add categoryTotal tograndTotal

categoryTotal = 0

previousCategory =bookCategory

return

Page 30: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

Performs Four of the Five Tasks required by all Control Break Performs Four of the Five Tasks required by all Control Break routines that include totalsroutines that include totals

1.1. Performs any necessary processing for the previous groupPerforms any necessary processing for the previous group(i.e., it prints the categoryTotal)(i.e., it prints the categoryTotal)

2.2. Rolls up the current level totals to the next higher levelRolls up the current level totals to the next higher level(i.e., it adds categoryTotal to grandTotal)(i.e., it adds categoryTotal to grandTotal)

3.3. Resets the current level’s totals to zeroResets the current level’s totals to zero(i.e., the categoryTotal is set to zero)(i.e., the categoryTotal is set to zero)

4.4. Performs any necessary processing for the new groupPerforms any necessary processing for the new group(i.e., there is none here)(i.e., there is none here)

5.5. Updates the control break fieldUpdates the control break field(i.e., previousCategory)(i.e., previousCategory)

Page 31: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

How Does the closedown( ) Module Change?How Does the closedown( ) Module Change?

1.1. You must print the last categoryTotalYou must print the last categoryTotal

2.2. You add the count for the last category into the grandTotalYou add the count for the last category into the grandTotal

Options:Options:

(1)(1) perform these two task as separate stepsperform these two task as separate steps

(2)(2) perform the control break routine categoryChange() perform the control break routine categoryChange() one lastone last

timetime

Page 32: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

closeDown()

print “Totalnumber of book

titles”, grandTotal

closefiles

return

categoryChange

Page 33: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

Important NotesImportant Notes

This control break program works whether there are three This control break program works whether there are three categories of books or 300.categories of books or 300.

It does not matter what the categories of books are.It does not matter what the categories of books are.

For example, the program never asks bookCategory = For example, the program never asks bookCategory = “fiction”,“fiction”,

instead the control of the program breaks when the category instead the control of the program breaks when the category fieldfield

changeschanges and it is in no way dependent on and it is in no way dependent on whatwhat that change is. that change is.

Page 34: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

How to Perform Multiple-level Control How to Perform Multiple-level Control BreaksBreaks

Let’s say your bookstore from the last example is so Let’s say your bookstore from the last example is so successful that you have a chain of them across the successful that you have a chain of them across the country. Everytime a sale is made you create a record with country. Everytime a sale is made you create a record with the fieldsthe fields bookTitle, bookCity, and bookState. bookTitle, bookCity, and bookState. You would You would like a report that prints a summary of books sold in each like a report that prints a summary of books sold in each city and each state.city and each state.

Page 35: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

BOOK SALES BY CITY AND STATEBOOK SALES BY CITY AND STATE

AmesAmes 200 200Des MoinesDes Moines 814 814Iowa CityIowa City 291 291

Total for IATotal for IA 13051305

ChicagoChicago 10931093Crystal LakeCrystal Lake 564 564McHenryMcHenry 213 213SpringfieldSpringfield 365 365

Total for ILTotal for IL 22352235

SpringfieldSpringfield 100 100WorcesterWorcester 389 389

Total for MATotal for MAGrand TotalGrand Total 39293929

Figure 7-16, page 262Figure 7-16, page 262

Page 36: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

Summary ReportSummary Report – a report that does not include any – a report that does not include any information about individual records, but instead includes information about individual records, but instead includes group totals.group totals.

Multiple-level Control BreakMultiple-level Control Break – the normal flow of control – the normal flow of control breaks away to print totals in response to more than just breaks away to print totals in response to more than just one change in condition.one change in condition.

Page 37: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

Just as the file you use to create a single-level control break Just as the file you use to create a single-level control break must be presorted, so must the input file you use to create must be presorted, so must the input file you use to create a multiple-level control break report.a multiple-level control break report.

Control Break FieldsControl Break Fields::

prevCityprevCityprevStateprevState

AccumulatorsAccumulators::

cityCountercityCounterstateCounterstateCountergrandTotalgrandTotal

Control Break ModulesControl Break Modules::

cityBreak()cityBreak()stateBreak()stateBreak()

Page 38: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

housekeeping()

declare variables

open files

read empRec

prevCity = bookCity

return

Figure 7-17page 263

headings()

prevState = bookstate

mainLoop()

bookState = prevState ?

bookCity = prevCity ?

stateBreak()

cityBreak()

add 1 to cityCounter

read bookRec

return

Figure 7-19page 265

TF

TF

stateBreak()

cityBreak()

print “Total for”, prevState,

stateCounter

stateCounter = 0

prevState = bookState

return

grandTotal = grandTotal + stateCounter

Figure 7-20page 266

cityBreak()

return

Figure 7-21page 266

print prevCity,cityCounter

stateCounter = stateCounter +

cityCounter

cityCounter = 0

prevCity=bookCity

closeDown()

stateBreak()

print “Grand Total”,

grandTotal

close files

return

Figure 7-22page 267

Page 39: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

Every time there is a change in the bookCity field, the Every time there is a change in the bookCity field, the cityBreak() module performs these standard control break cityBreak() module performs these standard control break

taskstasks

1.1. Performs any necessary processing for the previous groupPerforms any necessary processing for the previous group(i.e., prints totals for the previous city)(i.e., prints totals for the previous city)

2.2. Rolls up the current level totals to the next higher levelRolls up the current level totals to the next higher level(i.e., adds the city count to the state count)(i.e., adds the city count to the state count)

3.3. Resets the current level’s totals to zeroResets the current level’s totals to zero(i.e., sets the city count to zero)(i.e., sets the city count to zero)

4.4. Performs any necessary processing for the new groupPerforms any necessary processing for the new group(i.e., in this case there is none)(i.e., in this case there is none)

5.5. It updates the control break fieldIt updates the control break field(i.e., sets prevCity to bookCity)(i.e., sets prevCity to bookCity)

Page 40: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

Within the stateBreak() module, you must perform one Within the stateBreak() module, you must perform one newnew type of type of task, as well as the control break tasks you are familiar with.task, as well as the control break tasks you are familiar with.

The stateBreak() module does the followingThe stateBreak() module does the following

1.1. It processes the lower-level breakIt processes the lower-level break(i.e., cityBreak())(i.e., cityBreak())

2.2. Performs any necessary processing for the previous groupPerforms any necessary processing for the previous group(i.e., prints totals for the previous state)(i.e., prints totals for the previous state)

3.3. Rolls up the current level totals to the next higher levelRolls up the current level totals to the next higher level(i.e., adds the state count to the grand total)(i.e., adds the state count to the grand total)

4.4. Resets the current level’s totals to zeroResets the current level’s totals to zero(i.e., sets the state count to zero)(i.e., sets the state count to zero)

5.5. Performs any necessary processing for the new groupPerforms any necessary processing for the new group(i.e., in this case there is none)(i.e., in this case there is none)

6.6. Updates the control break fieldUpdates the control break field(i.e., sets prevState to bookState)(i.e., sets prevState to bookState)

Page 41: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

Why is It Necessary to Check bookState before checking Why is It Necessary to Check bookState before checking bookCity?bookCity?

Because when a bookCity changes, the bookState also Because when a bookCity changes, the bookState also mightmight be changing, but when bookState changes, it means the be changing, but when bookState changes, it means the bookCity bookCity mustmust be changing. be changing.

** You should always check for the major-level break ** You should always check for the major-level break first **first **

Page 42: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

Major-Level and Minor-Level BreaksMajor-Level and Minor-Level Breaks

If the records are sorted by bookCity within bookState, then a If the records are sorted by bookCity within bookState, then a change in bookState causes a change in bookState causes a major-level breakmajor-level break and a and a change in bookCity causes a change in bookCity causes a minor-level breakminor-level break..

What Occurs in the closedown() Module?What Occurs in the closedown() Module?

1.1. Perform cityBreak()Perform cityBreak()

2.2. Perform stateBreak()Perform stateBreak()

3.3. Print grandTotal variablePrint grandTotal variable

Page 43: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

A Control Break Program Should Check Whether You Need To A Control Break Program Should Check Whether You Need To Complete Each of the Following Tasks with ModulesComplete Each of the Following Tasks with Modules

1.1. Performing the lower-level break, if anyPerforming the lower-level break, if any

2.2. Performing any control break processing for the previous Performing any control break processing for the previous groupgroup

3.3. Rolling up the current level totals to the next higher levelRolling up the current level totals to the next higher level

4.4. Resetting the current level’s totals to zeroResetting the current level’s totals to zero

5.5. Performing any control break processing for the new groupPerforming any control break processing for the new group

6.6. Updating the control break fieldUpdating the control break field

Page 44: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

How to Perform Page BreaksHow to Perform Page Breaks

Let’s say you have a file called Let’s say you have a file called CUSTOMERFILE CUSTOMERFILE that contains that contains 1,000 customers with two character fields that you have 1,000 customers with two character fields that you have decided to call decided to call custLast custLast andand custFirst. custFirst. You want to print a You want to print a list of these customers, 60 detail lines to a page.list of these customers, 60 detail lines to a page.

What Is the Solution to this Problem?What Is the Solution to this Problem?

You will use a You will use a line-counterline-counter variable to keep track of the variable to keep track of the number of printed lines so that you can break to a new number of printed lines so that you can break to a new page after printing 60 lines.page after printing 60 lines.

Page 45: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

start

getReady()

eof produceReport

cleanUp()

stop

F

T

Figure 7-24page 269

getReady()

open files

print head1

print head2

read custRec

return

Figure 7-25page 269

produceReport()

lineCounter = 60

startNewpage()

print custLast, custFirst

add 1 to lineCounter

read custRec

return

Figure 7-26page 270

startNewPage()

print head1

print head2

lineCounter = 0

return

Figure 7-27page 271

Page 46: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

What Happens If You Neglect to reset the lineCounter?What Happens If You Neglect to reset the lineCounter?

Its value will increase with each successive record and never Its value will increase with each successive record and never be equal to 60 again.be equal to 60 again.

Page 47: Chapter 07 Control Breaks. Understanding Control Break Logic Control Break – a temporary detour in the logic of a program. Control Break Program – a program

The startNewPage() module must perform only Two Tasks you have The startNewPage() module must perform only Two Tasks you have seen required by control break routinesseen required by control break routines

1.1. It does not perform the lower-level break, because there is noneIt does not perform the lower-level break, because there is none

2.2. It does not perform any control break processing for the previousIt does not perform any control break processing for the previousgroup, because there is nonegroup, because there is none

3.3. It does not roll up the current level totals to the next higher level,It does not roll up the current level totals to the next higher level,because there are no totalsbecause there are no totals

4.4. It does not reset the current level’s totals to zero, because there It does not reset the current level’s totals to zero, because there areareno totalsno totals

5.5. it does perform control break processing for the new group byit does perform control break processing for the new group bywriting headings at the top of the new pagewriting headings at the top of the new page

6.6. it does update the control break field – the line counterit does update the control break field – the line counter