0% found this document useful (0 votes)
35 views

Basic Control Flow: Repetitions and Loop Statements: TCB2073 Structured Programming & Database Systems

The document discusses C++ repetition structures like while, for, and do-while loops. It provides examples of using these loops to repeat tasks like printing asterisks or summing numbers. Key terms discussed include loop body, counter-controlled loop, loop repetition condition, and sentinel value. Nested loops are also covered, with examples of how an inner loop iterates within each iteration of an outer loop. Problems are provided to help understand loop structures, including printing patterns using nested loops based on a user-input number.

Uploaded by

Hyomin
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Basic Control Flow: Repetitions and Loop Statements: TCB2073 Structured Programming & Database Systems

The document discusses C++ repetition structures like while, for, and do-while loops. It provides examples of using these loops to repeat tasks like printing asterisks or summing numbers. Key terms discussed include loop body, counter-controlled loop, loop repetition condition, and sentinel value. Nested loops are also covered, with examples of how an inner loop iterates within each iteration of an outer loop. Problems are provided to help understand loop structures, including printing patterns using nested loops based on a user-input number.

Uploaded by

Hyomin
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Basic Control Flow: Repetitions and Loop

Statements

TCB2073
Structured Programming & Database
Systems
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

Page 2 of 13
Control Structures Revisit
Sequence Selection Repetition

Page 3 of 13
C++ Repetition Structures

Page 4 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

Page 5 of 13
General Flowchart for while Statement
(counter-controlled loop)

initialisation

testing

Loop body

updating
Page 6 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

Page 7 of 13
Problem 1: Print N (qty) asterisks

 Perform desk check!

Page 8 of 13
Problem 2: Descending order (N to 0)

 Perform desk check!

Page 9 of 13
Problem 3: Sum a series of N numbers

 Perform desk check!

Page 10 of 13
Problem 4: Find the average of a series of N
numbers

Page 11 of 13
do .. while statement
 executes the statements in the loop body at least once

Loop body

updating

testing

Page 12 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);

Page 13 of 15
Problem 1: Print N (qty) asterisks

start

counter = 0

Get qty

Display *

counter ++

F T
Counter < qty ?

 Perform desk check!


End

Page 14 of 15
do .. while Counting Stars

Page 15 of 15
Problem 3: to sum N numbers,
without knowing N

updating

testing

Page 16 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

Page 17 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 <<” * “;
}

Page 18 of 15
Problem 2: Descending order (N to 0)

intialisation

 testing

updating

 Perform desk check! Page 19 of 15


Nested Loop
 When a task performs a repetitive operation and that task
itself must be repeated.
 Examples:
– Test-averaging program for a number of students.
• Outer loop – repeats n students
• Inner loop – repeats n tests
– A digital clock with hour, minute and second.
• Outer loop – repeats 24 times
• Middle loop - repeats 60 times of each outer loop
Inner loop - repeats 60 times of each middle loop
 An inner loop goes through all of its iterations for each
iteration of an outer loop.

Page 20 of 13
NESTED LOOP
 The inner loop must be completely reside inside the outer
loop with a different control variable
 Some possible structures of Nested For:

For I For I
For J
For J

For K

End J
For K
End K

End J
End K
End I End I
Page 21 of 13
Example

 Perform desk check!

Page 22 of 13
Trace the output

Page 23 of 13
Class Average

Page 24 of 13
Digital Clock
Output:
00:00:00
00:00:01
.
.
.
23:59:59

Outermost loop has iterated 24 times


Middle loop will have iterated 1,440 times
Innermost loop will have iterated 86, 4000 times!
Page 25 of 13
Problem #1
 Write a nested loop to produce the following pattern if the
user input is 5

Page 26 of 6
Problem #2
 Write a nested loop to produce the following pattern if the
user input is 5

Page 27 of 6
Problem #3
 Write a nested loop to produce the following pattern if the
user input is 5

Page 28 of 6
Summary

Page 29 of 13

You might also like