Chapter 07
This presentation is the property of its rightful owner.
Sponsored Links
1 / 47

Chapter 07 PowerPoint PPT Presentation


  • 72 Views
  • Uploaded on
  • Presentation posted in: General

Chapter 07. Control Breaks. Understanding Control Break Logic. Control Break – a temporary detour in the logic of a program. Control Break Program – a program in which a change in the value of a variable initiates special actions or causes special or unusual processing to occur.

Download Presentation

Chapter 07

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Chapter 07

Chapter 07

Control Breaks


Understanding control break logic

Understanding Control Break Logic

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

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


Chapter 07

When do you use a Control Break Program?

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

How do Control Break Programs Work?

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


Chapter 07

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

Examples of Control Break Reports

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

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

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

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


Chapter 07

Two Shared Traits of Control Break Reports

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

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

To generate a Control Break Report, your input records

must be organized in sorted order based on the field

that will cause the breaks.


Performing single level control breaks

Performing Single-level Control Breaks

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

File name: EMPSBYDEPT

Sorted by: Department

FIELD DESCRIPTIONPOSITIONSDATA TYPEDECIMALS

Department1-2Numeric0

Last Name3-14Character

First Name15-26Character

Figure 7-1, page 247


Chapter 07

EMPLOYEES BY DEPARTMENT

LAST NAMEFIRST NAME

XXXXXXXXXXXXXXXXXXXXXXXX

XXXXXXXXXXXXXXXXXXXXXXXX

XXXXXXXXXXXXXXXXXXXXXXXX

Figure 7-2, page 247

empRec (declare variables)

numempDept

charempLast

charempFirst

charhead1 = “EMPLOYEES BY DEPARTMENT”

char head2 = “LAST NAME FIRST NAME”

numoldDept


Chapter 07

Basic Logic of the Algorithm

1.Read the first employee record from the input file

2.Determine whether the employee belongs to the same department

as the previous employee

3.True

Print the employee and read the next record

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

Print the headings on the top of the new page

4.Finally, you proceed to print the employees who belong to the new

department


Chapter 07

There is a Slight Problem in this Algorithm.

What Is It?

( Think About How Variables are Stored in Memory)

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 different department number – the previous record’s data has been replaced by the new record’s data.


Chapter 07

How Do You Solve this Problem?

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

How Does a Control Break Field Work?

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 control the program break.


Chapter 07

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

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


Chapter 07

The First Two Tasks Required by all Control Break Routines

1.Performs any necessary processing for the new group

2.Updates the control break field


Using control data within the control break module

Using Control Data within the Control Break Module

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

EMPLOYEES FOR DEPARTMENT 99

LAST NAMEFIRST NAME

XXXXXXXXXXXXXXXXXXXXXXXX

XXXXXXXXXXXXXXXXXXXXXXXX

XXXXXXXXXXXXXXXXXXXXXXXX

Figure 7-8, page 252


Chapter 07

The Heading Contains Two Parts

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

2.a variable ending (the department number)


Chapter 07

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

EMPLOYEES FOR DEPARTMENT

LAST NAMEFIRST NAME

XXXXXXXXXXXXXXXXXXXXXXXX

XXXXXXXXXXXXXXXXXXXXXXXX

XXXXXXXXXXXXXXXXXXXXXXXX

END OF DEPARTMENT 99

Figure 7-10, page 254


Chapter 07

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

Two Basic Rules:

1.Headings usually require information about the Next Record

2.Footers usually require information about the Previous Record


Chapter 07

Three Tasks Required in All Control Break Routines

1.Performs any necessary processing for the previous group

(i.e., writes the footer)

2.Performs any necessary processing for the new group

(i.e., writes the heading)

3.Updates the control break field

(i.e., oldDept)

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


Chapter 07

Two Things to Note

1.The very first heading prints separately from all others at the

beginning

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


How to perform control breaks with totals

How to Perform Control Breaks with Totals

Suppose you run a bookstore, and one of the files you maintain is called BOOKFILE, which has one record for every book title that you carry. Each record has fields such as bookTitle, bookAuthor, bookCategory, bookPublisher, and bookPrice.

