Programming Languages Chapter 5 Programming From Problem Analysis Program Design Eighth Edition Control Structures Repetition

subject Type Homework Help
subject Pages 9
subject Words 2742
subject Authors D. S. Malik

Unlock document.

This document is partially blurred.
Unlock all pages and 1 million more documents.
Get Access
page-pf1
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 5-1
Chapter 5
Control Structures II (Repetition)
A Guide to this Instructor’s Manual:
We have designed this Instructor’s Manual to supplement and enhance your teaching
experience through classroom activities and a cohesive chapter summary.
This document is organized chronologically, using the same headings that you see in the
textbook. Under the headings, you will find lecture notes that summarize the section, Teacher
Tips, Classroom Activities, and Lab Activities. Pay special attention to teaching tips and
activities geared towards quizzing your students and enhancing their critical thinking skills.
In addition to this Instructor’s Manual, our Instructor’s Resources also contain PowerPoint
Presentations, Test Banks, and other supplements to aid in your teaching experience.
At a Glance
Instructor’s Manual Table of Contents
Overview
Objectives
Teaching Tips
Quick Quizzes
Class Discussion Topics
Additional Projects
Additional Resources
Key Terms
page-pf2
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 5-2
Lecture Notes
Overview
In Chapter 5, students learn how repetition structures are incorporated into programs.
Students will explore different types of loops, including counter-controlled, sentinel-
controlled, flag-controlled, and EOFcontrolled loops. The three C++ repetition
structures will be introduced: while loops, for loops, and do…while loops. Students
will also examine the use of break and continue statements in loops. Finally, they
will learn how to incorporate nested repetition and selection structures into their
programs.
Objectives
In this chapter, the student will:
Learn about repetition (looping) control structures
Learn how to use a while loop in a program
Explore how to construct and use counter-controlled, sentinel-controlled, flag-
controlled, and EOFcontrolled repetition structures
Learn how to use a for loop in a program
Learn how to use a do…while loop in a program
Examine break and continue statements
Discover how to form and use nested control structures
Learn how to avoid bugs by avoiding patches
Learn how to debug loops
Teaching Tips
Why is Repetition Needed?
1. Explain why repetition structures are invaluable to a programming language. Discuss
why repeating a set of statements in a program using a loop is preferable to typing in the
statements over and over.
Teaching
Tip
Students may have felt somewhat limited with previous programming
assignments because of the inability to use several sets of input. Discuss how
programs from previous chapters, such as the Calculator program, could be
enhanced with repetition structures.
page-pf3
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 5-3
while Looping (Repetition) Structure
2. Describe the flow of execution in a while loop using Figure 5-1, and define the term
infinite loop.
Teaching
Tip
Your students will probably program an infinite loop at some point during this
class. Explain how they can stop program execution when running a program in
which they have inadvertently programmed an infinite loop (CTRL + C on most
systems). Demonstrate with a simple program.
3. Discuss the proper use of a loop control variable using Example 5-1.
Designing while Loops
1. Use Example 5-2 to explain that the expression in a while loop typically checks
Case 1: Counter-Controlled while Loops
1. Explain that a counter-controlled loop is useful when you know how many times a set
of statements needs to be executed.
2. Describe how to initialize and update the counter variable in a while loop using
Example 5-3.
Teaching
Tip
Initializing and incrementing a counter variable can be tricky, especially because
they vary slightly among different types of looping structures. As you discuss the
loop structures in this chapter, emphasize the steps for implementing an
appropriate loop control variable for each of them. For the while loop, note that
the counter must be set initially before the loop executes for the first time, and
then updated again inside the loop for future executions.
Case 2: Sentinel-Controlled while Loops
1. Define the term sentinel and explain how it is used to terminate the execution of a
while loop. Use Example 5-4 to illustrate.
page-pf4
1. Step through the Telephone Digits program to illustrate how a user can be prompted to
enter a sentinel that terminates a program.
1. Explain that a flag-controlled while loop uses a bool variable, called a flag variable,
to control the loop.
Number Guessing Game (Example 5-6)
1. Step through the Number Guessing Game to demonstrate how a flag variable can be set
to true when the user enters the correct response.
Case 4: EOF-Controlled while Loops
1. Explain why the use of an EOF-controlled loop is sometimes preferable to a sentinel-
controlled loop.
2. Describe how the variable cin can act as a loop control variable by returning a false
value when there is no more input data.
eof Function
1. Introduce the C++ function eof and demonstrate how it is used to determine the end of
input data in a file.
2. Walk through Example 5-7 to demonstrate the use of an EOF-controlled while loop.
Teaching
Tip
Mention that when creating a data file that will be used for a program that relies
on the end-of-file marker, it might be wise to include a final newline at the end of
the data. Some compilers, such as gcc, require this to properly execute eof.
More on Expressions in while Statements
1. Explain that the expression in a while statement may be a complex logical expression
2. Discuss the implementations of the Fibonacci Number program at the end of this
section to consolidate the students’ understanding of the while loop repetition
page-pf5
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 5-5
Quick Quiz 1
1. How do you avoid an infinite loop?
2. True or False: The loop control variable for a while loop must be initialized before the
while loop.
3. The expression in a while loop checks whether a variable, called the
____________________, satisfies certain conditions.
LCV
4. A(n) ____________________ is an arbitrary value that, when read, stops the execution
for Looping (Repetition) Structure
2. Discuss the syntax of a for loop, including its three major components: the initial
statement, the loop condition, and the update statement.
3. Illustrate the flow of execution in a for loop using Figure 5-2.
Teaching
Tip
Emphasize the difference between a for loop and a while loop regarding the
initialization of the loop control variable.
Teaching
Tip
Ask your students to discuss situations in which they might want to use a
floating-point number with a fractional part for a loop control variable. What are
the pitfalls of using real numbers for the index variable in a for loop?
page-pf6
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 5-6
do…while Looping (Repetition) Structure
1. Explain the syntax of the do…while loop.
2. Describe the flow of execution in a do…while loop using Figure 5-3.
3. Look at Examples 5-18 and 5-19 to illustrate the use of a do…while loop.
Teaching
Tip
Discuss why you might want a loop to execute once before testing for a
condition. For example, a posttest loop is useful when the condition that
determines the end of the loop is calculated within the loop statement itself.
Divisibility Test by 3 and 9 (Example 5-20)
1. Use Example 5-20 to show how to use a do…while loop to determine whether a
positive integer is divisible by 3 and 9.
Choosing the Right Looping Structure
break and continue Statements
1. Explain that the break statement can be used in a loop structure to provide a means for
2. Introduce the continue statement and explain its use in loop structures. Emphasize
that it is different from a break statement in that it skips the remaining statements in
the loop and proceeds on with the next iteration of the loop.
Teaching
Tip
Discuss the unpredictable results that might occur from misplacing the
continue statement in a while or do…while loop. Emphasize that the loop
counter variable is incremented in a for loop regardless of the placement of a
continue statement.
page-pf7
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 5-7
Quick Quiz 2
1. How many times does the initial statement of a for loop execute?
2. True or False: A semicolon at the end of a for statement is a semantic error.
3. What is the purpose of the third expression of the for statement?
4. True or False: If the loop condition in a for statement is omitted, it is assumed to be
false.
Nested Control Structures
1. This section reviews the control structures introduced in this chapter and in Chapter 4.
2. Using Examples 5-21 through 5-25, demonstrate how using nested structures can
increase the functionality of a program.
Avoiding Bugs by Avoiding Patches
1. Define a software patch and explain that the following section will instruct students on
avoiding patches. Introduce the sample program beginning on page 322. Stress the
importance of fixing the underlying cause of an error rather than “treating the
symptoms.”
Debugging Loops
1. Discuss some of the issues specific to debugging loops. Explain how to use loop
invariants.
Class Discussion Topics
1. Discuss the fact that it is important to test for invalid input for the initial sentinel value
when using while loops. How might you modify the code in Examples 5-4 and 5-5 to
account for an invalid initial sentinel value? In other words, you would like the
page-pf8
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
2. You can use the repetition structures in this chapter interchangeably for many
circumstances. There are a few occasions when one is clearly preferable to another.
Review these, give an overview of all of the repetition structures discussed in the
chapter, and then discuss how to determine which structure to use when there is no clear
indication. How much of the choice should be based on personal preference and how
much on conventional practices? Are there performance issues as well?
Additional Projects
1. Ask your students to write a menu-driven program that calculates the total price for a
picnic lunch that the user is purchasing for a group of friends. The user is first asked to
enter his or her budget for the lunch. The user has the option of buying apples, cheese,
2. Modify the Telephone Digits program in this chapter to allow for enhanced user input
and error checking. In the Telephone Digits program, rather than terminating the
Additional Resources
2. The for loop:
4. Control Flow:
page-pf9
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 5-9
Key Terms
Counter-controlled while loop: a while loop that is used when you know how
many items of data are to be read; the loop will continue until the condition designated
by the counter is met or evaluates to false
Decision maker: an expression in an if statement, which determines whether to
execute the statement that follows it
Divisor: suppose that m and n are integers and m is nonzero. Then m is called a divisor
of n if n = mt for some integer t; that is, when m divides n, the remainder is 0.
End-of-file (EOF)-controlled while loop: a while loop that stops when it reaches
the end of the input file
Fibonacci number: a number in the Fibonacci sequence
Fibonacci sequence: an = an-1 + an-2
Flag variable: a Boolean variable used to control the execution of a while loop
Flag-controlled while loop: uses a Boolean variable to control the execution of the
loop
for loop (indexed loop): used to simplify the writing of counter-controlled loops;
consists of an initialization statement, the loop condition, and the update statement

Trusted by Thousands of
Students

Here are what students say about us.

Copyright ©2022 All rights reserved. | CoursePaper is not sponsored or endorsed by any college or university.