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

## Use of Control Structure Repetition

Discover the best professional documents and content resources in AnyFlip Document Base.

# 2.1(c) Use of Control Structure Repetition

### Keywords: Looping

2.1 Design a Solution

c) Explain each control structure and their respective
purposes.
d) Apply appropriate control structure

Sequence
Selection
Looping

Edited by: azlina mohamad | computer science unit

Control Structure : Looping

• Definition Looping Control Structure:

• Presentation of a set of instruction to be performed
repeatedly, as long as the condition is true.

Control Structure : Looping

• Purpose of Looping Control Structure:
• directs the computer to repeat one or more program
instructions/statements until some condition is met.

• allows the programmer to repeatedly process a set of
instructions, while only typing them in once.

• allows for a portion of an algorithm or computer
program to be done any number of times dependent on
some condition being met.

• Also knows as iteration/repetition.

Control Structure : Looping
• The loop contains a condition that controls

whether the instructions are repeated or
NOT.
• Instructions are repeated are called the
loop body.

Loop body

Control Structure : Looping

An Essential feature

• The loop counter requires the following to be known

1. Name of a loop counter
2. Initial value of the loop counter
3. Update -Increment or decrement by which to modify

the loop counter in each traversal
4. Condition to test the final value of loop counter

Control Structure : Looping
Loops can be ended in 2 ways :-

1. Counter controlled 2. Sentinel value
controlled
• Used when Number of
loop is known before Used when the exact number
the loop begin of repetitions is not known in
executing. advance. So a special value is
entered to end the loop.

Control Structure : Looping

1. Counter Controlled

• Use a variable name, example counter to control the
number of loops.

• Number of loop is known before the loop begin
executing.

• Involve three steps:
1. Set the initial value of the loop counter
2. Check the condition to test whether the loop
counter has reached the final value
3. Update the loop counter in every pass of the loop

Control Structure : Looping

1. Counter Controlled

• General Format :Pseudocode :
start

initialization
repeat while (condition)

one or more instructions

Loop body

update(increment/decrement)

end repeat
stop

Control Structure : Looping

1. Counter Controlled

• General Format :Pseudocode : i

start Set the initial value
counter = 1

repeat while (until counter less than 10)

print counter Check the condition

Loop body c

counter=counter + 1

end repeat

stop u

Update

Control Structure : Looping

1. Counter Controlled
• General Format :Flowchart: example

start Set the initial value

counter=1

Check the counter < 10 True
condition
False Print
stop counter

counter=counter+1

Update

Control Structure : Looping

1. Counter Controlled

• If the condition is True, the statement is executed, then the whole
thing is done again. How many times to repeat?

• The statement is executed repeatedly until the condition becomes
False => (What is the value of counter when the condition is False?)

• If the condition starts out False, the statement is never executed at all.

Control Structure : Looping

1. Counter Controlled

Example of Problem 1

Write the problem analysis, pseudocode and draw
flowchart to display “I Love Programming” 8 times.

Example of Problem 1 – IPO Analysis

Write the problem analysis, pseudocode and draw
flowchart to display “I Love Programming” 8 times.

Input -
Process
Output To display the given message 8 times
or

To repeat the condition for 8 times

Display “I love Programming” 8 times

Control Structure : Looping

1. Counter Controlled - Pseudocode

Example of Problem 1 : Use sequence approach

Write the problem analysis, pseudocode and draw
flowchart to display “I Love Programming” 8 times.

Start What if you have to
Print “I Love Programming” repeat 100 times or
Print “I Love Programming”
Print “I Love Programming” 1000 times or
Print “I Love Programming” more?
Print “I Love Programming”
Print “I Love Programming”
Print “I Love Programming”
Print “I Love Programming”
Stop

Control Structure : Looping

1. Counter Controlled - Pseudocode

Example of Problem 1 : Use looping approach

Write the problem analysis, pseudocode and draw
flowchart to display “I Love Programming” 8 times.

Step 1

How to make the statement repeats 8 times?
- Put the statement in a repeat/while structure

