Csc121 - Topic 5 (Control Structure (Repetition)
Csc121 - Topic 5 (Control Structure (Repetition)
INTRODUCTION
TO ALGORITHM
DESIGN AND
DEVELOPMENT
TOPIC 5: ALGORITHM DESIGN
FOR
REPETITION CONTROL
STRUCTURE
1
COURSE OUTLINE
TOPIC 5
Analysis of Problems Requiring Repetition Control Structure
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 REPITITION
Program to
Program that
Application to calculate fees which
Program to outputs every
ensure that user increase by 4
accumulate number from 1
data entries are percent each year.
multiple calculation through 20 along
valid by repeatedly Display the fees
in business reports with its value
prompting the user. each year for the
doubled and tripled
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
Repetition allows
programming
the programmer
statements to be
to efficiently use
repeated as long
variables
as specific
conditions are
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 Control True
variable Action(s)
initialized at Loop control variable is
beginning of tested to determine
pretest loop number of times
False
instructions within loop
body are processed
10
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
Action(s) are processed at least
once, the first time!
Control True
variable
Loop condition
appears at end
False
of posttest loop
11
SETTING THREE
REQUIREMENTS OF A
REPETITION
STRUCTURE:
INITIALIZATION,
CONDITION AND
1
2
UPDATING
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 variable:
1. A variable, the loop control variable is initialized (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 expression eventually evaluates
13 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
repeated m times, then the updating is also done m times.
14
WHILE LOOP
❑The general form of the while statement is:
while (expression)
statement;
❑The expression acts as a decision maker and is usually a logical expression. It provides an
entry condition. If it initially to true, the statement executes. The loop condition (the
expression) is then reevaluated.
❑The statement continues to execute until the expression is no longer true (false). Include
an exit condition that will eventually evaluate expression to be false.
❑A loop that continues to execute endlessly is called an infinite loop.
Exp Statement
True
False
15
INITIALIZATION AND UPDATING
Initialization
Initialization
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
while (variable != sentinel) //test loop control
int counter=0; //initialize loop control variable
variable
while (counter < n) //test loop control variable
{
{ counter++; //update loop control variable }
cin >> variable; //update loop control variable
}
19
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 ends when user
20 insert N (No) for stop the process.
ALGORITHM
DEVELOPMENT FOR
REPETITION
CONTROL
STRUCTURE
(PSEUDO-CODE
AND FLOWCHART)
2
1
WHILE LOOP WITH A COUNTER
Start
Pseudo-code:
Start
num count = 0 num count = 0
while count < 4
print “Hello”
count = count + 1
endwhile
print End
count < 4?
“Hello”
Yes
No
22
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
Display “Sum = “,sum
End
23
WHILE LOOP WITH A SENTINEL VALUE
Start
Pseudo-code:
get
response get response
while response = ‘Y’
print bankBal
bankBal = bankBal + bankBal *intRate
response = Yes Print “Do you want to see next year’s
print
‘Y’? balance? Y or No…”
bankBal
get response
endwhile
No
bankBal = bankBal + bankBal
print “Have * intRate
a nice day”
print “Do you want to see
next year’s balance Y or
N…”
End
get
response
24 Banking application
EXERCISE 5.2
Design the pseudo-code and flowchart for the questions in Exercise 5.1
25
END OF TOPIC
THANK YOU!
26