Csc121 - Topic 5 (Control Structure (Repetition)
Csc121 - Topic 5 (Control Structure (Repetition)
INTRODUCTION
TO ALGORITHM
DESIGN AND
DEVELOPMENT
TOPIC 5:
ALGORITHM
DESIGN FOR
REPETITION
1
COURSE OUTLINE
TOPIC 5
Analysis of Problems Requiring Repetition Control
Structure
Setting Three Requirements of a Repetition
Structure: Initialization, Condition and Updating
2
REPETITION/ITERATION IN
DAILY LIFE
3
ANALYSIS OF
PROBLEMS
REQUIRING
REPETITION
CONTROL
4
STRUCTURE
WHAT IS
REPITITION/ITERATION?
Iteration is basically the repetition of one or more steps in an
algorithm
Executes the same section of program code over and over again, as
long as a loop condition of some sort is met with each iteration
Also called looping and the condition is accordingly called the loop
condition/loop control variable
5
SCENARIOS REQUIRING
REPETITION
Loops are used in many applications such as:
Program to
Application to Program that calculate fees
Program to ensure that user outputs every which increase
accumulate data entries are number from 1 by 4 percent
multiple valid by through 20 each year.
calculation in repeatedly along with its Display the fees
business reports prompting the value doubled each year for
user. and tripled the next 10
years.
6
Can you think of
any other
situations where
looping can be
applied?
7
WHY REPITITION/ITERATION IS
IMPORTANT
Programmer
Computer able can write one
to perform set of
complicated instructions
tasks is the that operates
ability to repeat on multiple,
them separate sets of
data
Can structure
programming Repetition
statements to allows the
be repeated as programmer to
long as specific efficiently use
conditions are variables
met
8
TYPE OF REPETITION
STRUCTURES
Loop statements
while
Pre-test Loop
for
Repetition
Structure
do…while
Post-test Loop
do…until
9
PRE-TEST LOOPS
❑ In each iteration, the loop control variable is tested first.
❑ If it is true, the loop action(s) is executed; if it is false, the
loop is terminated.
❑ Types of pretest loop: while | for
Loop control
variable /
condition Contro True
Loop control
l Action(s)
initialized at variable is tested
beginning of variabl to determine
pretest loop e number of times
False
instructions within
loop body are
10
processed
POST-TEST LOOPS
❑ In each iteration, the loop action(s) is executed first.
❑ Next, the loop control expression is tested.
❑ If it is true, a new iteration is started; otherwise, the loop
terminates.
❑ Types of pretest loop: do…while | do…until
Actions/
instructions are
Action(s) processed at least
once, the first
time!
Control True
Loop
variable
condition
appears at
False
end of
11 posttest loop
SETTING THREE
REQUIREMENTS OF
A REPETITION
STRUCTURE:
INITIALIZATION,
CONDITION AND
UPDATING
1
2
USING A LOOP CONTROL
VARIABLE
❑ You can use a loop to execute a body of statements
continuously as long as some condition continues to be
true.
❑ To ensure the loop runs correctly, the number of
repetition/iteration should end correctly
❑ You can declare a loop control variable to manage
the number of repetitions a loop performs. Three
separate actions should occur using a loop control
1. A variable, the loop control variable is initialized
variable:
(before entering the loop)
2. The loop control variable is tested, and if the result is true,
the loop body is entered.
3. The body of the loop must take some action that alters
the value of the loop control variable (so that the while
13 expression eventually evaluates as false)
INITIALIZATION AND UPDATING
LOOP INITIALIZATION
Before a loop can start, some preparation is usually required.
Such preparation is called loop initialization. Initialization
must be done before the first execution of the body. It sets the
stage for the loop actions.
LOOP UPDATE
How can the condition that controls the loop be true for a while
and then change to false…?
The answer is that something must happen inside the body of
the loop to change the condition. Otherwise, we would have an
infinite loop. This changes the resulting condition from true to
false (are known as loop update). Updating is done in each
iteration, usually as the last action. If the body of the loop is
14
repeated m times, then the updating is also done m times.
WHILE LOOP
❑The general form of the while statement is:
while (expression)
statement;
False
15
INITIALIZATION AND UPDATING
Initializatio
Initializati n
on
Action(s)
Action(s) Updating
Updating
Pretest
loop Posttest
loop
16
CONTROLLING WHILE LOOP
REPETITION
When you write a loop, you must control the number
of repetition it performs. If you don’t, you run the risk
of creating an infinite loop.
17
CONTROLLING WHILE LOOP
REPETITION
Counter-controlled Sentinel-controlled
18
SYNTAX FOR CONTROLLED
WHILE LOOP
Syntax of sentinel-controlled while
statement:
Syntax of counter-controlled while
statement:
cin >> variable; //initialize loop
control variable
int counter=0; //initialize loop
while (variable != sentinel) //test
control variable
loop control variable
while (counter < n) //test loop
{
control variable
cin >> variable; //update loop
{ counter++; //update loop control
control variable
variable }
}
19
ALGORITHM
DEVELOPMENT
FOR REPETITION
CONTROL
STRUCTURE
(PSEUDO-CODE
2
AND FLOWCHART)
0
WHILE LOOP WITH A COUNTER
Start
Pseudo-code:
Start
num count num count = 0
=0 while count < 4
print “Hello”
count =
count + 1
count < print endwhile
4? “Hello” End
Ye
s
No
21
WHILE LOOP WITH A COUNTER
Pseudo-
code:
Start
sum=0
count=0
repeat 10 times
Display
“Enter number:”
Read
num
sum=sum+num
count++
endRepeat
Scenario: Calculate sum Display “Sum =
of 10 integer “,sum
End
22
WHILE LOOP WITH A SENTINEL
VALUE
Start
Pseudo-code:
get
response get response
while response = ‘Y’
print bankBal
bankBal = bankBal + bankBal
response Yes *intRate
print
= ‘Y’? Print “Do you want to see
bankBal
next year’s
balance? Y or
No
No…”
bankBal = bankBal + get response
print bankBal * intRate endwhile
“Have a
nice day”
print “Do you want to
see next year’s
balance Y or N…”
End
get
response
23
Banking application
EXERCISE 5.1
Identify the type of while loop repetition control (counter/sentinel) for the
following problems:
1. Determine the highest mark in a class of 35 students.
2. Determine the highest mark in a class. The process stops when the user
enters an invalid mark.
3. Ask the user to enter 30 numbers. Count how many odd numbers.
4. Calculate the total salary of all staff in finance department. The number
of staff will be entered by the user.
5. Count how may odd numbers in a group of numbers. The process will
end if user insert press equal sign.
6. Calculate the total salary of all staff in finance department. The process
24 ends when user insert N (No) for stop the process.
EXERCISE 5.2
Design the pseudo-code and flowchart for the questions in Exercise 5.1
25
END OF
TOPIC
THANK YOU!
26