Chapter 4 Assignment Page 1

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

To create a properly formed DOWHILE loop you must:
    1. Place the loop test before any other step within the loop (leading decision).
    2. Place the loop steps in the "yes" path of the loop test.
    3. Indicate that the loop will exit in the "no" path of the loop test.
Pseudocode Example:

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:

To create a properly formed DOUNTIL loop you must:
    1. Place the loop test after all steps within the loop (trailing decision).
    2. Place the loop steps in the "no" path of the loop test.
    3. Indicate that the loop will exit in the "yes" path of the loop test.
Pseudocode Example:

wpe7.gif (1037 bytes)

Flowchart example:

wpeC.gif (3693 bytes)

Week 12 Course Outline
2. Counter controlled loop NOTE: Accumulators and counters are created using variables. The variables need to be initialized before they can be used. If they are used to hold a total, they are usually initialized to 0. This gives the variable location a starting value, otherwise you are adding to a preexisting value and will not receive the correct result. Some programming languages initialize variables to 0 automatically, but as a rule you should always initialize the variables.

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.
 
 

The preparation symbol is used to initialize a variable and place a limit on a loop-control variable.

Preparation Symbol -

wpe2.gif (1270 bytes)

A counter controlled loop uses a counter to tell the computer system when it is time to exit the loop. With a counter controlled loop, a counter is created using a variable. The counter is either initialized to 0 or to the total of times to perform the loop. If the counter is initialized to 0, each time through the loop, the counter is incremented by 1and compared to the total of times the loop should be performed. Once the counter value equals the total number of time the loop should be performed, the loop is exited. If the counter is initialized to the total number of time to perform the loop, each time through the loop, the counter is decremented by 1 and compared to 0. Once the counter value equals 0, the loop is exited. This type of loop is used when the number of input records or times to perform the loop in known and will not change. If the number of input records changes the programming statements must be changed. NOTE: The problem below, walks through an example of a counter controlled loop that will simply read a record and print the output. This problem will allow you to focus on the loop and not processing the problem. Walk through the steps the computer will perform using a memory diagram.

Input File

Kendall Boulton
Charleen Thompson
Carol Knox
Michael Worthington
Warren Smith
 

Pseudocode Example:

wpe9.gif (1037 bytes)

Flowchart Example:
 
 

Exercises:

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

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.

wpeE.gif (2561 bytes)

  1. Problems using modules
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:

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.

IST 153 Home Page | Syllabus | Week 6 Course Outline