Title: Lecture Notes 91003
1Lecture Notes 9/10/03
- Repetition Control Structures
2Repetition Using the DOWHILE stucture
- We already talked a bit about repetition
structures in chapter 2. - Now well deal with them a bit more formally.
- General Form
- DOWHILE condition is true
- statement block
- ENDDO
3Repetition Using the DOWHILE stucture cont
- The DOWHILE is a leading decision loop.
Therefore, the following processing takes place - The logical condition is tested
- If the condition is true, the statements within
the statement block are executed once. Control
then returns to the top of the loop to be
re-tested. - If the condition is false, control passes to the
next statement AFTER the ENDDO.
4Repetition Using the DOWHILE stucture cont
- Some important notes
- Students often confuse DOWHILE structures with IF
THEN structures. - Remember that the only real difference between
them is that when the statement block is finished
control goes to - The next statement after ENDIF for an IF THEN
structure. - The condition at the top of the loop for a
DOWHILE structure. - If nothing is done within the loop to eventually
modify the tested condition, the loop will
repeatedly execute forever. This is called an
infinite loop. - Note Oftentimes your program will seem to
freeze, but it may be in an infinite loop. A
quick way to see if thats the case, is to put a
COUTltltloopltltendl statement within your loop.
5Example 5.1 (pg. 51)
- A weather station receives 15 temperatures
expressed in Fahrenheit each day. A program
needs to be written that will accept the 15
temperatures, convert them to Celcius, and
display those values on the screen. - Once all the temperatures have been processed,
display the words All Temperatures Processed
on the screen.
6Example 5.1 cont
- First, lets create a defining diagram
Input
Processing
Output
F_temp
Get Fahrenheit temperatures Convert
temperatures Display Celcius temperatures Display
screen message
C_temp
7Example 5.1 cont
- Now, we can work through the solution algorithm
- Fahrenheit_Celcius_Conversion
- Set temperature_count to zero
- DOWHILE temperature_count lt15
- Prompt operator for 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
8Desk Checking Example 5.1
- Lets give our algorithm two valid sets of data
Input Data
First Data Set
Second Data Set
32
50
Output Data
First Data Set
Second Data Set
0
10
9Desk Checking Example 5.1 cont
- What we need to do is make sure that at each
step, we are not making a mistake in our
algorithm
Fahrenheit_Celcius_Conversion Set
temperature_count to zero...
temperature_count 0 DOWHILE temperature_count
lt15 Prompt operator for f_tempF_temp32
compute c_temp (f_temp 32) 5/9. C_temp
0 Display c_temp Add 1 to
temperature_count ENDDO Display All
temperatures processed to the screen END
10Example 5.2
- A program is to be written that gets and displays
a series of names and exam scores for students
enrolled in a mathematics course. The class
average is to be computed and displayed at the
end of the report. Scores can range from 0
to100. The last record contains a blank name and
a score of 999 and is not to be displayed or
included in the calculations.
11Example 5.2 cont
- Lets first draw a defining diagram
12Example 5.2 cont
- As you begin to think about how to write out the
solution algorithm, you may take note of a few
things - The condition of the loop is that the score
entered is not 999 (called a sentinel) - Youll need a variable (accumulator) to store the
running total of the scores - Youll need a variable (accumulator) to store the
running total of student scores entered
13Example 5.2 cont
- Lets write out the solution algorithm
- Display_examination_scores
- Set total_score to zero
- set total_students to zero
- get name, exam_score
- DOWHILE exam_score ltgt 999
- add 1 to total_students
- display name, exam_score
- compute total_score total_score exam_score
- get name, exam score
- ENDDO
- IF total_students ltgt zero THEN
- average_score total_score/total_students
- display average_score
- ENDIF
- END
14Example 5.3
- This is a very similar problem to the last one
except that there is no sentinel or trailer
record. - On example 5.2, I did the algorithm as if the
records were coming from the keyboard. - For this example, however, records are coming
from a file. - File input/output will be covered later in the
semester, but in psuedocode the only difference
is that we typically use the keyword READ
instead of GET and WRITE instead of
DISPLAY when dealing with files.
15Example 5.3 cont
- A program is required which will read a file of
student records, and select and print only those
student records for students enrolled in a course
called Programming I whose unit number is
18500. - Each student record contains
- Student_number
- Name
- Address
- Zipcode
- Gender
- Course_unit_number
16Example 5.3 cont
- Lets do a defining diagram
17Example 5.3 cont
- A few quick notes before diving into the solution
algorithm - There is no sentinel, but we can look to see if
there are anymore records in the file in our
condition - For example
- DOWHILE more data
- DOWHILE more records
- Well need accumulators for the total number of
females, males, and students.
18Example 5.3 cont
- Solution Algorithm
- Process_student_enrollments
- Set total_females to zero
- Set total_males to zero
- Set total_students to zero
- Read student record
- DOWHILE more data
- IF course_unit 18500 THEN
- print student details
- compute total_students total_students 1
- IF gender female THEN
- compute total_females total_females 1
- ELSE
- compute total_males total_males 1
- ENDIF
- ENDIF
- Read student record
- ENDDO
- print total_females
19Summary
- You should have read and understood chapters 1-5
in Simple Program Design by now. - Wednesday is our first exam. It will encompass
chapters 1-5 in Simple Program Design. - There will be NO C (or any other actual
programming language) on this exam. Answers in
actual code will be awarded no credit. - If you are struggling with this material, I
highly recommend going to the free tutoring
sessions in the Thompson building (see my home
page). I know the tutor and she is VERY bright. - Questions?