1 / 15

Repetition Control Structures

5. Repetition Control Structures. Simple Program Design Third Edition A Step-by-Step Approach. 5. Objectives. To develop algorithms which use the DOWHILE and REPEAT…UNTIL control structures To introduce a pseudocode structure for counted repetition loops

aren
Download Presentation

Repetition Control Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 5 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

  3. 5 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

  4. 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. 5 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

  6. 5 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

  7. 5 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

  8. 5 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)

  9. 5 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

  10. 5 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

  11. 5 Repetition Using the Repeat…Until Structure • The format of the REPEAT…UNTIL structure is: REPEAT statement 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

  12. 5 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

  13. 5 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

  14. 5 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

  15. 5 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

More Related