Chapter 4 CSC128
Chapter 4 CSC128
Chapter 4 CSC128
Chapter 4
1
Norhafizah Hashim/CSC128
Chapter Outline
2
Introduction
• Assume you want to find the average of three integer
numbers. The program segment may look like this:
Norhafizah Hashim/CSC128 3
Introduction
Power of computers are the ability to repeat an operation or a
series of operations many times.
Allows certain instructions to be executed
more than once.
Norhafizah Hashim/CSC128 4
Pseudocode and Flowchart for Repetition Control
Structure
• To repeat the same CONDITION
F
statement(s),
certain conditions Pre-test loop T
An action or series of
must be met. actions
Norhafizah Hashim/CSC128 5
Pseudocode and Flowchart for Repetition Control
Structure
Pre-test loop
Each iteration:
F The loop expression [condition] is tested
CONDITION
first.
T
If it is TRUE, the loop action(s) are executed.
An action or series of
actions
If it is FALSE, the loop is terminated.
Norhafizah Hashim/CSC128 6
Pseudocode and Flowchart for Repetition Control
Structure
Post-test loop
In each iteration:
An action or series
of actions The loop action(s) are executed first.
Next, the loop expression [condition] is
T
CONDITION tested.
F If it is TRUE, a new iteration is started,
otherwise the loop terminates.
Norhafizah Hashim/CSC128 7
Pseudocode and Flowchart for Repetition Control
Structure
num = 1
EXAMPLE: A program to print number 1 to
F
100. (By using the pre-test loop) num <= 100
1. Set num to 1 T
Norhafizah Hashim/CSC128 8
Pseudocode and Flowchart for Repetition Control
Structure
num = 1
EXAMPLE: A program to print number 1 to
100. (By using the post-test loop) Print num
1. Set num to 1
2. Repeat the following steps: num=num + 1
Norhafizah Hashim/CSC128 9
Types of Repetition Control Structures
Rules For Loop:
1. Should have loop control variable. If the loop continues to
execute endlessly it is called infinite loop.
2. Must assign an initial value to loop control variable.
3. Should have a loop control condition.
4. A loop control variable updating, either increases (increment)
or decreases (decrement) its value.
Norhafizah Hashim/CSC128 10
Types of Repetition Control Structures
The forms
Types of tests
Counter-
for controlled loop
-Fixed value
Pre-test loop
while
Sentinel-controlled
Post-test loop do…while loop
-execute the action(s) -Custom value
at least once
Flag-controlled
loop
-bool value
Norhafizah Hashim/CSC128 11
Types of Repetition Control Structures
Syntax:
Single statement: for (initial value; condition; update)
statement;
Multiple statements: for (initial value; condition; update)
{
Statement1;
statementN;
}
Norhafizah Hashim/CSC128 13
Types of Repetition Control Structures
Example 1: The tracing table:
Print ‘$’ 5 times in a single line. i i<=5 cout<<“$” i++
1 true $ i=1+1
for(int 2 true $ i=2+1
i=1;i<=5;i++){ 3 true $ i=3+1
cout<<”$”; 4 true $ i=4+1
} 5 true $ i=5+1
6 false
Output:
$$$$$
Norhafizah Hashim/CSC128 14
Types of Repetition Control Structures
Counter-controlled loops while
Syntax: Example:
initial value int i=1;
while (condition){ while(i<=5){
statement(s); cout<<“$”;
update; i++;
} }
Norhafizah Hashim/CSC128 15
Types of Repetition Control Structures
Counter-controlled loops do…while
Syntax: Example:
initial value int i=1;
do{ do{
statement(s); cout<<“$”;
update; i++;
}while(condition); } while(i<=5);
Norhafizah Hashim/CSC128 16
Types of Repetition Control Structures
Example 2: before applying repetition
Norhafizah Hashim/CSC128 17
Types of Repetition Control Structures
Example 2: after applying repetition (while)
Norhafizah Hashim/CSC128 18
Types of Repetition Control Structures
Example 3: Display the SUM & AVERAGE of 5 marks that are
entered by user.
Norhafizah Hashim/CSC128 19
Types of Repetition Control Structures
Sentinel-controlled loops
A sentinel value is NOT part of data to be
while Example 4: processed.
Assume the marks are 10, 20 and 30.
cout<<”Enter mark or enter -1 to stop:”;
cin>>mark;
Norhafizah Hashim/CSC128 20
Types of Repetition Control Structures
Example 5
Sentinel-controlled loops
do…while
A menu-driven
program uses this
post-test loop.
Since it will be
executed at least
once, the initial
value of control
variable is not
required
Norhafizah Hashim/CSC128 21
Types of Repetition Control Structures
Sentinel-controlled loops do…while
Norhafizah Hashim/CSC128 22
Types of Repetition Control Structures
Flag-controlled loops Uses a bool variable to control the loop
found=false;
while(!found)
{
….
….
if(expression)
found=true;
}
Norhafizah Hashim/CSC128 23
Types of Repetition Control Structures
Flag-controlled loops Example 6: Assume the input are 23, 89, 45
//A
//B
Norhafizah Hashim/CSC128 24
Nested Loop
• A loop (inner) as one of the statements in the body of another loop (outer)
while (condition 1){ Outer loop
while (condition 2)
inner loop body; Inner loop
outer loop body;
} for (expr1; expr2; expr3)
{
…
for (expr1; expr2; expr3)
*A program may have the combination of {
different types of repetition control …
structures }
…
}
Norhafizah Hashim/CSC128 25
Nested Loop
int i;
Example: Write a program that
will produce the following
int j;
output:
for(i=1; i<=4; i++)
*** {
*** for(j=1; j<=3; j++)
*** {
***
cout<<"*";
}
cout<<endl;
}
Norhafizah Hashim/CSC128 26
i i<=4 j j<=3 cout
1 T 1 T *
Nested Loop 2 T *
3 T *
int i; 4 F
int j; 2 T 1 T *
2 T *
3 T *
for(i=1; i<=4; i++) 4 F
{ 3 T 1 T *
Output 1
for(j=1; j<=3; j++) 2 T *
{ *** 3 T *
cout<<"*"; 4 F
***
} 4 T 1 T *
cout<<endl; *** 2 T *
3 T *
} *** 4 F
Norhafizah Hashim/CSC128 5 F 27