The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.

Basic knowledge for programmer in understanding repetition control structure

Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by Gee Lail, 2020-02-26 22:13:54

REPETITION CONTROL STRUCTURE

Basic knowledge for programmer in understanding repetition control structure

DISCOVERING COMPUTERS 2012: CHAPTER 13 2

2.0
DESIGN A SOLUTION

2.1 D) CONTROL STRUCTURE

Learning Outcome

At the end of this topic, students should be able to:
2.1 Design a Solution
d) Apply appropriate control structures
Repetition/Looping
- Counter-controlled, sentinel-controlled

4



Control Structure Repetition

Programmers use the repetition structure, when
they need the computer to repeatedly process one
or more program instructions

Repetition structure:
• also called looping control structure

6

Control Structure Repetition

2 ways to stop loop Counter Controlled Counter

(when we know how Accumulator
many times loop body
Both Counter &
will be executed ) Accumulator

Repetition

Sentinel Controlled

(when we don’t know exactly
know how many times loop

body will be executed

Repetition structure:
• also called looping control structure

7



Repetition Counter-Controlled

Concept # a counter is a numeric variable used for
counting something.

Definition : A counter-controlled /Looping condition specifies the
requirement for repeating the instructions.

Indicates when the computer should continue “looping”
through the instructions.

The counter-controlled has 4 important parts:

(1) Name the counter : any names can be given to the loop counter

(2) Initialize the counter :

* initializing means to assign a beginning value to the counter .

Counters are usually initialized to the number 0 or 1; however
they can be initialized to any number; depending on the value
required by the algorithm.

(3) Condition to test the final value of loop counter.

(4) Update the counter (increment/decrement) :

*means adding a number to the value stored in the counter. The number
can be either positive or negative, integer or non integer.

9

Repetition Counter-Controlled

Pseudocode 1 &2 3

Start 4

name & initialize counter
repeat while (condition)

statement_n
update the counter
end repeat
Stop

1 & 2. Name & Initialize loop counter control variable, before the loop begins
3. Condition, test the loop counter control variable in the while condition; if the

condition is true, the loop begins executing
4. Update the value of the loop counter control variable (increment or decrement)

10

Repetition Counter-Controlled

Flowchart: Start Loop control
variable is named &
Loop control name &initialize initialized
variable is counter
tested 1&2

3 condition true

false statement_n Loop control
variable is
Stop counter updated
increment
4

11

Repetition Example 1

Counter Controlled

Problem statement

Create a program that will print ‟Hello” four times.

IPO analysis

Input -

Process -

Output ‟Hello” is printed 4 times

12

Repetition Example 1

Counter Controlled

Problem statement
Create a program that will print ‟Hello” four times.
Pseudocode:

Start

set count = 1 1&2

repeat while (count < 5) 3

Print ‟Hello”

count = count + 1 4

end repeat

Stop

13

Repetition Example 1

Counter Controlled

Flowchart: Loop control
variable is named &
Start initialized
set count = 1
1&2

condition count < 5 true

3 false Print
‟Hello”
Stop update
count =
count + 1 4

14

Repetition Example 1

Counter Controlled

Counter Table:

set count repeat while Print “Hello” count = count + 1
=1 (count < 5)
1 “Hello” 1+1=2
2 true “Hello” 2+1=3
3 true “Hello” 3+1=4
4 true “Hello” 4+1=5
5 true end repeat
false

15

Repetition Example 2

Counter Controlled

Problem statement

Create a program that calculates and displays the pay of 5 employees given
hours worked and hourly pay rate.

IPO analysis Sequence Repetition

Input hours worked, pay rate hours worked, pay rate
Process
Calculate the pay for an Determine the pay for 5

employee. employees.

Output pay pay

16

Repetition Example 2

Counter Controlled

Pseudocode:

Start 1&2

set x to 1

repeat while (x ≤ 5) 3

Read hours worked, pay rate

pay = hours worked x pay rate

Print pay 4

x=x+1
end repeat

Stop

17

Repetition Example 2

Counter Controlled Start name &
x=1 initialize
Flowchart:
1&2

test

true

3 x≤5

false Read hours
worked, pay rate

Stop pay = hours worked
x pay rate

update Print pay 18
x=x+1
4

Repetition Example 2

Counter Controlled

Counter Table:

x =1 repeat Read hours worked, pay =hours Print x=x +1
while pay rate worked x pay pay
(x ≤ 5) 1+1=2
rate 2+1=3
3+1=4
1 true 40.00 , 20.00 Pay=40x20 800 4+1=5
5+1=6
2 true 50.00 , 22.50 1 125

3 true 55.00 , 25.00 1 375

4 true 60.00 , 23.00 1 380

5 true 66.00 , 25.00 1 650

6 false end repeat

19

Repetition Example 3

Counter Controlled

Problem statement

Create a program that calculates and displays the pay for a number of
employees given hours worked and hourly pay rate.

IPO analysis Sequence Repetition

Input hours worked, pay rate hours worked, pay rate, n
Process
Calculate pay of an Determine the pay for a number
employee of employees (n)

Output pay pay

20

Repetition Example 3

Counter Controlled

Pseudocode:

Start

set employee to 1 1&2

Read n 3
repeat while (employee <= n)
21
Read hours worked, pay rate

pay = hours worked x pay rate

Print pay 4

employee = employee + 1
end repeat

Stop

Repetition Start Example 3initialize
employee = 1
Counter Controlled 1&2
Read n
Flowchart:
employee ≤ true
condition n
Read hours
3 false worked, pay rate

Stop pay = hours worked
x pay rate

update Print pay

4 employee = employee + 1 22

Repetition Example 3

Counter Controlled

Counter Table:

employee Read n Repeat Read hours Calculate Print employee =
=1 5 while pay employee + 1
(employee worked, pay rate pay
1 ≤ n) 800 1+1=2
2 40.00 , 20.00 1 125 2+1=3
3 true 50.00 , 22.50 1 375 3+1=4
4 55.00 , 25.00 1 380 4+1=5
5 true 60.00 , 23.00 1 650 5+1=6
6 66.00 , 25.00
true
end repeat
true

true

false

23

1. Display “Great” 3 times and display “Bye” when you quit
the program.

2. Create a program that calculates and displays the bonus
for 10 employees based on 20% from their monthly salary.

3. Create a program that calculates and displays the bonus
for a number of employees based on 20% from their
monthly salary.

24

1. Display “Great” 3 times and display “Bye” when you quit the
program

Pseudocode Start
set count = 1
Start
count <
set count = 1 4 true
repeat while (count < 4)
false Print
Print “Great” ‟Great”
count = count + 1
end repeat count =
count + 1
Print “Bye”

Stop Print
‟Bye”

Stop 25



Repetition Counter-Controlled (Accumulation)
Concept

Some problems require you to calculate a subtotal, a
total, or an average. An accumulator is a numeric
variable used for accumulating (adding together)
something.

Running total:
• also known as the total of a series of numbers

27

Repetition Counter-Controlled (Accumulation)

Concept

The loop counter-controlled (accumulation) has 2 important parts :
(1) Name & Initialize the Accumulator :
* initializing means to assign a beginning value to the accumulator .

Accumulator are usually initialized to the number 0 or 1; however
they can be initialized to any number; depending on the value
required by the algorithm.
(2) Update the accumulators
* means adding a number to the value stored in the accumulator.
The number can be either positive or negative, integer or non
integer. The accumulator is updated by a value that varies.

28

Repetition Counter-Controlled (Accumulation)

General logic for calculating a running total:

Start Counter, accumulator is named &
initialize initialized
accumulator
1
condition
true Update the
false accumulator
Read number
Stop add number to 2
accumulator
add 1 to counter 29

Repetition Example 1

Accumulator

Problem statement
Calculate the sum of three integers.

IPO analysis Sequence Looping

Input num1, num2, num3 num
Process Determine to calculate the sum for
Calculate the sum of 3 integers.
Output three integer sum

sum

30

Repetition Example 1

Accumulator

Pseudocode:

Start name & initialize condition
set count to 1 count, sum
set sum to 0 3
1&2
accumulate
repeat while count < 4 sum

Read num Note:
• sum is the variable used
sum = sum + num 5
to accumulate values of
count = count + 1 4 num
end repeat
Print sum update
Stop count

31

Repetition Example 1

Accumulator

Flowchart: Start name &
initialize
count = 1
sum = 0 1&2

condition true

3 count < 4

false Read accumulate
num
Print 4
sum sum = sum + num 5
count = count + 1
Stop update

32

Repetition Example 1

Accumulator

Counter Table:

count =1 sum =0 Repeat Read sum = sum + count = count Print
while num num + 1 sum
1 0 (count <
2 90 90 0 + 90 = 90 1 + 1 = 2
3 160 4) 70
4 240 true 90 + 70 = 2+1=3
true 80 160

true 160 + 80 = 3+1=4
240
false
end repeat 240

33

Repetition Example 2

Accumulator

Problem statement

Suppose you are writing a program that calculates a business’s total
sales for a week. The program would read the sales for each day as
input and calculate the total of those numbers.

34

Repetition Example 2

Accumulator

IPO analysis Sequence Looping

Input sales1, sales2, sales3, sales
sales4, sales5, sales6,
sales7

Process Calculate total sales by Determine total sales for a week.
a week.

Output total sales total sales

35

Repetition Example 2

Accumulator

Pseudocode:

Start initialize
count, sum

set i to 1 1&2 condition
3
set total sales to 0
repeat while (i ≤ 7) accumulate
total sales
Read sales
36
total sales = total sales + sales 4

i=i+1 5
end repeat
Print total sales update
Stop count

Repetition Example 2

Accumulator repeat Read total sales = i=i+1 Print
while sales total sales + total
set (i ≤ 7) sales
set i total true sales
true 380307 0
=1 sales = 1000 0 + 1000 = 1000 1+1=2
0 true 2+1=3
1250 1000 + 1250 = 3+1=4
10 true 2250 4+1=5
2 1000 5+1=6
true 3000 2250 + 3000 = 6+1=7
3 2250 5250 7+1=8
true
4 5250 12000 5250 + 12000 =
true 17250
5 17250
false 10000 17250 + 10000
6 27250 = 27250

7 30500 3250 27250 + 3250 =
30500
8 38000
7500 30500 + 7500 =
38000

end repeat

Repetition Example 3

Accumulator

Problem statement

Calculate and display the average of 100 numbers input by the
user.

IPO Sequence Looping
analysis

Input num1, num2, num
…num100

Process Calculate the average Calculate the average of 100

of 100 numbers numbers.

Output average average

38

Repetition Example 3

Accumulator

Pseudocode: name &initialize
Start count, sum

set i to 1 1&2 condition

set sum to 0 3

repeat while count < 101 accumulate
sum
Read num
Note:
sum = sum + num 4 • sum is the variable used

i=i+1 5 to accumulate values of
end repeat num
update
Calculate average count 39
average= sum/100

Print average
Stop

Repetition Example 3

Accumulator initialize

Flowchart: Start

set i = 1 1 &2
set sum = 0

condition true

3 i < 101

false Read
num

average = sum/100 sum = sum + num accumulate
i=i+1
Print 4
average 5

Stop update

40

Repetition Example 3

Accumulator

Counter Control Table:

i sum i<101 Read sum = sum + i=i+1 average= Print
num num 2 sum/100 average

1 0 true 10 0+10=10

2 true

3 true

4 true

5 true

101 false XXXX XXXX

41

1. Create a program that calculates and displays the total
bonus for 10 employees based on 20% from their monthly
salary.

2. Create a program that calculates and displays the total
bonus for a number of employees based on 20% from
their monthly salary.

42



Repetition Compare the questions

Sentinel Control

1. Create a program that will calculate the area
of 100 circles.

2. Create a program that will calculate the area
of circles based on radius. The program will
stop if user enter negative radius.

Repetition Question 1

Sentinel Control

Create a program that will calculate the area
of 100 circles.

In this question, you know that the program will
calculate the area of 100 circles based on the
radius entered.
 Repetition control structure using

counter-controlled

Repetition Question 2

Sentinel Control

Create a program that will calculate the area of
circles based on radius. The program will stop if
user enter negative radius.

In this question, you did not know how many
times the program will calculate the area of
circle. But you know that the program will STOP
if user enter negative radius.

 Repetition control structure using sentinel-

controlled

Repetition Repetition control structure using

Sentinel Control sentinel-controlled

To solve this kind of problems, we need this:

1. Initial value of sentinel control variable –
input from user

2. Condition to test sentinel control variable
3. Update the value of sentinel control

variable – input from user

Repetition Question 2

Sentinel Control

1. Initial value of sentinel control

variable – input from user read radius

2. Condition to test sentinel while (radius ≥ 0)

control variable

3. Update the value of sentinel

control variable read radius

Repetition Solution – Question 2

Sentinel Control

Input
radius
Process
Calculate area of circles as long as the
radius is positive.
Output
area

Repetition Solution – Question 2

Sentinel Control

Initial value of sentinel

Start variable

read radius Condition to test
sentinel variable
while (radius ≥ 0)

area = 3.142 x radius x radius

print area

read radius Update the value of
repeat while sentinel variable

Stop


Click to View FlipBook Version