You are on page 1of 29

Basic Control Flow: Repetitions and Loop

Statements
TCB2073
Structured Programming & Database
Systems
TBB1073 : Basic Control Flow -Repetition and loop statements Page 2 of 13
Objectives
By the end of this lecture, you will be able to:
describe the C++ repetition structures (while, for,
do..while)
design and implement a C++ program that involves
repetition structures
use SENTINEL in a do .. while statement
desk check loop statement

TBB1073 : Basic Control Flow -Repetition and loop statements Page 3 of 13
Control Structures Revisit

Sequence Selection Repetition
TBB1073 : Basic Control Flow -Repetition and loop statements Page 4 of 13
C++ Repetition Structures

TBB1073 : Basic Control Flow -Repetition and loop statements Page 5 of 13
Terms
loop a control structure that repeats a group of steps in
a program.
loop body the statements that are repeated in the loop.
counter-controlled loop (counting loop) a loop whose
required number of iterations can be determined before
loop execution begins
loop repetition condition the condition that controls
loop repetition
loop control variable the variable whose value controls
loop repetition
infinite loop a loop that executes forever

TBB1073 : Basic Control Flow -Repetition and loop statements Page 6 of 13
General Flowchart for while Statement
(counter-controlled loop)

initialisation
testing
updating
Loop body
TBB1073 : Basic Control Flow -Repetition and loop statements Page 7 of 13
While Statement Syntax

SYNTAX: while (loop repetition condition)
statement
EXAMPLE: /*display N asterisks */
count_star = 0;
while (count_star < N) {
cout << * ;
count_star = count_star + 1;
}

INTERPRETATION : The statements in the body
loop are repeated as long as the loop repetition condition is
true. The while loop is exited when the repetition condition
is false
TBB1073 : Basic Control Flow -Repetition and loop statements Page 8 of 13
Problem 1: Print N (qty) asterisks















Perform desk check!
TBB1073 : Basic Control Flow -Repetition and loop statements Page 9 of 13
Problem 2: Descending order (N to 0)












Perform desk check!
TBB1073 : Basic Control Flow -Repetition and loop statements Page 10 of 13
Problem 3: Sum a series of N numbers













Perform desk check!
TBB1073 : Basic Control Flow -Repetition and loop statements Page 11 of 13
Problem 4: Find the average of a series of N
numbers

TBB1073 : Basic Control Flow -Repetition and loop statements Page 12 of 15
for Statement Syntax

SYNTAX: for (initialisation expression;
loop repetition condition;
update expression)
statement
EXAMPLE: /*display N asterisks */
for (count_star = 0;
count_star < N;
count_star++) {
cout << * ;
}

TBB1073 : Basic Control Flow -Repetition and loop statements Page 13 of 15
Problem 1: Print N (qty) asterisks















Perform desk check!












Perform desk check!
TBB1073 : Basic Control Flow -Repetition and loop statements Page 14 of 15
Problem 2: Descending order (N to 0)
intialisation
testing
updating
TBB1073 : Basic Control Flow -Repetition and loop statements Page 15 of 15
do .. while statement
executes the statements in the loop body at least once

Loop body
updating
testing
TBB1073 : Basic Control Flow -Repetition and loop statements Page 16 of 15
do .. while statement syntax

SYNTAX: do {
statement
} while(loop repetition condition);

EXAMPLE: /*display N asterisks */
count_star = 0;
do {
cout << * ;
count_star++;
} while (count_star < 5);

TBB1073 : Basic Control Flow -Repetition and loop statements Page 17 of 15
do .. while Counting Stars


TBB1073 : Basic Control Flow -Repetition and loop statements Page 18 of 15
Problem 3: to sum N numbers,
without knowing N
updating
testing
TBB1073 : Basic Control Flow -Repetition and loop statements Page 19 of 15
SENTINEL
Sentinel value an end marker that follows the last item
in a list of data
e.g
#define SENTINEL 999

List of data:
56 90 85 100 72 45 999


Sentinel value
The danger of using == and/or
!=
for loop to hand-trace
What is the output?
for (int i = 0;
i != 9;
i += 2)
cout << i << " ";





Infinite Loops Can Occur in for Statements
Infinite Loops Can Occur in for Statements
for loop
The output never ends
0 2 4 6 8 10 12




for (int i = 0;
i != 9;
i += 2)
cout << i << " ";
= = and != are best avoided
in the check of a for statement
Nested Loops
Nested loops are used mostly for data in tables as rows
and columns.

The processing across the columns is a loop, as you have
seen before, nested inside a loop for going down the
rows.

Each row is processed similarly so design begins at that
level. After writing a loop to process a generalized row,
that loop, called the inner loop, is placed inside an
outer loop.
Nested Loop Examples
The loop variables can have a value relationship.
In this example the inner loop depends on the value of the
outer loop.


for (i = 1; i <= 4; i++){
for (j = 1; j <= i; j++)
cout << "*";
cout << endl;
}

The output will be:

*
**
***
****
for (i = 1; i <= 4; i++){
for (j = 1; j <= i; j++)
cout << "*";
cout << endl;}




j stops at: i i i i
1 2 3 4

line num. i 1 *
i 2 * *
i 3 * * *
i 4 * * * *
Nested Loop Examples
i represents the
row number or the
line number
j is the number of columns (or
the lines length), which depends
on the line number, i
Sample table of data
Use nested loop to create the following
output:


1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
TBB1073 : Basic Control Flow -Repetition and loop statements Page 26 of 6
Problem #1
Write a nested loop to produce the following pattern if
the user input is 5



TBB1073 : Basic Control Flow -Repetition and loop statements Page 27 of 6
Problem #2
Write a nested loop to produce the following pattern if
the user input is 5

TBB1073 : Basic Control Flow -Repetition and loop statements Page 28 of 6
Problem #3
Write a nested loop to produce the following pattern if
the user input is 5

Summary
TBB1073 : Basic Control Flow -Repetition and loop statements Page 29 of 13

You might also like