0% found this document useful (0 votes)
48 views27 pages

Chapter 5: Control Structure: Csc118 Fundamentals of Algorithm Development

The document discusses repetition structures and control flow in algorithms. It covers the key components of a repetition structure, including a loop control variable, loop condition, and loop body. It describes the different types of repetition statements like while, do-while, and for loops. It also defines and provides examples of counter-controlled, sentinel-controlled, and flag-controlled loops. The document aims to help students understand repetition structures and how to use different looping constructs in algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views27 pages

Chapter 5: Control Structure: Csc118 Fundamentals of Algorithm Development

The document discusses repetition structures and control flow in algorithms. It covers the key components of a repetition structure, including a loop control variable, loop condition, and loop body. It describes the different types of repetition statements like while, do-while, and for loops. It also defines and provides examples of counter-controlled, sentinel-controlled, and flag-controlled loops. The document aims to help students understand repetition structures and how to use different looping constructs in algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

PART 2

CHAPTER 5: CONTROL STRUCTURE


CSC118 FUNDAMENTALS OF ALGORITHM DEVELOPMENT

1
LESSON OUTCOMES
Upon completion of this chapter, students should be able to:

 Understand on Repetition Structure


 Requirements of a Repetition Structure
 Types of Repetition Statement
 Types of Repetition Structure

2
CONTROL STRUCTURE
REPETITION STRUCTURE

REPETITION

Repetition Control Structure also known


as Looping or Iteration. The control
structure that directs the computer to
repeat one or more instructions while
certain condition remains true.

FLOWCHART

3
CONTROL STRUCTURE
REPETITION STRUCTURE
Requirements of a Repetition Structure

 An Iteration/ Repetition structure requires:


1. Loop Control Variable (LCV)
 A value of variable determines whether the loop body will be
executed or not.
2. Loop condition
 If the condition is true, the loop body is executed; otherwise the
loop exits.
3. Loop body
 Block of statement that had to be repeated.
4
CONTROL STRUCTURE
REPETITION STRUCTURE
Requirements of a Repetition Structure
 An Execution of the loop body is controlled by 3 operations:
1. Initialization of the Loop Control Variable (LCV)
2. Evaluation of LCV in the loop condition
3. Update of the LCV.
 Operator is placed to indicate the increment or decrement of
LCV
Example: counter++ counter--

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

Statement that change the


LCV
13
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)

 Example # 1: Compute the sum


of 3 numbers

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

You might also like