Presentation is loading. Please wait.

Presentation is loading. Please wait.

Repetition Control Structures Simple Program Design Third Edition A Step-by-Step Approach 5.

Similar presentations


Presentation on theme: "Repetition Control Structures Simple Program Design Third Edition A Step-by-Step Approach 5."— Presentation transcript:

1

2 Repetition Control Structures Simple Program Design Third Edition A Step-by-Step Approach 5

3 Objectives To develop algorithms which use the DOWHILE and REPEAT…UNTIL control structures To introduce a pseudocode structure for counted repetition loops To develop algorithms using variations of the repetition construct 5

4 Repetition Using the DOWHILE Structure Most programs require the same logic to be repeated for several sets of data The most efficient way to deal with this situation is to establish a looping structure in the algorithm that will cause the processing logic to be repeated a number of times The DOWHILE structure will continue to repeat a group of statements while a condition remains true 5

5 Repetition Using the DOWHILE Structure 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 5

6 Example 5.1 Fahrenheit-Celsius Conversion A program is to be written which will accept each Fahrenheit temperature, convert it to Celsius, and display the converted temperature to the screen. After 15 temperatures have been processed, the words ‘All temperatures processed’ are to be displayed on the screen A Defining diagram (see figure at top of page 52 of the textbook) The defining diagram still only lists what needs to be done; the equation to convert the temperature will not need to be known until the algorithm is developed 5

7 Example 5.1 Fahrenheit-Celsius Conversion Having defined the input, output, and processing, you are ready to outline a solution to the problem In this example you need: –A DOWHILE structure to repeat the necessary processing –A counter, initialized at zero, which will control the fifteen repetitions B Solution algorithm This solution algorithm could also have been expressed using the keywords WHILE…DO and ENDWHILE 5

8 Example 5.1 Fahrenheit-Celsius Conversion Fahrenheit_Celsius_conversion Set temperature_count to zero DOWHILE temperature_count <15 Prompt operator for f_temp Get f_temp Compute c_tempt = (f_temp - 32) * 5/9 Display c_tempt Add 1 to temperature_count ENDDO Display ‘All temperatures processed’ to the screen END 5

9 Example 5.1 Fahrenheit-Celsius Conversion C Desk checking Although the program will require 15 records to process properly, it is still only necessary to check the algorithm at this stage with two valid sets of data (i) Input data: (see first figure on page 53 of the textbook) (ii) Expected results: (see second figure on page 53 of the textbook) 5

10 Example 5.1 Fahrenheit-Celsius Conversion (iii) Desk check table Desk checking this algorithm shows the exact processing of a DOWHILE loop There is some initial processing (first statement), which will be executed only once Then the DOWHILE condition is tested and found to be true 5

11 Repetition Using the Repeat…Until Structure The REPEAT…UNTIL structure is similar to the DOWHILE structure, in that a group of statements are repeated in accordance with a specified condition However, where the DOWHILE structure tests the condition at the beginning of the loop, a REPEAT…UNTIL structure tests the condition at the end of the loop 5

12 Repetition Using the Repeat…Until Structure The format of the REPEAT…UNTIL structure is: REPEAT statement “ UNTIL condition is true The REPEAT…UNTIL is a trailing decision loop; the statements are executed once before the condition is tested The statements within a REPEAT…UNTIL structure will always be executed at least once 5

13 Counted Repetition Constructs 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, and instead of using DOWHILE, or REPEAT…UNTIL, the simple keyword DO is used as follows: DO loop_index = initial_value to final_value statement block ENDDO 5

14 Counted Repetition Constructs The DO loop does more than just repeat the statement block. It will: 1.Initialize the loop_index to the required initial_value 2.Increment the loop_index by 1 for each pass through the loop 3.Test the value of the loop_index at the beginning of each loop to ensure that it is within the stated range of values 4.Terminate the loop when the loop_index has exceeded the specified final_value 5

15 Summary This chapter covered the repetition control structure in detail Descriptions and pseudocode examples were given for DOWHILE, REPEAT…UNTIL, and counted repetition loops Most of the solution algorithms had the same general pattern 5

16 Summary This pattern consisted of: 1.Some initial processing before the loop 2.Some processing for each record within the loop 3.Some final processing once the loop has been exited 5


Download ppt "Repetition Control Structures Simple Program Design Third Edition A Step-by-Step Approach 5."

Similar presentations


Ads by Google