Chapter 07 Control Breaks.

Slides:



Advertisements
Similar presentations
Microsoft ® Office Word 2007 Training Header and footer basics Sweetwater ISD presents:
Advertisements

Data Dependencies Describes the normal situation that the data that instructions use depend upon the data created by other instructions, or data is stored.
1 Transportation Model. 2 Basic Problem The basic idea in a transportation problem is that there are sites or sources of product that need to be shipped.
Chapter 9: Advanced Array Manipulation
System Design System Design - Mr. Ahmad Al-Ghoul System Analysis and Design.
Programming Logic and Design Fourth Edition, Introductory
Programming Logic and Design, Second Edition, Comprehensive
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
Chapter 7: Control Breaks Programming Logic and Design, Third Edition Comprehensive.
Programming Logic and Design, Third Edition Comprehensive
Programming Logic and Design Fourth Edition, Introductory
Objectives In this chapter, you will learn about:
Writing a Complete Program
Understanding Arrays and Pointers Object-Oriented Programming Using C++ Second Edition 3.
Modules, Hierarchy Charts, and Documentation
General Algorithms for Common Business Problems
Chapter 1 Program Design
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
- Meeting 4 – Writing a Complete Program
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
Cost Volume Profit Analysis or Break Even Analysis Dr. R. Jayaraj, M.A., Ph.D.,
Structured COBOL Programming, Stern & Stern, 9th Edition PowerPoint Presentation: Richard H. Baum, Ph.D. DeVry Institute of Technology 9th Edition Structured.
Copyright CovalentWorks Training Guide for Invoices MYB2B Powered by CovalentWorks.
Structured COBOL Programming Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout.
10- 1 Chapter 10. To familiarize you with  Main types of computer-generated reports  Techniques used for efficient printing of group reports and control.
Programming Logic and Design Seventh Edition
10-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
Simple Program Design Third Edition A Step-by-Step Approach
Programming Logic and Design, Second Edition, Comprehensive
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Selection Control Structures Simple Program Design Third Edition A Step-by-Step Approach 4.
Programming Logic and Design Fifth Edition, Comprehensive
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
Database Systems Microsoft Access Practical #2 Making Forms and Reports Nos 215.
File Handling and Control Break Logic. Objectives In this chapter, you will learn about: Computer files Writing a program that reads from and/or writes.
6 Chapter 61 Looping Programming Logic and Design, Second Edition, Comprehensive 6.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Chapter 2 Pseudocode. Objectives To introduce common words, keywords and meaningful names when writing pseudocode To define the three basic control structures.
General Algorithms for Common Business Problems Simple Program Design Third Edition A Step-by-Step Approach 10.
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
Verification & Validation. Batch processing In a batch processing system, documents such as sales orders are collected into batches of typically 50 documents.
13-1 Sequential File Processing Chapter Chapter Contents Overview of Sequential File Processing Sequential File Updating - Creating a New Master.
1.  Introduction  The Benefits of the Report Writer Module ◦ For Detail and Summary Printing ◦ For Control Break Processing ◦ For Printing Headings.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Control Break Processing
COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
Program variables Counters Accumulators Major and minor totals Control Click icon to hear comments (the download may take a minute )
Chapter 11: Sequential File Merging, Matching, and Updating Programming Logic and Design, Third Edition Comprehensive.
11 Chapter 111 Sequential File Merging, Matching, and Updating Programming Logic and Design, Second Edition, Comprehensive 11.
Chapter 9 General algorithms for common business problems.
ACCESS REPORT GENERATOR. IT Fundamentals2 Reports Overview Reports are an important part of any computer business application. Features that are important.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
World of Wokcraft The very best in Single pan cooking themed fantasy gaming!
Programming Logic and Design Fourth Edition, Comprehensive
Programming Logic and Design Eighth Edition
Structured COBOL Programming
Designing and Writing Control-Break Programs
Algorithm Discovery and Design
CHAPTER 17 The Report Writer Module
Writing a Complete Program
Chapter 4: Writing and Designing a Complete Program
Presentation transcript:

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.

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.

Examples of Control Break Reports 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

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 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 DESCRIPTION POSITIONS DATA TYPE DECIMALS Department 1-2 Numeric 0 Last Name 3-14 Character First Name 15-26 Character Figure 7-1, page 247

EMPLOYEES BY DEPARTMENT LAST NAME FIRST NAME XXXXXXXXXXXX XXXXXXXXXXXX Figure 7-2, page 247 empRec (declare variables) num empDept char empLast char empFirst char head1 = “EMPLOYEES BY DEPARTMENT” char head2 = “LAST NAME FIRST NAME” num oldDept

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

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.

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.

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.

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 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 NAME FIRST NAME XXXXXXXXXXXX XXXXXXXXXXXX Figure 7-8, page 252

The Heading Contains Two Parts 1. a constant beginning, (“EMPLOYEES BY DEPARTMENT”) 2. a variable ending (the department number)

Suppose you have a report where the department prints following the employee list for the department. EMPLOYEES FOR DEPARTMENT LAST NAME FIRST NAME XXXXXXXXXXXX XXXXXXXXXXXX END OF DEPARTMENT 99 Figure 7-10, page 254

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

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.

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 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 DESCRIPTION POSITIONS DATA TYPE DECIMALS Title 1-30 Character Author 31-36 Character Category 47-56 Character Publisher 57-72 Character Price 73-77 Numeric 2 Figure 7-12, page 257

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 Total number of book titles: 999 Figure 7-13, page 258

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

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

1. Performs any necessary processing for the previous group 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)

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

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

BOOK SALES BY CITY AND STATE Ames 200 Des Moines 814 Iowa City 291 Total for IA 1305 Chicago 1093 Crystal Lake 564 McHenry 213 Springfield 365 Total for IL 2235 Springfield 100 Worcester 389 Total for MA Grand Total 3929 Figure 7-16, page 262

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.

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()

1. Performs any necessary processing for the previous group 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)

The stateBreak() module does the following 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)

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 **

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

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

What Happens If You Neglect to reset the lineCounter? Its value will increase with each successive record and never be equal to 60 again.

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