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

Chapter 4 - Control Structures - Repetition

Uploaded by

johnmf1011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Chapter 4 - Control Structures - Repetition

Uploaded by

johnmf1011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

©2017 Pearson Education, Inc.

Hoboken,
NJ. All Rights Reserved.

Chapter 4
Control Structures:
Repetition
Outline
Objectives
1. Algorithm Development
2. Repetition Structures
3. Problem Solving Applied: GPS Data
4. C++ break and continue Statements
5. Structuring Input Loops
6. Problem Solving Applied: Weather Balloons
7. Building C++ Solutions with IDEs: Microsoft Visual C++

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
Objectives
Develop problem-solving solutions in
C++ containing:
• Repetition structures that allow us to
repeat a set of steps as long as a
condition is true.
• Three common forms of input loops
• Algorithm development and descriptions of
loops using flowcharts and pseudo code.

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
Algorithm Development

• Algorithms using repetition structures can be


described with
• Pseudocode
• Flowcharts

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
Example: Computing Velocity
over a Range of Time.

• Sequential structures
are not well suited for
computing the same
quantities repeatedly.
• This flow chart
describes repetition best
implemented with a
while statement.

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
Repetition Structures
• while Statement

• do while Statement

• for Statement

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
The while Statement:
Syntax and flowchart
Syntax:
while (boolean_expression) while (boolean_expression) {
statement; statement1;

statement_n;
}
Examples
while (!isspace(ch))
cin.get(ch); //statement repeated until
// whitespace is read

while (x>y) { //statement block executed


//while x>0
++cl;
--x;
}

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
Infinite Loops
• An infinite loop is one in which the
loop condition is always true.
– Selective execution of a break statement or
aborting/exiting the program are methods of
terminating an infinite loop.

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
Avoiding Bugs
• When writing long programs, it is not uncommon
to have a large number of error messages the
first time you compile.
– A single error often causes several errors as the
compiler gets ‘confused’ over what follows an error.
– Start correcting errors from the top of the error list, NOT
the bottom.
– Recompile your program frequently, limiting the places
where errors may occur.
• Use endl in output statements that are used for
debugging to be sure the output buffer is flushed
and text is seen on screen.

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
The do-while Statement:
Syntax and flowchart
Syntax:
do do {
statement; statement1;
while (boolean_expression); …
statement_n;
} while (boolean_expression);

Examples
do
cin.get(ch); //statement repeated until
while (ch!=‘\n’) // newline character read

do { //statement block executed


v = a*t + v0;
cout << t << “ “ << v << endl;
t += 0.5;
} while (t<10); //while t< 10
©2017 Pearson Education, Inc. Hoboken, NJ. All
Rights Reserved.
The for Statement

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
The for Statement
Syntax:
for (initialization_stmt; boolean_condition; modification_stmt) {
statement(s);
}

Examples
int sum=0;
for (int i = 1; i <= 10; i++)
sum += i;
cout << sum << endl;

int sum=0;
for (int i = 0; i < 10; ++i)
sum += i;
cout << sum << endl;

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
Multiple Initialization and
Modification Statements
• When multiple statements are
required in a for loop’s initialization or
modification, they are separated by commas.
– The comma operator has the lowest operator
precedence and is left-associative.

for(int k=0,j=0; k<10; ++k,++j)


{
sum1 += k;
sum2 += j;
}

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
Problem Solving Applied:
GPS Data

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
Problem Solving Applied:
GPS Data

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
Problem Solving Applied:
GPS Data

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
Problem Solving Applied:
GPS Data

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
Problem Solving Applied:
GPS Data

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
Problem
Solving
Applied:
GPS Data

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
C++ break and continue
Statements

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
The break Statement
• break;
– terminates loop
– execution continues with the first statement
following the loop

Example: What is the output? 0


for (int i=0; i<10; ++i) {
if(i%2) break;
cout << i << endl;
}

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
The continue Statement
• continue;
– forces next iteration of the loop, skipping any
remaining statements in the loop

Example: What is the output? 0


for (int i=0; i<10; ++i) { 2
4
if(i%2) continue;
6
cout << i << endl; 8
}

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
Structuring Input Loops
• Repetition is useful when inputting
data from standard input or from a file.
• Common repetition structures:
– counter-controlled
– sentinel-controlled
– end-of-data controlled

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
Counter-Controlled
Loops
• May be used for
reading input data if
the number of data
values is known
before the data are
entered.

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
Sentinel-Controlled
Loops
• May be used for
reading input data if
a special data value
exists that can be
used to indicate the
end of data.

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
End-of-data Controlled
Loops
• Is the most flexible for
reading input data.
• No prior knowledge of
the number of data
values is required.
• No sentinel value is
required.

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
Problem Solving Applied:
Weather Balloons

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
Problem Solving Applied:
Weather Balloons

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
Problem Solving Applied:
Weather Balloons

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
Problem Solving Applied:
Weather Balloons

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
Building C++ Solutions with
IDEs:
Microsoft Visual C++

Visual C++ is an IDE developed by Microsoft for the


design and development of C and C++ program
solutions.

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
Divide-and-Conquer Algorithm:
Guess a number between 1 and 20.
Pseudocode
main: print greeting to user
while(!done && high > = low)
guess = (high + low)/2
print guess
if guess equals number
print number of tries required to guess
number
done = true
else if guess is larger than number
increment number of tries
high = guess - 1
else if guess is smaller than number
increment number of tries
low = guess + 1
©2017 Pearson Education, Inc. Hoboken, NJ. All
endRights
while
Reserved.

You might also like