Learning outcomes
Develop algorithms that use the
repetition control
structures(DOWHILEENDDO, REPEAT
UNTIL, DOTOENDDO)
Develop algorithms using variations of the
repetition construct
In this chapter, we learn differently. First Programming,
then Flowchart, lastly Pseudocode
Why use repetition?
Instead of repeatedly typing statements over
and over again, we just use repetition
structures to specify how many times we need
to repeat compound statements
Concepts to Look at &
Remember
From Implementation (Programming)
Perspectives
From Logical (Theory) Perspectives
Repeat definite, indefinitely
In general, we have two types of repetition as
we implement in programming
Definite Loop counter-controlled loop
I.
You know exactly how many times to repeat
Theres a initial starting value
Theres an ending value a limit
All these values are contained in a variable
commonly known as counter.
Repeat definite, indefinitely
(cont.)
Indefinite Loop permission-controlled loop
II.
You do not know how many times you need to
repeat
You ask for permission to continue
Theres NO initial starting value
Theres NO ending value NO limit
Hence, theres NO counter
You need a variable to keep the permission,
commonly known as a sentinel or trailer.
Repetition in C
Programming
Definite
Indefinite
for loop
for loop
while loop
while loop
do-while loop
do-while loop
Repetition in C
Programming
Counted Loop
Definite
for loop
Leading Decision
Loop
Indefinite
Trailing Decision
Loopfor loop
while loop
while loop
do-while loop
do-while loop
Repetition - Theory
There are three different ways that a set of
instructions can be repeated, and each way is
determined by where the decision to repeat is placed:
A. Leading Decision Loop
Condition at the beginning of the loop
B. Trailing Decision Loop
Condition at the end of the loop
C. Counted Loop
a counted number of times
Logical
Implementation
(Theory) (Programming)
Repetition - Theory
A. Leading decision loop
DOWHILE ENDDO
(indefinite) while loop
B. Trailing decision loop
REPEAT UNTIL
do-while loop
C. Counted loop
DO TO ENDDO
(definite) for loop
A) Leading Decision Loop
The DOWHILE construct is a leading decision loop
that is, the condition is tested before any statements
are executed
There are two important considerations of which you
must be aware before designing a DOWHILE loop
First, the testing of the condition is at the beginning of
the loop
Second, the only way to terminate the loop is to render
the DOWHILE condition false
A) Leading Decision Loop
(cont.)
DOWHILE condition p is true
statement block
ENDDO
DOWHILE P is true
Display It is true
ENDDO
Repeat with an unknown set of
times
1
When a trailer record or sentinel exists
When there are an unknown number of items to
process, you cannot use a counter, so another way of
controlling the repetition must be used
Often, a trailer record or sentinel signifies the end of the
data
This sentinel is a special record or value placed at
the end of valid data to signify the end of that
data
It must contain a value that is clearly distinguishable
from the other data to be processed
Example: Print Examination
Scores
A program is required to read and print a
series of names and exam scores for
students enrolled in a mathematics 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.
Example: Print Examination
Scores
(cont.)
Print_Exam_Score
Set total_score = 0, set total_students = 0
read name-exam_score
DOWHILE exam_score<> 999
Add 1 to total_student
print name, exam_score
add exam_score to total_score
read name. exam_score
ENDDO
.. please complete the rest
END
Repeat with an unknown set of
times (cont.)
When there is no trailer record or sentinel to signify
the end of the data, the programmer needs to check for
an end-of-life marker (EOF)
This EOF marker is added when the file is created, as the
last character in the file
The check for EOF is positioned in the DOWHILE clause,
using one of the following equivalent expressions:
DOWHILE more data
DOWHILE more records
DOWHILE records exist
DOWHILE NOT EOF
(B) Trailing Decision Loop
The REPEATUNTIL structure is similar to the
DOWHILE structure, in that a group of statements
are repeated in accordance with a specified
condition
However, where the DOWHILE structure tests the
condition at the beginning of the loop, a REPEAT
UNTIL structure tests the condition at the endof the
loop
REPEATUNTIL is a trailing decision loop; the
statements are executed once before the condition
is tested
(B) Trailing Decision Loop (cont.)
REPEAT
REPEAT
statement
Display Hello
statement
Display Welcome
.
.
UNTIL condition is true
UNTIL exit is 1
(B) Trailing Decision Loop
(cont.)
There are two other considerations of which you
need to be aware before using REPEATUNTIL
First, REPEATUNTIL loops are executed when the
condition is false
Second, the statements within a REPEATUNTIL
structure will always be executed at least once
Example 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 items
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.
Example 4: Process Inventory
Items
Process_Invent_Record
initialize
print report heading
REPEAT
read inventory record
IF item_number .
IF stock_amount
print
increment
ENDIF
ENDIF
UNTIL item_number = 0
print
END
(C) Counted Loop
Counted repetition occurs when the exact number
of loop iterations is known in advance
The execution of the loop is controlled by a loop
index, and instead of using DOWHILE, or
REPEATUNTIL, the simple keyword DOTOis
used
(C) Counted Loop (cont.)
DO loop_index = initial_value TO final_value
statement block
ENDDO
DO counter = 0 TO 4
Display Hello
ENDDO
(C) Counted Loop (cont.)
The DO loop does more than just repeat the
statement block
1. Initialize the loop_index to the required
initial_value
2. Increment the loop_index by 1 for each pass
through the loop
3. Test the value of loop_index at the beginning of
each loop to ensure that it is within the stated
range of values
4. Terminate the loop when the loop_index has
exceeded the specified final_value
(C) Counted Loop (cont.)
Convert_to_Celcius
DO temp_count = 1 to 15
Prompt for
Get f_temp
Compute c_temp = (f_temp 32) * 5/9
Display c_temp
ENDDO
Display
END
Learning outcomes
Develop algorithms that use the
repetition control
structures(DOWHILEENDDO, REPEAT
UNTIL, DOTOENDDO)
Develop algorithms using variations of the
repetition construct
Questions
Print out all integers from 5 down to -5 by using
repetition
Expected Output:
5 4 3 2 1 0 -1 2 3 4 5
Questions
In mathematics, the factorial of a non-negative
integer n, denoted by n!, is the product of all
positive integers less than or equal to n. For
example,
5! 1 2 3 4 5 120
where n! represents n factorial. The notation n!
was introduced by Christian Kramp in 1808. Write
a C program to demonstrate how a program is
able to calculate the value of a n factorial if n is
provided by user.
(11 marks)
Questions
Create a program to calculate how long does it
take for a student to get RM10,000 assuming
that student deposit RM5,000 into a Bank with
yearly interest 2%.
Questions
Create a program to get a series of integers determined
by user, and display the largest number, smallest
number, and average.
Expected Output:
Total Integers: 2
Insert Integer 1: 5
Insert Integer 2: 6
Smallest Number: 5
Largest Number: 6
Average: 5.5