520 likes | 1.22k Views
Repetition Control Structure. Lecture 5. Repetition using the DOWHILE structure. Three different ways that a set of instruction can be repeated: Beginning of the loop (leading decision loop) The end of the loop ( trailing decision loop) A counted number of times (counted loop).
E N D
Repetition Control Structure Lecture 5
Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: • Beginning of the loop (leading decision loop) • The end of the loop ( trailing decision loop) • A counted number of times (counted loop)
Leading Decision Loop DOWHILE condition p is true Statement block ENDDO
Two Important consideration for Decision Loop • The testing of the condition is at the beginning of the loop programmer may need to perform initial processing • The only way to terminate the loop is to render the DOWHILE condition false need to set up some process within the statement block that will change the condition to be false.
Example 5.1 Fahrenheit-Calcius Conversion • Every day, a weather station receives 15 temperatures expressed in degrees Fahrenheit. A program is to be written that will accept each Fahrenheit temperature, convert it to Celcius and display the converted temperature to the screen. After 15 temperaturs have been processed, the words „All temperatures processed“ are to be displayed to the screen.
Solution Algorithm • In this example, we need: • A DOWHILE structure • A counter, called temperature_count initialised to zero
Solution Algorithm (cont) Fahrenheit_Celcius_conversion 1 Set temperature_count to zero 2 DOWHILE temperature_count < 15 3 Promt operator for f_temp 4 Get f_temp 5 Compute c_temp = (f_temp - 32) * 5/9 6 Display c_temp 7 Add 1 to temperature_count ENDDO 8 Display „All temperatures processed“ to the screen END
Desk Checking 1. Input Data
Using DOWHILE to Repeat Unkown Number of Times • We cannot use counter • Instead, we use : trailer record or sentinel • Sentinel : special record or value placed at the end of valid data to signify the end of that data.
Example 5.2 Print examination scores A program is required to read and print a series of names and exams scores for student enrolled in fundamental programming course. The class average is to be computed and printed at the end of the report. Scores can range from 0 to 100. The last record contains a blank name and a score of 999 and is not to be included in the calculations.
We need: • A DOWHILE structure • An accumulator fo total score • An accumulator for total students
Solution Algorithm Print_examination_scores 1 Set total_score to zero 2 Set total_students to zero 3Read name, exam_score 4 DOWHILE exam_score not = 999 5 Add 1 to total_students 6 Print name, exam_score 7 Add exam_score to total_score 8 Read name, exam_score ENDDO 9 IF total_students not = zero THEN average_score = total_score/total_students Print average_score ENDIF END Priming Read
Desk Checking 1. Input Data
2. Expected Result • First name and score of 50 • Second name and score of 100 • Average score 75
When a trailer record or sentinel does not exist • We need to check for EOF (End Of File) Marker • EOF marker is added when the file is created as the last character in the file. • The check of EOF is in the DOWHILE clause, using one of the following expression: • DOWHILE more data • DOWHILE more records • DOWHILE records exist • DOWHILE NOT EOF
Example 5.3 Process Student Enrolments A program is required to read a file of student records, and select and print only those students enrolled in a course unit named Fundamental Programming. Each student record contains student number, name, address, postcode, gender, and course unit number. The course unit number for Fundamental Programming is TEL104. Three totals are to be printed at the end of the report: total females enrolled in the course, total males enrolled in the course, and total students enrolled in the course.
We need: • A DOWHILE structure • IF statements • Accumulator for 3 total fields
Solution Algorithm Process_students_enrolments 1 Set total_females_enrolled to zero 2 Set total_males_enrolled to zero 3 Set total_students_enrolled to zero 4 Read student record 5 DOWHILE record exist 6 IF course_unit = TEL104 THEN print student details increment total_students_enrolled IF student_gender = female THEN increment total_females_enrolled ELSE increment total_males_enrolled ENDIF ENDIF 7 Read student record ENDDO 8 Print total_females_enrolled 9 Print total_males_enrolled 10 Print total_students_enrolled END
Desk Checking 1. Input Data
2. Expected Result • Student number, name, address, postcode, F (2nd student) • Student number, name, address, postcode, M (3rd student) • Total females enrolled 1 • Total males enrolled 1 • Total students enrolled 2
Conclusion • Basic pattern for DOWHILE to process sequential file: Process_sequential_file Initial processing Read first record DOWHILE more record exist Process this record Process next record ENDDO Final processing END
REPEAT...UNTIL STRUCTURE • DIFF: • DOWHILE test the condition at the beginning of the loop • REPEAT..UNTIL test the condition at the end of the loop • REPEAT..UNTIL Structure: REPEAT Statement Statement . . . UNTIL the condition is true
Consideration of using REPEAT..UNTIL • REPEAT..UNTIL is a trailing loop the statements are excuted before the condition is tested. • REPEAT..UNTIL loops are executed when the condition is false. (opposite of DOWHILE) Example: DOWHILE more records equivalent to REPEAT..UNTIL no more records DOWHILE number NOT = 99 equivalent to REPEAT..UNTIL number=99 • The statements within a REPEAT..UNTIL structure will always be executed at least once. no need priming Read, but extra IF statement needed after the READ to prevent the processing the trailer record.
DOWHILE vs REPEAT..UNTIL DOWHILE Process_students_records Set student_count to zero Read student record DOWHILE student_number NOT = 999 Write student record Increment student_count Read student record ENDDO Print student_count END
DOWHILE vs REPEAT..UNTIL (cont‘) REPEAT..UNTIL Process_students_records Set student_count to zero REPEAT Read student record IF student_number NOT = 999 THEN Write student record Increment student_count ENDIF UNTIL student_number = 999 Print student_count END
Example 5.4 Process Inventory Items A program is required to read a series of inventory records that contain item number, item description and stock figure. The last record in the file has an item number of zero. The program is to produce a low stock item report, by printing only those records that have a stock figure of less than 20 items. A heading is to be printed at the top of the report and a total low stock item count printed at the end.
We need: • A REPEAT..UNTIL structure • IF statements to select stock figures < 20 • Accumulator for total_low_stock_items • Extra IF, within the loop, to ensure the trailer record is not processed
Solution Algorithm Process_inventory_records 1 Set total_low_stock_items to zero 2 Print ‚Low Stock Items‘ heading REPEAT 3 Read inventory record 4 IF item_number > zero THEN IF stock_figure < 20 THEN print item_number, item_description, stock_figure increment total_low_stock_items ENDIF ENDIF 5 UNTIL item_number = zero 6 Print total_low_stock_items END
Desk Checking 1. Input Data
2. Expected Result • Low Stock Items • 123 8 (first record) • Total Low Stock Item = 1
Counted Repetition • Structure: DO loop_index = initial_value to final_value statement block ENDDO • This structure will: • Perform the initialising • Incrementing and testing the loop counter automatically • Terminate th loopwhen the required number of repetition has been executed.
Example 5.5 Fahrenheit-Calcius Conversion • Every day, a weather station receives 15 temperatures expressed in degrees Fahrenheit. A program is to be written that will accept each Fahrenheit temperature, convert it to Celcius and display the converted temperature to the screen. After 15 temperaturs have been processed, the words „All temperatures processed“ are to be displayed to the screen.
We Need: • DO loop • a loop counter (temperature_count)
Solution Algorithm Fahrenheit_Celcius_conversion 1 DO temperature_count = 1 to 15 2 Promt operator for f_temp 3 Get f_temp 4 Compute c_temp = (f_temp - 32)* 5/9 5 Display c_temp ENDDO 6 Display „All temperatures processed“ to the screen END
Desk Checking 1. Input Data