repetition control structure lecture 5. repetition using the dowhile structure three different ways...

46
Repetition Control Structure Lecture 5

Post on 18-Dec-2015

218 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

Repetition Control Structure

Lecture 5

Page 2: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

Repetition using the DOWHILE structure

Three different ways that a set of instruction can be repeated:

1. Beginning of the loop (leading decision loop)

2. The end of the loop ( trailing decision loop)

3. A counted number of times (counted loop)

Page 3: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

Leading Decision Loop

DOWHILE condition p is trueStatement block

ENDDO

Page 4: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

Two Important consideration for Decision Loop

1. The testing of the condition is at the beginning of the loop programmer may need to perform initial processing

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

Page 5: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

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.

Page 6: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

• Defining diagram

Input Processing Output

F_temp(15 temperatures)

Get fahrenheit temperaturesConvert temperaturesDisplay Celcius temperaturesDisplay screen message

C_temp (15 temperatures)

Page 7: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

Solution Algorithm

• In this example, we need:– A DOWHILE structure– A counter, called temperature_count

initialised to zero

Page 8: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

Solution Algorithm (cont)

Fahrenheit_Celcius_conversion1 Set temperature_count to zero2 DOWHILE temperature_count < 153 Promt operator for f_temp4 Get f_temp5 Compute c_temp = (f_temp - 32) * 5/96 Display c_temp7 Add 1 to temperature_count

ENDDO8 Display „All temperatures processed“ to the screen

END

Page 9: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

Desk Checking

Data Set 1 Data Set 2

F_temp 32 50

1. Input Data

Page 10: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

Data Set 1 Data Set 2

C_temp 0 10

2. Expected Result

Page 11: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

3. Desk Check Table

Statement number

Temperature_count

DOWHILE condition

F_temp C_temp

1 0

2 True

3,4 32

5 0

6 Display

7 1

2 True

3,4 50

5 10

6 Display

7 2

Page 12: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

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.

Page 13: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

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.

Page 14: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

• Defining diagram

Input Processing Output

NameExam_score

Read student detailsPrint student detailsCompute average scorePrint average score

NameExam_scoreAverage_score

Page 15: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

We need:

• A DOWHILE structure

• An accumulator fo total score

• An accumulator for total students

Page 16: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

Solution AlgorithmPrint_examination_scores

1 Set total_score to zero2 Set total_students to zero3 Read name, exam_score4 DOWHILE exam_score not = 9995 Add 1 to total_students6 Print name, exam_score7 Add exam_score to total_score8 Read name, exam_score

ENDDO9 IF total_students not = zero THEN

average_score = total_score/total_studentsPrint average_score

ENDIFEND

Priming Read

Page 17: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

Desk Checking

Record 1 Record 2 Record 3

score 50 100 999

1. Input Data

Page 18: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

2. Expected Result

• First name and score of 50

• Second name and score of 100

• Average score 75

Page 19: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

3. Desk Check Table

Statement number

Total_Score Total_

Students

Exam_

score

DOWHILE condition

Average_

score

1,2 0 0

3 50

4 true

5 1

6 print

7 50

8 100

4 True

5 2

6 Print

7 150

8 999

4 False

9 75

print

Page 20: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

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

Page 21: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

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.

Page 22: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

• Defining diagram

Input Processing OutputStudent_record•Student_no•Name•Address•Postcode•Gender•Course_unit

Read student recordsSelect student recordsPrint selected recordsCompute total females enrolledCompute total males enrolledCompute total students enrolledPrint totals

Selected student recordsTotal_females_enrolledTotal_males_enrolledTotal_students_enrolled

Page 23: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

We need:

• A DOWHILE structure

• IF statements

• Accumulator for 3 total fields

Page 24: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

Solution AlgorithmProcess_students_enrolments

1 Set total_females_enrolled to zero2 Set total_males_enrolled to zero3 Set total_students_enrolled to zero4 Read student record5 DOWHILE record exist6 IF course_unit = TEL104 THEN

print student detailsincrement total_students_enrolledIF student_gender = female THEN

increment total_females_enrolledELSE

increment total_males_enrolledENDIF

ENDIF7 Read student record

ENDDO8 Print total_females_enrolled9 Print total_males_enrolled10 Print total_students_enrolled

END

Page 25: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

Desk Checking

Record 1 Record 2 Record 3

Course unit TIK100 TEL104 TEL104

gender F F M

1. Input Data

Page 26: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

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

Page 27: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

3. Desk Check Table

Statement number

Course_

unit

gender DOWHILE condition

Total_

Females_

enrolled

Total_

Males_

enrolled

Total_

Students_

enrolled

1,2,3 0 0 0

4 TIK100 F

5 True

6

7 TEL104 F

5 True

6 print print 1 1

7 TEL104 M

5 True

6 print print 1 2

7 EOF

5 False

8,9,10 print print print

Page 28: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

Conclusion

• Basic pattern for DOWHILE to process sequential file:

Process_sequential_fileInitial processingRead first recordDOWHILE more record exist

Process this recordProcess next record

ENDDOFinal processing

END

Page 29: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

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:

REPEATStatementStatement...

UNTIL the condition is true

Page 30: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

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.

Page 31: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

DOWHILE vs REPEAT..UNTIL

Process_students_recordsSet student_count to zeroRead student recordDOWHILE student_number NOT = 999

Write student recordIncrement student_countRead student record

ENDDOPrint student_count

END

DOWHILE

Page 32: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

DOWHILE vs REPEAT..UNTIL (cont‘)

Process_students_recordsSet student_count to zeroREPEAT

Read student recordIF student_number NOT = 999 THEN

Write student recordIncrement student_count

ENDIFUNTIL student_number = 999Print student_count

END

REPEAT..UNTIL

Page 33: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

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.

Page 34: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

• Defining diagram

Input Processing OutputInventory_record•item_number•Item_description•Stock_figure

Read inventory recordsSelect low stock itemsPrint low stock recordsPrint total low stock items

HeadingSelected records•item_number•Item_description•Stock_figureTotal_low_stock_items

Page 35: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

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

Page 36: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

Solution AlgorithmProcess_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

Page 37: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

Desk Checking

Record 1 Record 2 Record 3

Item_number 123 124 0

Stock_figure 8 25

1. Input Data

Page 38: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

2. Expected Result

• Low Stock Items

• 123 8 (first record)

• Total Low Stock Item = 1

Page 39: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

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.

Page 40: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

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.

Page 41: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

• Defining diagram

Input Processing Output

F_temp(15 temperatures)

Get fahrenheit temperaturesConvert temperaturesDisplay Celcius temperaturesDisplay screen message

C_temp (15 temperatures)

Page 42: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

We Need:

• DO loop

• a loop counter (temperature_count)

Page 43: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

Solution Algorithm

Fahrenheit_Celcius_conversion1 DO temperature_count = 1 to 152 Promt operator for f_temp3 Get f_temp4 Compute c_temp = (f_temp - 32)* 5/95 Display c_temp

ENDDO6 Display „All temperatures processed“ to the

screenEND

Page 44: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

Desk Checking

Data Set 1 Data Set 2

F_temp 32 50

1. Input Data

Page 45: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

Data Set 1 Data Set 2

C_temp 0 10

2. Expected Result

Page 46: Repetition Control Structure Lecture 5. Repetition using the DOWHILE structure Three different ways that a set of instruction can be repeated: 1.Beginning

3. Desk Check Table

Statement number Temperature_count F_temp C_temp

1 1

2,3 32

4 0

5 Display

1 2

2,3 50

4 10

5 display