Simple Program Design, 4Th. Ed., L. Robertson

Total Page:16

File Type:pdf, Size:1020Kb

Simple Program Design, 4Th. Ed., L. Robertson

Simple Program Design, 4th. Ed., L. Robertson

Chapter 5 – Repetition Control Structures

There are three different ways that a set of instructions can be repeated, and each way is determined by where the decision to repeat is placed:

 at the beginning of the loop (leading decision loop)  at the end of the loop (trailing decision loop)  a counted number of times (counted loop)

5.1 Repetition Using the DOWHILE Structure

Leading Decision Loop

The DOWHILE construct is a leading decision loop – that is, the condition is tested before any statements are executed.

General format:

DOWHILE condition p is true Statement block ENDDO

There are two important considerations about which you must be aware before designing a DOWHILE loop.

First, the testing of the condition is at the beginning of the loop.

Second, the only way to terminate the loop is to render the DOWHILE condition false.

1 Simple Program Design, 4th. Ed., L. Robertson

Chapter 5 – Repetition Control Structures

5.1 Repetition Using the DOWHILE Structure-- continued

There are two ways to utilize this structure.

5.1.1. Using DOWHILE to Repeat a Set of Instructions a Known Number of Times

When a set of instructions is repeated a specific number of times, a counter can be used in pseudocode.

For this construct two actions must be indicated:

1. The counter is initialized before the DOWHILE statement and

2. The counter is incremented before the ENDDO statement.

Examine the program statement and the appropriate solution illustrated in Example 5.1on pages 56 and 57 of the textbook.

2 Simple Program Design, 4th. Ed., L. Robertson

Chapter 5 – Repetition Control Structures

5.1 Repetition Using the DOWHILE Structure-- continued

5.1.2. Using DOWHILE to Repeat a Set of Instructions an Unknown Number of Times

5.1.2.1 When a trailer record or sentinel exists

Often, a trailer record or sentinel signifies the end of the data.

This sentinel is a special record or value placed at the end of valid data to signify the end of that data.

It must contain a value that is clearly distinguishable from the other data to be processed.

5.1.2.2 When a trailer record does not exist – the most common approach

When there is no trailer record to signify the end of the data, the programmer needs to check for an end-of-file marker (EOF).

This EOF marker is added when the file is created, as the last character in the file.

The check for EOF is positioned in the DOWHILE clause, using one of the following equivalent expressions:

DOWHILE more data DOWHILE more records DOWHILE records exist DOWHILE NOT EOF

Examine the program statements and the appropriate solutions illustrated in Examples 5.2 and 5.3 on pages 58 through 63 of the textbook.

3 Simple Program Design, 4th. Ed., L. Robertson

Chapter 5 – Repetition Control Structures

5.2 Repetition Using the REPEAT…UNTIL Structure

Trailing Decision Loop

The REPEAT…UNTIL structure is similar to the DOWHILE…ENDDO structure, in that a group of statements are repeated in accordance with a specified condition.

However, where the DOWHILE…ENDDO structure tests the condition at the beginning of the loop; a REPEAT…UNTIL structure tests the condition at the end of the loop.

The REPEAT…UNTIL is a trailing decision loop; the statements are executed once before the condition is tested.

There are two other considerations about which you need to be aware before using REPEAT…UNTIL.

1. REPEAT…UNTIL loops are executed when the condition is false; it is only when the condition becomes true that repetition ceases.

2. The statements within a REPEAT…UNTIL structure will always be executed at least once.

General formats:

REPEAT DOUNTIL condition p is true Statement block OR Statement block UNTIL condition p is true ENDDO

Examine the program statement and the appropriate solution illustrated in Example 5.4 on pages 65 through 67 of the textbook.

4 Simple Program Design, 4th. Ed., L. Robertson

Chapter 5 – Repetition Control Structures

5.3 Counted Repetition

Counted Loop

Counted repetition occurs when the exact number of loop iterations is known in advance.

The execution of the loop is controlled by a loop index.

General format:

DO loop-index = initial-value to final-value Statement block ENDDO

The DO loop does more than just repeat the statement block.

The DO…ENDDO loop functions:

1. Initializes the “loop-index” to the “initial-value”

2. Increments the “loop-index” by 1 for each pass through the loop

3. Tests the value of the “loop-index” at the beginning of each pass through the loop to ensure it is in the stated range of values

4. Ends the loop when the “loop-index” is greater than the “final-value”

Examine the program statement and the appropriate solution illustrated in Example 5.5 on pages 68 and 69 of the textbook.

5

Recommended publications