repeat while (x<= 8)
print “I Love Programming”

end while

Control Structure : Looping

1. Counter Controlled - Pseudocode

Example of Problem 1 : Use looping approach

Write the problem analysis, pseudocode and draw
flowchart to display “I Love Programming” 8 times.

Step 2

What’s the initial value of counter?

- Initialize the counter =0

counter = 0
repeat while (counter<=7)

print “I Love Programming”
end while

Control Structure : Looping

1. Counter Controlled - Pseudocode

Example of Problem 1 : Use looping approach

Write the problem analysis, pseudocode and draw
flowchart to display “I Love Programming” 8 times.

Step 3
How to make the loop stop?

- Increment the counter, i.e add 1 to x in every pass of the loop

counter = 1
repeat while (counter<= 8)

print “I Love Programming”
counter = counter +1
End while

Control Structure : Looping

1. Counter Controlled - Pseudocode

Example of Problem 1 : Use looping approach

Write the problem analysis, pseudocode and draw

Complete solution flowchart to display “I Love Programming” 8 times.

start Initialization

counter = 1

repeat while (counter<= 8) condition

print “I Love Programming”

counter = counter + 1

end while update
stop

 counter = counter + 1

Must be read as: counter gets the value of counter + 1

Control Structure : Looping

1. Counter Controlled - Pseudocode

Example of Problem 1 : Use looping approach

Write the problem analysis, pseudocode and draw

Complete solution flowchart to display “I Love Programming” 8 times.

start

counter=1

counter True
<= 8
Print “I Love
False Programming”

stop counter=counter+1

TRACING TABLE

counter counter<= Print Increment
8 counter=counter+1
1 T I LOVE PROGRAMMING
2 T I LOVE PROGRAMMING 2=1+1
3 T I LOVE PROGRAMMING 2=1+1
4 T I LOVE PROGRAMMING 3=2+1
5 T I LOVE PROGRAMMING 4=3+1
6 T I LOVE PROGRAMMING 5=4+1
7 T I LOVE PROGRAMMING 6=5+1
8 T I LOVE PROGRAMMING 7=6+1
9 F 8=7+1
-
-

Control Structure : Looping

1. Counter Controlled – Pseudocode & Flowchart
Example of Problem 2 Write the IPO Analysis, pseudocode and

draw flowchart to print number 1 to 10.

Complete solution

 Number of loops – (10 x)
 Numbers – known, 1,2,…,10
 Task to do in every pass of the loops:

 To print a number (1,2,…,10)
 To update / increment the loop counter
 Task to do after the loop stops:
 none

Example of Problem 2 – IPO Analysis

Write the IPO Analysis, pseudocode and draw flowchart to print
number 1 to 10

Input -
Process
Output To display the first 10 numbers
or

To repeat the condition for 10 times

1 2 3 4 5 6 7 8 9 10

Control Structure : Looping

1. Counter Controlled – Pseudocode
Example of Problem 2 Write the IPO Analysis, pseudocode and
Complete solution draw flowchart to print number 1 to 10

start
number = 1
repeat while (number <= 10)
print number
number = number + 1
end while

stop

Control Structure : Looping

1. Counter Controlled –Flowchart
Example of Problem 2 Write the IPO Analysis, pseudocode and draw
Complete solution flowchart to print the first 10 numbers.

start True repeat loop
body until
number=1 Print number number =11

number number=number+1
<=10

False
stop

Control Structure : Looping

1. Counter Controlled – Tracing table
Example of Problem 2 Write the IPO Analysis, pseudocode and

Complete solution draw flowchart to print the first 10 numbers.

Control Structure : Looping

1. Counter Controlled – IPO Analysis, Pseudocode & Flowchart

Example of Problem 3 Write the IPO Analysis, pseudocode and draw
flowchart to display 10 numbers entered by
user.

Complete solution

 Number of loops – (10 x)
 Numbers – unknown, depends on user input
 Task to do in every pass of the loops:

 To input a number
 To print the number
 Task to do after the loop stops:
 none

Example of Problem 3 – IPO Analysis

