Introduction To
Computer Programming
(CSC425)
Lecturer : Capt Dr Nur Atiqah Sia Abdullah
Credited to Afiza Ismail and DS Malik for the presentation
slides and notes
Control Structure:
Repetition
Topic 3
Content
Introduction
The for loop
Nested for loop
The while loop
Counter-controlled while loop
Sentinel-controlled while loop
The do..while loop
break and continue statement
3
Introduction
The repetition structure is a section of repeating
codes in a program, which is a loop
The loop contains a set of statements to be
executed repeatedly based on a condition
Three basic types of loops :
for
while
do..while
4
The for loop
The general form of the for statement is:
for (initial statement; loop condition; update statement)
{
statement1;
statement2;
statement3;
}
The initial statement, loop condition, and update
statement are called for loop control statements
5
The for loop (cont.)
6
The for loop (cont.)
for (initial statement; loop condition; update statement)
The initial statement consists of a single
statement used to set the initial value or starting value of
a variable called counter variable.
The loop condition tests the value of the counter
variable and determines when the loop is to stop.
The update statement (expression) provides
the increment value added or subtracted from the
counter variable each time the loop is executed.
For example:
for (int i = 1; i <= 3; i++)
cout << “*”;
7
The for loop (cont.)
During the execution of the for statement, the following
steps of action occurs :
1. The initial statement is evaluated.
2. The loop condition is evaluated.
3. If the loop condition evaluates to true, then
i. statement is executed
ii. execute the update statement (the third
expression in the parentheses).
iii. repeat Step 2 until the loop condition is false.
otherwise the for loop terminates and control
transfers to the next statement following it.
8
The for loop (cont.)
The use of for loops are shown as follows :
Example 1 : This loop prints the first 10 non-negative integers.
Step 1 Step 2 i
for (int i = 1; i <= 3; i++) 1
2
4
3
Step 3
cout << i << “ ”; Step 4
false
cout << endl;
i Condition cout << i << “ ”; i++
1 1 <=3 true 1 i=1+2=2
2 <=3 true 2 i=2+1=3
3 <=3 true 3 i=3+1=4
4 <=3 false
9
The for loop (cont.)
Example 2 :
10
The for loop (cont.)
Example 3 :
11
The for loop (cont.)
Exercise 1: Write a program segment that display all the
numbers from 1 to 20.
Exercise 2: Write a program segment that display all the even
numbers from 10 to 50.
Exercise 3: Write a program segment that asks the user to
input 5 numbers.
Exercise 4: Write a program segment that asks the user to
input 5 numbers and display the average of the numbers.
12
The for loop (cont.)
Counting the number of repetition :
Symmetric loop (a <= n <= b)
Example :
for (i = 10; i <= 40; i += 5)
the counts is
(b – a)/c+1 = (40-10)/5 + 1 = 7
Asymmetric loop (a <= n < b)
for (i = 10; i < 40; i += 5)
the counts is
(b – a)/c = (40-10)/5 = 6
13
The for loop (cont.)
2 forms of the for statement :
Ascending for loop
Example :
for (variable=initial_value; variable <=
final_value; increment expression)
e.g : for (i = 10; i <= 40; i += 5)
Descending for loop
for (variable=initial_value; variable >=
final_value; decrement expression)
e.g : for (i = 40; i >= 10; i -= 5)
14
The nested for loop
Loop contained within another loop
Example 4 :
15
The nested for loop (cont.)
Example 5 :
How to display a pattern of numbers like this :
*
* *
* * *
* * * *
* * * * *
16
The nested for loop (cont.)
Example 6 :
How to display a pattern of numbers like this :
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
17
The while loop
Required for repetitive execution that cannot be determined
in advance
The syntax :
while (condition)
statement;
statement can be simple or compound; the body of the
loop
condition acts as a decision maker and is usually a logical
expression
The parentheses are part of the syntax
18
The while loop (cont.)
condition provides an entry condition
statement executes if the expression initially evaluates to
true
Loop condition is then re-evaluated
Statement continues to execute until the expression is no
longer true (false)
19
The while loop (cont.)
Counter-Controlled while Loops :
If you know exactly how many pieces of data need to be
read, the while loop becomes a counter-controlled loop
20
The while loop (cont.)
Counter-Controlled while Loops :
Example 7 :
21
The while loop (cont.)
Exercise 1: Write a program segment that display all the
numbers from 1 to 20.
Exercise 2: Write a program segment that display all the even
numbers from 10 to 50.
Exercise 3: Write a program segment that asks the user to
input 5 numbers.
Exercise 4: Write a program segment that asks the user to
input 5 numbers and display the average of the numbers.
22
The while loop (cont.)
Sentinel-Controlled while Loops :
Sentinel variable is tested in the condition and loop ends when
sentinel is encountered.
23
The while loop (cont.)
24
The while loop (cont.)
Exercise 1: Write a segment to repeat a statement until the
user enter the sentinel value of ‘N’. The repeated statement is
“Do you want to continue? Y for Yes, N for No”
Exercise 2: Write a program segment that asks the user to
input any numbers until it reads 999 to stop. Then display the
total of the numbers.
25
The do..while loop (cont.)
The general form of a do...while statement is:
do
statement
while (expression);
The statement executes first, and then the
expression is evaluated
If the expression evaluates to true, the
statement executes again
As long as the expression in a do...while
statement is true, the statement executes
26
The do..while loop (cont.)
27
The do..while loop (cont.)
28
The do..while loop (cont.)
29
The do..while loop (cont.)
30
‘break’ & ‘continue’ statement
A break statement forces an immediate exit when
executed in a while, for, do while or switch statement
A continue statement skips the remaining statements
in the body of the structure and proceeds with the next
iteration of the loop
31
‘break’ & ‘continue’ statement
32