Counter Controlled Loop
NOTE: The problems in the previous chapters perform the processing steps for only one input record. If you need to perform the processing steps multiple times for more than one input record, then you need to use a loop. A loop performs the steps multiple times. A computer will perform the loop steps until it is told to stop. There are four methods that can be used to tell the computer when to stop performing the loop steps. Two of the methods are covered in Chapter 4: Counter Controlled loop and Header Record Logic. The loops in Chapter 4 can only be used if the number of times the loop will be performed is known. Two of the methods are covered in Chapter 5: Trailer Record logic and Automatic End of File. The loops in Chapter 5 are used when the data is large or volatile. It is used when there are many input records and the number of input records are constantly changing. For example when dealing with student files, the number of enrolled students is constantly increasing and decreasing.
1. Characteristics of a loop
NOTE: The keywords for the DOWHILE loop in pseudocode are DOWHILE, which marks the beginning of the loop, and ENDDO, which marks the end of the loop. All statements between the words DOWHILE and ENDDO are on the true path of the loop and are performed when there are more records to process. All statements on the true path are indented. All statements on the false path of the loop are placed after the keyword ENDDO. These statements are processed when there are no more records to process and the loop is exited. The statements on the false path of the loop are not indented. The students will work on problems that have statements placed on the false path of the loop in Chapter 5 when totals need to be output.
Flowchart Example:
Accumulator – a variable that is used to hold the sum of a group of values. This sum is computed by gradually adding each value to the variable each time the loop is executed.
Counter - a special type of accumulator.
A counter adds or accumulates by a constant amount, usually 1. The value
of such a counter increases by 1 each time the loop if executed.
- Purpose of the preparation symbol –
Preparation Symbol -
- Problems using counter controlled loops–
Input File
Kendall Boulton
Charleen Thompson
Carol Knox
Michael Worthington
Warren Smith
Pseudocode Example:
Problem 9 – This problem walks through a flowchart that is already designed. Create a memory diagram and walk through the problem to show the correct processing steps. The problem then asks you what would happen if something were changed in the design. The are many solutions to this problem depending upon where the new statements are placed. For example one of the steps asks what would happen if something were placed inside the loop. There are many locations that are considered inside the loop and each location would produce a different result. You need to indicate to me where you placed the statement.
Problems 10, 11, 12 – These problems may be difficult to solve because they do not use input from a file.
Problem 19 - This is the
best problem to start with because it is similar to the problems from Chapter
3. It asks you to solve the problem using modules.
Modules
Concept of modules
- Module – takes a problem and breaks it into smaller manageable pieces. Each of the pieces is called a module. This allows you to concentrate on the overall processing the program needs to perform, instead of the being overwhelmed by the details early on. To solve a program using modules you will initially decide on the pieces of the program and solve of the pieces of the program one at a time.
- Benefits of modules in design –
- Complex program can be organized into smaller more manageable pieces.
- Programs can be modified easier than with non modular programs. It is relatively easy to maintain, or change, the program because modules can be changed or corrected individually without requiring revisions to other portions of the program.
- A collection of library modules can be created that can be used in other programs.
- Errors in logic can be quickly isolated and fixed.
- Modules can be developed and tested individually. A programmer can focus on one module at a time, without having to consider the big picture.
- Rules of modularization – NOTE: The rules are just guidelines for you to follow.
- Each module should have a single function.
- Each module must be a manageable size; it should have less than 50 program instructions.
- Each module has a single entry point and a single exit point.
- Structure chart – to show the overall modular structure, a structure chart (hierarchy chart) is used. A structure chart shows the flow of control within a program. It shows the relationship between all modules within a program. A hierarchy chart portrays a module as a rectangular box in which the module name is placed. The structure chart is created from a general level to a more specific lever. It shows the order and structures in which these modules are executed.
- Calling and the called module – a main routine (Overall control), or calling module should be created. This routine executes all other modules and contains the entry and exit points for the program. The other modules are called the called modules. They represent modules that may be given control during program execution. When the steps in the called modules are finished, then control is returned back to the calling module. It is possible for a module to be both the calling module and the called module. The modules are named and can also be labeled to indicate level they are on in the structure chart and the order to perform them in.
NOTE: In this example, the Overall control module is the calling module. The module calls the Initialization, Process a Record and Termination modules. The Initialization, Process a Record and Termination modules are the called modules. Once the computer have finished processing the modules, processing returns to the Overall Control module.
Initially the problem can be broken down into three parts: Initialization, Process a record and Termination. Almost all programs that the students will solve contains a main processing section of the program that separates the main processing of the program from the initialization and termination sections.Initialization – these steps are performed before records are processed. Some of the steps that can be performed are:
- Set accumulator to 0
- Set counter to 0
- Initialize switches
- Write headings
Process a record – these steps perform nearly identical processing for each input record. This module contains all the processing steps that are necessary to process one input record. These steps are performed inside the loop.Termination – these steps are performed after the records are processed. Some of the steps that can be performed are:
- Write total lines
- Process overall averages
Once the problem is broken down into these three steps, each of these steps may be broken into smaller modules. For example: in the termination module There is processing to calculate totals and averages and also output totals. These functions can be broken into two smaller modules.