Repetition Control Structure - Jurusan Teknik Elektro dan

Download Report

Transcript Repetition Control Structure - Jurusan Teknik Elektro dan

Repetition Control Structure
Lecture 5
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)
Leading Decision Loop
DOWHILE condition p is true
Statement block
ENDDO
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.
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
F_temp
(15
temperatures)
Processing
Get fahrenheit temperatures
Convert temperatures
Display Celcius temperatures
Display screen message
Output
C_temp
(15 temperatures)
Solution Algorithm
• In this example, we need:
– A DOWHILE structure
– A counter, called temperature_count
initialised to zero
Solution Algorithm (cont)
1
2
3
4
5
6
7
8
Fahrenheit_Celcius_conversion
Set temperature_count to zero
DOWHILE temperature_count < 15
Promt operator for f_temp
Get f_temp
Compute c_temp = (f_temp - 32) * 5/9
Display c_temp
Add 1 to temperature_count
ENDDO
Display „All temperatures processed“ to the screen
END
Desk Checking
1. Input Data
F_temp
Data Set 1
Data Set 2
32
50
2. Expected Result
C_temp
Data Set 1
Data Set 2
0
10
3. Desk Check Table
Statement
number
1
Temperature_
count
DOWHILE
condition
F_temp
C_temp
0
2
True
3,4
32
5
0
6
Display
7
1
2
True
3,4
50
5
10
6
Display
7
2
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
Name
Exam_score
Processing
Read student details
Print student details
Compute average score
Print average score
Output
Name
Exam_score
Average_score
We need:
• A DOWHILE structure
• An accumulator fo total score
• An accumulator for total students
Solution Algorithm
1
2
3
4
5
6
7
8
9
Print_examination_scores
Set total_score to zero
Priming Read
Set total_students to zero
Read name, exam_score
DOWHILE exam_score not = 999
Add 1 to total_students
Print name, exam_score
Add exam_score to total_score
Read name, exam_score
ENDDO
IF total_students not = zero THEN
average_score = total_score/total_students
Print average_score
ENDIF
END
Desk Checking
1. Input Data
score
Record 1
Record 2
Record 3
50
100
999
2. Expected Result
• First name and score of 50
• Second name and score of 100
• Average score 75
3. Desk Check Table
Statement
number
1,2
Total_Score
Total_
Students
0
0
3
Exam_
score
true
5
1
6
print
50
8
100
4
True
5
2
6
7
8
4
9
Average_
score
50
4
7
DOWHILE
condition
Print
150
999
False
75
print
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
1
2
3
4
5
6
7
8
9
10
Process_students_enrolments
Set total_females_enrolled to zero
Set total_males_enrolled to zero
Set total_students_enrolled to zero
Read student record
DOWHILE record exist
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
Read student record
ENDDO
Print total_females_enrolled
Print total_males_enrolled
Print total_students_enrolled
END
Desk Checking
1. Input Data
Course unit
gender
Record 1
Record 2
Record 3
TIK100
TEL104
TEL104
F
F
M
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
1,2,3
4
TIK100
Total_
Females_
enrolled
Total_
Males_
enrolled
Total_
Students_
enrolled
0
0
0
F
5
True
6
7
TEL104
F
5
True
6
print
print
7
TEL104
M
5
print
7
EOF
8,9,10
1
True
6
5
1
print
1
2
print
print
False
print
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.
• Defining diagram
Input
Processing
Inventory_record
•item_number
•Item_description
•Stock_figure
Read inventory records
Select low stock items
Print low stock records
Print total low stock items
Output
Heading
Selected records
•item_number
•Item_description
•Stock_figure
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
2
Set total_low_stock_items to zero
Print ‚Low Stock Items‘ heading
REPEAT
Read inventory record
IF item_number > zero THEN
IF stock_figure < 20 THEN
print item_number, item_description, stock_figure
increment total_low_stock_items
ENDIF
ENDIF
UNTIL item_number = zero
Print total_low_stock_items
3
4
5
6
END
Desk Checking
1. Input Data
Record 1
Record 2
Record 3
Item_number
123
124
0
Stock_figure
8
25
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.
• Defining diagram
Input
F_temp
(15
temperatures)
Processing
Output
Get fahrenheit temperatures C_temp
(15 temperatures)
Convert temperatures
Display Celcius temperatures
Display screen message
We Need:
• DO loop
• a loop counter (temperature_count)
Solution Algorithm
1
2
3
4
5
6
Fahrenheit_Celcius_conversion
DO temperature_count = 1 to 15
Promt operator for f_temp
Get f_temp
Compute c_temp = (f_temp - 32)* 5/9
Display c_temp
ENDDO
Display „All temperatures processed“ to the screen
END
Desk Checking
1. Input Data
F_temp
Data Set 1
Data Set 2
32
50
2. Expected Result
C_temp
Data Set 1
Data Set 2
0
10
3. Desk Check Table
Statement number
1
Temperature_count
F_temp
C_temp
1
2,3
32
4
0
5
Display
1
2,3
2
50
4
10
5
display