PATH: Instructional Server> COP 2000> Examples> Accumulating Loop>

Text-based Documentation for Sentinel Controlled Accumulating Loops


This web page contains text-based alternatives to the graphic-based elements (flowcharts and columnar tables) shown in the web page An Example of an Accumulation Problem Involving Repetition Structure. That page offers examples of two different control structures, "leading decision" repetition and "trailing decision" repetition. The first two items below on this web page relate to the leading decision loop example. The final two items relate to the trailing decision loop example.

Leading Decision Loop Algorithm Outline (alternative to a flowchart)

It is important to clearly describe logical control structures that involve branching away from the simple sequential flow of steps and the conditions upon which they are based. Outlines are well suited to this task because of the hierarchical nature of their lettered and numbered steps. Analysts often employ indentation using tabs or spaces to help clarify step subordination. If your rendering software cannot indicate the presence of such "whitespace" characters, be give careful attention to the labeling of the steps to determine the subordination of the steps.

Note: in the document below, "[CR]" represents the carriage return and/or line feed sequence necessary to start a new line of output.

A. Start "Leading Decision Accumulating Loop Example"
B. Assign the value 0 into T.("Initialization of the accumulator")
C. Request and store the initial value for the Price.
   C.1. Prompt for the price.
        C.1.a. Display "Item price (" without [CR].
        C.1.b. Display the value of SENT without [CR].
        C.1.c. Display " to stop)? " and a [CR].
   C.2. Read and store P from the keyboard ("Prime Read")
D. While P is not equal to SENT ("Test for passage/exit"), do a loop pass:
   D.1. Increase T by the value in P. ("Loop body")
   D.2. Prompt for the price.
        D.2.a. Display "Item price (" without [CR].
        D.2.b. Display the value of SENT without [CR].
        D.2.c. Display " to stop)? " and a [CR].
   C.2. Read and store P from the keyboard ("Next Read")
   D.3. Go back to loop test. ("Loopback")
E. Display the results.
   E.1. Display a [CR].
   E.2. Display "Total price of purchase: $" without [CR].
   E.3. Display T rounded to 2 decimal places.
F. End

Note that the leading decision approach above requires the use of a "prime read" ahead of the loop entry to ensure that there is a value stored in P before the "sentinel test". The variable P must acquire a new value at the bottom of the loop's "pass" so that it will have another value to test at the start of the next pass. This redundancy is a required element when we choose a leading decision structure in a sentinel controlled loop.

Leading Decision Loop Tracing List (alternative to a columnar Tracing Chart)

The list below is an alternative to the traditional columnar Tracing Chart used to track the values of data items as they are stored in main memory and to track the true or false results of conditional tests within an algorithm. Each line below indicates a change to one of those items in the format "Identifier or condition: value or result". The order of the lines below indicates the order in which the changes occurred. These should be studied in combination with the Test Softcopies shown on the An Example of an Accumulation Problem Involving Repetition Structure.

T: 0.0
P: 5.25
P not = SENT: True
T: 5.25
P: 2.15
P not = SENT: True
T: 7.40
P: 3.40
P not = SENT: True
T: 10.80
P: 1.20
P not = SENT: True
T: 12.00
P: -1
P not = SENT: False

Trailing Decision Loop Algorithm Outline (alternative to a flowchart)

It is important to clearly describe logical control structures that involve branching away from the simple sequential flow of steps and the conditions upon which they are based. Outlines are well suited to this task because of the hierarchical nature of their lettered and numbered steps. Analysts often employ indentation using tabs or spaces to help clarify step subordination. If your rendering software cannot indicate the presence of such "whitespace" characters, be give careful attention to the labeling of the steps to determine the subordination of the steps.

Note: in the document below, "[CR]" represents the carriage return and/or line feed sequence necessary to start a new line of output.

A. Start "Trailing Decision Accumulating Loop Example"
B. Assign the value 0 into T.("Initialization of the accumulator")
C. Do a loop pass:
   C.1. Prompt for the price.
        C.1.a. Display "Item price (" without [CR].
        C.1.b. Display the value of SENT without [CR].
        C.1.c. Display " to stop)? " and a [CR].
   C.2. Read and store P from the keyboard ("Prime Read")
   C.3. Test condition: P is not equal to SENT ("Guard the body")
	      C.3.a. If so, then increase T by the value in P. ("Loop body")
   C.4. If P is not equal to SENT ("Test for passage/exit"), do another pass. ("Loopback")
D. Display the results.
   D.1. Display a [CR].
   D.2. Display "Total price of purchase: $" without [CR].
   D.3. Display T rounded to 2 decimal places.
E. End

Note that the trailing decision approach above requires the use of a "guarding selection" ahead of the loop body to ensure that the "sentinel value" does affect the processes being performed by the loop body (in this situation, the accumulation). This additional test is a required element when we choose a trailing decision structure in a sentinel controlled loop.

Trailing Decision Loop Tracing List (alternative to a columnar Tracing Chart)

The list below is an alternative to the traditional columnar Tracing Chart used to track the values of data items as they are stored in main memory and to track the true or false results of conditional tests within an algorithm. Each line below indicates a change to one of those items in the format "Identifier or condition: value or result". The order of the lines below indicates the order in which the changes occurred. These should be studied in combination with the Test Softcopies shown on the An Example of an Accumulation Problem Involving Repetition Structure.

T: 0.0
P: 5.25
P not = SENT (Guard): True
T: 5.25
P not = SENT (Loopback): True
P: 2.15
P not = SENT (Guard): True
T: 7.40
P not = SENT (Loopback): True
P: 3.40
P not = SENT (Guard): True
T: 10.80
P not = SENT (Loopback): True
P: 1.20
P not = SENT (Guard): True
T: 12.00
P not = SENT (Loopback): True
P: -1
P not = SENT (Guard): False
P not = SENT: False
PATH: Instructional Server> COP 2000> Examples> Accumulating Loop>