Write the IPO Analysis, pseudocode and draw flowchart to
display 10 numbers entered by user.

Input number
Process
Output To display 10 numbers entered by
user

or
To repeat the condition for 10 times

Display 10 numbers entered by user

Control Structure : Looping

1. Counter Controlled – Pseudocode

Example of Problem 3 Write the IPO Analysis, pseudocode and draw
flowchart to display 10 numbers entered by
user.

Complete solution

start
c=1
repeat while (c <= 10)
print num
c=c+1
end repeat/while

stop

Control Structure : Looping

1. Counter Controlled – Flowchart

Example of Problem 3 Write the IPO Analysis, pseudocode and draw
flowchart to display 10 numbers entered by

Complete solution user.

start

c=1

c < = 10

False print num

stop C=C+1

Control Structure : Looping

1. Counter Controlled – Pseudocode & Flowchart
Example of Problem 3 Write the IPO Analysis, pseudocode and draw

flowchart to display 10 numbers entered by
user.

Complete solution

c=c+1

Control Structure : Looping

1. Counter Controlled – Accumulated Loop

Example of Problem 4 Write the IPO Analysis, pseudocode and draw
flowchart to calculate and print the total of 10
numbers entered by user.

Complete solution

 Number of loops – (10 x)
 Numbers – unknown, depends on user input
 Task to do in every pass of the loops:

 To input a number
 To total up the number
 Task to do after the loop stops:
 To print total

Example of Problem 4 – IPO Analysis

Write the IPO Analysis, pseudocode and draw flowchart to calculate
and print the total of 10 numbers entered by user.

Input number
Process
Output To calculate and display the total of
10 numbers entered by user
Display the total of 10 numbers
entered by user

Control Structure : Looping

1. Counter Controlled – Accumulated Loop (Pseudocode)

Example of Problem 4 Write the IPO Analysis, pseudocode and draw
flowchart to calculate and print the total of 10
numbers entered by user.

Complete solution

start
y = 0, total = 0
repeat while (y < 10)
input num
total = total + num
y=y+1
end while

print total
stop

Control Structure : Looping

1. Counter Controlled – Accumulated Loop (Flowchart)

Example of Problem 4 Write the IPO Analysis, pseudocode and draw
flowchart to calculate and print the total of 10
numbers entered by user.

Complete solution

start

y=0
total = 0

y < 10 True
False
print total
total = total + num
stop y=y+1

Control Structure : Looping

1. Counter Controlled – Accumulated Loop

Example of Problem 4 Write the IPO Analysis, pseudocode and draw
flowchart to calculate and print the total of 10
numbers entered by user.

Complete solution

Control Structure : Looping

1. Counter Controlled – Accumulated Loop

Example of Problem 5 Write the IPO Analysis, pseudocode and draw
flowchart to calculate and display all odd

Complete solution numbers from 1 to 10 (inclusive)

 Number of loops – (5 x)
 Odd_num – known, 1,3,5,7,9
 Task to do in every pass of the loops:

 To check condition in order to enter
looping body.

 Print the first 5 odd number.(1 3 5 7 9)
 To update odd number.. How?
 Task to do after the loop stops:
 none.

Example of Problem 5 – IPO Analysis

Write the IPO Analysis, pseudocode and draw flowchart to display
all odd numbers from 1 to 10 (inclusive).

Input -
Process
Output To determine odd numbers from
range 1 to 10.

13579

Control Structure : Looping

1. Counter Controlled – Accumulated Loop (Pseudocode)

Example of Problem 5 Write the IPO Analysis, pseudocode and draw
flowchart to calculate and display the first 5
odd numbers.

Complete solution

start
odd_num=1
repeat while (odd_num < 10)
print odd_num
odd_num= odd_num + 2
end while

stop

Control Structure : Looping

1. Counter Controlled – Accumulated Loop (Flowchart)

Example of Problem 5 Write the IPO Analysis, pseudocode and draw
flowchart to calculate and display the first 5
odd numbers.

Complete solution

start

odd_num True Print
=1 odd_num

odd_num odd_num= odd_num + 2
< 10

