Chapter 5: Control Structure: Csc118 Fundamentals of Algorithm Development
Chapter 5: Control Structure: Csc118 Fundamentals of Algorithm Development
1
LESSON OUTCOMES
Upon completion of this chapter, students should be able to:
2
CONTROL STRUCTURE
REPETITION STRUCTURE
REPETITION
FLOWCHART
3
CONTROL STRUCTURE
REPETITION STRUCTURE
Requirements of a Repetition Structure
5
CONTROL STRUCTURE
REPETITION STRUCTURE
Types of Iteration/Looping/Repetition Statements
1. WHILE…END WHILE statements
6
CONTROL STRUCTURE
3. REPETITION STRUCTURE
Types of Iteration/Looping/Repetition Statements
2. DO…WHILE statements
7
CONTROL STRUCTURE
3. REPETITION STRUCTURE
Types of Iteration/Looping/Repetition Statements
2. FOR…END FOR statements
8
CONTROL STRUCTURE
REPETITION STRUCTURE
Types of Repetition Structure
1. Counter-Controlled loop
Used when the number of time a segment code needs to be
repeated is known in advance.
Example: Repeat a process for 10 times
Requirement:
a. The name of a control variable or loop counter
b. The initial value of the control variable
c. The condition that test for the final value of the control variable
d. The increment or decrement of the control variable each time loop is
executed. 9
CONTROL STRUCTURE
REPETITION STRUCTURE
Types of Repetition Structure
1. Counter-Controlled loop
10
CONTROL STRUCTURE
3. REPETITION STRUCTURE
Types of Repetition Structure
1. Counter-Controlled loop (Example)
11
CONTROL STRUCTURE
REPETITION STRUCTURE
Types of Repetition Structure
2. Sentinel-Controlled loop
Used when the number of repetition depends on a certain
condition.
It uses a special end-of-data value called sentinel value that serves
as a signal for loop termination.
Also called event-controlled loops. The number of repetition is
not known.
For example : Write a complete C++ program display ‘Incorrect pin
number, please try again’ as long as the code entered is not 877.
12
CONTROL STRUCTURE
REPETITION STRUCTURE
Types of Repetition Structure
2. Sentinel-Controlled loop
14
CONTROL STRUCTURE
REPETITION STRUCTURE
Types of Repetition Structure
3. Flag-Controlled loop
Uses a Bool variable to control the loop.
Flag variable is used to control the execution of the loop.
15
CONTROL STRUCTURE
REPETITION STRUCTURE
Types of Repetition Structure
3. Flag-Controlled loop (Example)
16
CONTROL STRUCTURE
REPETITION STRUCTURE
Example (while….end while)
17
CONTROL STRUCTURE
REPETITION STRUCTURE
Example (while..end while)
Example # 1: Compute the sum of 3 numbers (Flowchart)
18
CONTROL STRUCTURE
REPETITION STRUCTURE
Example (do…while)
19
CONTROL STRUCTURE
REPETITION STRUCTURE : Example (do…while)
20
CONTROL STRUCTURE
REPETITION STRUCTURE : Example (for…end for)
counter++
21
CONTROL STRUCTURE
REPETITION STRUCTURE : Example (for…end for)
22
CONTROL STRUCTURE
REPETITION STRUCTURE : Try this out
Write a
pseudocode
(from the given
flowchart) to
find the sum of
all the even
numbers from 2
to 20.
23
CONTROL STRUCTURE
REPETITION STRUCTURE : Try this out (Answer)
24
CONTROL STRUCTURE
DIFFERENCES
25
Give THREE (3) requirements of repetition structure?
What is the definition of counter‐controlled loop?
Given the program segment below:
int n = 1;
while (n <= 5)
{
n = n + 1;
cout << n << “ “;
}
Write the statement/expression that corresponds to the loop operation.
Answer:
Initialization: _________________________________________________
Evaluation : _________________________________________________
Update : _________________________________________________
26
END OF CHAPTER 5
27