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.
Defining diagram Input Processing Output F_temp (15 temperatures) Get fahrenheit temperatures Convert temperatures Display Celcius temperatures Display screen message C_temp
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 Data Set 1 Data Set 2 F_temp 32 50
2. Expected Result Data Set 1 Data Set 2 C_temp 10
3. Desk Check Table Statement number Temperature_count DOWHILE condition F_temp C_temp 1 2 True 3,4 32 5 6 Display 7 50 10
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.
Defining diagram Input Processing Output Name Exam_score Read student details Print student details Compute average score Print average score Average_score
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 3 Read 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 Record 1 Record 2 Record 3 score 50 100 999 1. Input Data Record 1 Record 2 Record 3 score 50 100 999
Second name and score of 100 Average score 75 2. Expected Result First name and score of 50 Second name and score of 100 Average score 75
3. Desk Check Table Statement number Total_Score Total_ Students Exam_ DOWHILE condition Average_ 1,2 3 50 4 true 5 1 6 print 7 8 100 True 2 Print 150 999 False 9 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.
Defining diagram Input Processing Output Student_record Student_no Name Address Postcode Gender Course_unit Read student records Select student records Print selected records Compute total females enrolled Compute total males enrolled Compute total students enrolled Print totals Selected student records Total_females_enrolled Total_males_enrolled Total_students_enrolled
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 7 Read student record ENDDO 8 Print total_females_enrolled 9 Print total_males_enrolled 10 Print total_students_enrolled END
Desk Checking Record 1 Record 2 Record 3 Course unit TIK100 TEL104 1. Input Data Record 1 Record 2 Record 3 Course unit TIK100 TEL104 gender F M
Student number, name, address, postcode, F (2nd student) 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
3. Desk Check Table Statement number Course_ unit gender DOWHILE condition Total_ Females_ enrolled Males_ Students_ 1,2,3 4 TIK100 F 5 True 6 7 TEL104 print 1 M 2 EOF False 8,9,10
Conclusion Basic pattern for DOWHILE to 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 . 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 Process_students_records Set student_count to zero Read student record DOWHILE student_number NOT = 999 Write student record Increment student_count ENDDO Print student_count END
DOWHILE vs REPEAT..UNTIL (cont‘) 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.
Defining diagram Input Processing Output Inventory_record item_number Item_description Stock_figure Read inventory records Select low stock items Print low stock records Print total low stock items Heading Selected records Total_low_stock_items
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 5 UNTIL item_number = zero 6 Print total_low_stock_items END
Desk Checking Record 1 Record 2 Record 3 Item_number 123 124 1. Input Data Record 1 Record 2 Record 3 Item_number 123 124 Stock_figure 8 25
Low Stock Items 123 8 (first record) Total Low Stock Item = 1 2. Expected Result Low Stock Items 123 8 (first record) Total Low Stock Item = 1
Total_low_stock_items 3. Desk Check Table Statement number Item_ number Stock_ figure REPEAT UNTIL Total_low_stock_items heading 1 2 Print 3 123 8 4 print 5 False 124 25 True 6
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.
Defining diagram Input Processing Output F_temp (15 temperatures) Get fahrenheit temperatures Convert temperatures Display Celcius temperatures Display screen message C_temp
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 Data Set 1 Data Set 2 F_temp 32 50
2. Expected Result Data Set 1 Data Set 2 C_temp 10
3. Desk Check Table Statement number Temperature_count F_temp C_temp 1 2,3 32 4 5 Display 2 50 10 display