False
stop

Control Structure : Looping

1. Counter Controlled – Accumulated Loop (Pseudocode)

Example of Problem 5 Write the IPO Analysis, pseudocode and draw
flowchart to calculate and display the first 5
odd numbers.

Complete solution

start
odd_num=1
repeat while (odd_num <= 10)
if(odd_num%2!=0)
print odd_num
odd_num= odd_num + 1
end while

stop

Control Structure : Looping

1. Counter Controlled – Accumulated Loop (Flowchart)
Example of Problem 5 Write the IPO Analysis, pseudocode and draw

flowchart to determine and display all odd
Complete solution numbers from 1 to 0

start

odd_num = 1

False odd_num False
stop <= 10 odd_num= odd_num + 1

True

If
odd_number

%2 !=0

True

Print
odd_num

Control Structure : Looping

1. Counter Controlled – Accumulated Loop

Example of Problem 6 Write the IPO Analysis, pseudocode and draw
flowchart to calculate and display total sum of
odd numbers from 1 to 10.

Complete solution

 Number of loops – (5 x)
 Odd_num – known, 1,3,5,7,9
 Task to do in every pass of the loops:

 To total sum the odd number.
 To update odd number.. How?
 Task to do after the loop stops:
 Print total sum of the first 5 odd

number

Example of Problem 6 – IPO Analysis

Write the IPO Analysis, pseudocode and draw flowchart to calculate
and display total sum of odd numbers from 1 to 10..

Input -
Process
Output To calculate and display the total
sum of the first 5 odd numbers.
Display the total sum of the first 5
odd numbers.

Control Structure : Looping

1. Counter Controlled – Accumulated Loop - Pseudocode

Example of Problem 6 Write the IPO Analysis, pseudocode and draw
flowchart to calculate and display total sum of

Complete solution odd numbers from 1 to 10.

( initialize )
start
odd_num=1,totalsum=0 ( check condition)

repeat while (odd_num < 10)

totalsum=totalsum+odd_num

odd_num= odd_num + 2

end while ( accumulate totalsum)
Print totalsum

stop ( update)

( print the last totalsum, after the loop end)

Control Structure : Looping

1. Counter Controlled – Accumulated Loop (Flowchart)

Example of Problem 6 Write the IPO Analysis, pseudocode and draw
flowchart to calculate and display total sum of

Complete solution odd numbers from 1 to 10..

start

odd_num
=1

odd_num True totalsum=totalsum+
< 10 odd_num

False odd_num=
odd_num + 2
print totalsum

stop

Control Structure : Looping

1. Counter Controlled – Accumulated Loop – Pseudocode & Flowchart

Example of Problem 6 Write the IPO Analysis, pseudocode and draw
flowchart to calculate and display total sum of

Complete solution odd numbers from 1 to 10.

Accumulating in Problem Solving

• Accumulating or summing is a task a program must often
perform.

• Accumulating means summing a group of numbers:
1. where a variable is added to another variable,
2. which holds the value of the sum or total.

• The expression for accumulating is as follow:

21

sum = sum + num

21

total = total + marks

How to apply Accumulating in Problem Solving?

Example 4: Accumulating in Sequence Structure

• Problem Statement:
• Calculate the average of three numbers.

Accumulating
total = num1 + num2 + num3
average = total / 3
Or
average = (num1 + num2 + num3) / 3
average

How to apply Accumulating in Looping?

Example 5: Accumulating in Looping Structure

• Problem Statement:
• Calculate the average of three numbers by using looping

start Accumulating
total=0, counter=1
while (counter<=100)

total = total + num
counter=counter+1
end while
average = total / 3
Print average
stop

Control Structure : Looping

1. Counter Controlled - Practice 1

Write the IPO Analysis, pseudocode and draw flowchart to

calculate and display the average exam score for a class

 Number of loops – num of student in class
 Enter number of student before loop
 score – unknown, depends on user input
 Task to do in every pass of the loops:

 To input score
 To total up the score
 Task to do after the loop stops:
 Calculate average
 Print average