File Name: BOOKFILE

Sorted by: Category

FIELD DESCRIPTIONPOSITIONSDATA TYPEDECIMALS

Title1-30Character

Author31-36Character

Category47-56Character

Publisher57-72Character

Price73-77Numeric2

Figure 7-12, page 257


Chapter 07

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 the list.

BOOK LIST

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Total number of book titles: 999

Figure 7-13, page 258


Chapter 07

The bookListLoop() Module Performs Three Major Tasks:

1.Prints a book title

2.Adds 1 to the grandTotal

3.Reads in the next book record


Chapter 07

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

You need two new variables:

previousCategory

categoryTotal

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


Chapter 07

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

1.Performs any necessary processing for the previous group

(i.e., it prints the categoryTotal)

2.Rolls up the current level totals to the next higher level

(i.e., it adds categoryTotal to grandTotal)

3.Resets the current level’s totals to zero

(i.e., the categoryTotal is set to zero)

4.Performs any necessary processing for the new group

(i.e., there is none here)

5.Updates the control break field

(i.e., previousCategory)


Chapter 07

How Does the closedown( ) Module Change?

1.You must print the last categoryTotal

2.You add the count for the last category into the grandTotal

Options:

(1)perform these two task as separate steps

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

time


Chapter 07

Important Notes

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

It does not matter what the categories of books are.

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

instead the control of the program breaks when the category field

changes and it is in no way dependent on what that change is.


How to perform multiple level control breaks

How to Perform Multiple-level Control Breaks

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


Chapter 07

BOOK SALES BY CITY AND STATE

Ames 200

Des Moines 814

Iowa City 291

Total for IA1305

Chicago1093

Crystal Lake 564

McHenry 213

Springfield 365

Total for IL2235

Springfield 100

Worcester 389

Total for MA

Grand Total3929

Figure 7-16, page 262


Chapter 07

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

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


Chapter 07

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 a multiple-level control break report.

Control Break Fields:

prevCity

prevState

Accumulators:

cityCounter

stateCounter

grandTotal

Control Break Modules:

cityBreak()

stateBreak()


Chapter 07

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

1.Performs any necessary processing for the previous group

(i.e., prints totals for the previous city)

2.Rolls up the current level totals to the next higher level

(i.e., adds the city count to the state count)

3.Resets the current level’s totals to zero

(i.e., sets the city count to zero)

4.Performs any necessary processing for the new group

(i.e., in this case there is none)

5.It updates the control break field

(i.e., sets prevCity to bookCity)


Chapter 07

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

The stateBreak() module does the following

1.It processes the lower-level break

(i.e., cityBreak())

2.Performs any necessary processing for the previous group

(i.e., prints totals for the previous state)

3.Rolls up the current level totals to the next higher level

(i.e., adds the state count to the grand total)

4.Resets the current level’s totals to zero

(i.e., sets the state count to zero)

5.Performs any necessary processing for the new group

(i.e., in this case there is none)

6.Updates the control break field

(i.e., sets prevState to bookState)


Chapter 07

Why is It Necessary to Check bookState before checking bookCity?

Because when a bookCity changes, the bookState also might be changing, but when bookState changes, it means the bookCity must be changing.

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


Chapter 07

Major-Level and Minor-Level Breaks

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

What Occurs in the closedown() Module?

1.Perform cityBreak()

2.Perform stateBreak()

3.Print grandTotal variable


Chapter 07

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

1.Performing the lower-level break, if any

2.Performing any control break processing for the previous group

3.Rolling up the current level totals to the next higher level

4.Resetting the current level’s totals to zero

5.Performing any control break processing for the new group

6.Updating the control break field


How to perform page breaks

How to Perform Page Breaks

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

What Is the Solution to this Problem?

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


Chapter 07

What Happens If You Neglect to reset the lineCounter?

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


Chapter 07

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

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

2.It does not perform any control break processing for the previous

group, because there is none

3.It does not roll up the current level totals to the next higher level,

because there are no totals

4.It does not reset the current level’s totals to zero, because there are

no totals

5.it does perform control break processing for the new group by

writing headings at the top of the new page

6.it does update the control break field – the line counter


  • Login