The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by inaazlin, 2021-02-06 10:49:50

2.1 Design a solution Part 2

2.1 Design a solution Part 2

Summary selection

Purpose: Makes a decision (based on some condition) and then takes an
appropriate action based on that decision

SINGLE SELECTION DUAL SELECTION MULTIPLE SELECTION

Repetition Control Structures

• In repetition control structure, certain tasks are
performed repeatedly.

• Here also, a specified condition (according to logic
used) is checked and a sequence of statements is
repeated against a condition

• Each time, the condition is checked for new data
• The task included in the loop are executed until the

desired condition is satisfied.

• 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 known as iteration/repetition.

Repetition Control Structures

• Specifies a block of one or more statements that are
repeatedly executed until a condition is satisfied.

• The keyword used is while.
• Format:

while (condition)
loop-body

end while

Types of Repetition
Control Structures

• Counter-controlled
• Sentinel-controlled

Counter-controlled Repetition
Control Structures

Involve three basic processes:

1) Initialization
• Set the initial value of the loop control variable

2) Evaluation
• Test the loop control variable to determine whether the
loop should continue or stop

3) Updating
• Increment/decrement the value of loop control
variable

Counter-controlled Repetition
Control Structures

START initialization
counter = 0 evaluation
while counter less than 10
updating
Statement block

counter = counter + 1
end while
STOP

Counter-controlled Repetition
Control Structures

• Example 1:

Print “Java Programming” 5 times. How to solve this
problem?

Counter-controlled Repetition
Control Structures

EXAMPLE 1: Yes! Found solution
Print “Java Programming” 5 times using sequence
approach
START
Print “Java Programming” But, what if you
Print “Java Programming” have to repeat 100
Print “Java Programming” times? Or 1000
Print “Java Programming” times?
Print “Java Programming”

STOP

Counter-controlled Repetition
Control Structures

• EXAMPLE 1:
Print “Java Programming” 5 times.

• How to make the statement repeats 5 times?

• Put the statement in a while structure

while counter less than 5
print “Java Programming”

end while

Counter-controlled Repetition
Control Structures

• EXAMPLE 1:
Print “Java Programming” 5 times.

• What’s the initial value of counter?

• Initialize the counter, set counter =0

counter =0

while counter less than 5
print “Java Programming”

end while

Counter-controlled Repetition
Control Structures

• EXAMPLE 1:
Print “Java Programming” 5 times.

• How to make the loop stops?

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

counter =0
while counter less than 5

print “Java Programming”
counter = counter + 1
end while

Counter-controlled Repetition
Control Structures

• EXAMPLE 1:
Print “Java Programming” 5 times.

• Pseudocode

start Initialization
counter =0

while counter less than 5 Evaluation

print “Java Programming”

counter = counter + 1 Updating
end while

end

Counter-controlled Repetition
Control Structures

EXAMPLE 1: Counter Counter <5 Print
Print “Java Programming” 5 times value
0 True √
Pseudocode: 1 True √
2 True √
START 3 True √
counter =0 4 True √
while counter less than 5 5 False X
print “Java Programming”
counter = counter + 1 How many times the loop repeats?
endwhile

STOP

Counter-controlled Repetition
Control Structures

EXAMPLE 1: Start
Print “Java Programming” 5 times counter = 0

• Flowchart

counter less False
than 5 Stop

True
Print “Java
Programming”

counter = counter + 1

Counter Initialization

• Why we need set initial value =0?

START
counter = 0
while counter less than 5
print “Java Programming”
counter = counter + 1
endwhile

STOP

Counter Initialization

• What if we set initial value = 1?

START
counter = 1
while counter less than __
print “Java Programming”
counter = counter + 1
endwhile

STOP

Counter Initialization

• What if we set initial value = 1?

START
counter = 1
while counter less than and equals to 5
print “Java Programming”
counter = counter + 1
endwhile

STOP

Counter Initialization

• What if we set initial value = 1?

START
counter = 1
while counter less than 6
print “Java Programming”
counter = counter + 1
endwhile

STOP

Counter Initialization

• What if we set initial value = 2?

START
counter = 2
while counter less than or equals to __
print “Java Programming”
counter = counter + 1
endwhile

STOP

Counter Initialization

• What it means by counter = counter + 1?
• Equal (=) sign here does not mean the value of expression in both

sides are equal, but it means the variable at the left gets the value of
the expression of the right side
• So, counter = counter + 1
must be read as:

counter gets the value of counter + 1

Control Structure : Looping

1. Counter Controlled – Accumulated Loop

Example 2 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 2 – 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 repeat adding counter by 1 until
10 and repeat adding 10 numbers
entered by user.

total

Control Structure : Looping

1. Counter Controlled – Accumulated Loop (Pseudocode)

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

start
counter = 0, total = 0
while (counter < 10)
Read number
total = total + number
counter = counter + 1
end while
print total

stop

Control Structure : Looping

1. Counter Controlled – Accumulated Loop (Flowchart)

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

start

counter = 0 counter = counter +
total = 0 1

counter True
< 10
read number
False
total = total +
print total number

stop

Control Structure : Looping

1. Counter Controlled – Accumulated Loop

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

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

print total print total
stop
stop

Sentinel-controlled Repetition Control
Structures

• The number of loop is not known before the loop begin executing.
• The loop will stop when a sentinel value is reached.
• sentinel value is a special value (called signal value, dummy value or

flag value) such as 0, -1, 999, Y, N, T, F to indicate end of data entry.

Sentinel-controlled Repetition
Control Structures

EXAMPLE 1 :

Create a program to calculate the total of marks entered by user until
the user key in -1.

Analyze the problem:

Input : mark
Process : repeat reading and adding mark until -1 is entered
Output : total

Sentinel-controlled Repetition
Control Structures

EXAMPLE 1 :

Create a program to calculate the total of marks entered by user until
the user key in -1.

• Pseudocode

START
total = 0
INPUT mark
while mark not equal to -1
total = total + mark
input mark
endwhile
PRINT total

STOP

Summary repetition

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

Counter control Sentinel control

Number of loop is known The number of loop is NOT known
such as before the loop begin executed.
• display hello 10 times.. The loop will stop when a sentinel
• Print number 1 until value is reached.
Sentinel value is a special value that
100 programmer set. Such as
• press 0 to exit
• Stop until user enter -1

THE END


Click to View FlipBook Version