Programming Languages Chapter 4 Programming From Problem Analysis Program Design Eighth Edition Control Structures Selection

subject Type Homework Help
subject Pages 9
subject Words 2740
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 4-1
Chapter 4
Control Structures I (Selection)
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 4-2
Lecture Notes
Overview
Thus far, your students have only written programs that execute statements in sequential
order, from beginning to end. Beginning in this chapter, students will learn how to write
code that executes based on certain conditions. Two major categories of control
structures will be discussed in this text: selection structures are introduced in this
chapter, and repetition structures in the next. In the process of learning about control
structures, students will also become familiar with relational operators and Boolean
expressions. Finally, they will learn how to handle input errors with the assert
function.
Objectives
In this chapter, the student will:
Learn about control structures
Examine relational operators
Discover how to use the selection control structures if, if…else
Examine int and bool data types and logical (Boolean) expressions
Examine logical operators
Explore how to form and evaluate logical (Boolean) expressions
Learn how relational operators work with the string type
Become aware of short-circuit evaluation
Learn how the conditional operator, ?:, works
Learn how to use pseudocode to develop, test, and debug a program
Discover how to use a switch statement in a program
Learn how to avoid bugs by avoiding partially understood concepts
Learn how to use the assert function to terminate a program
Teaching Tips
Control Structures
Selection: if and if…else
1. Introduce the concept of logical expression.
2. Review the relational operators using Table 4-1.
page-pf3
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 4-3
Teaching
Tip
Your students may have felt that the programs in the previous chapters were
rather pointless in terms of functionality. Once students are introduced to
conditional statements, they will begin to see the power of programming. Revisit
some of the programs in previous chapters, such as the Movie Ticket Sale
program, and discuss how they might be enhanced with conditional statements.
Relational Operators and Simple Data Types
2. Walk through the expressions in Example 4-1.
Teaching
Tip
Students are most likely familiar with Boolean logic; however, you might give a
brief review of truth tables to enhance the discussion of selection structures. Here
is one Web site with a simple introduction: http://www.learn-c.com/boolean.htm
Comparing Characters
1. Using the table on page 190, demonstrate how relational expressions with ASCII
characters are evaluated.
2. Review how C++ returns the result of logical expressions with the integers 0 or 1.
Quick Quiz 1
1. Name all the possible values of Boolean variables.
2. True or False: In C++, true and false are reserved words.
3. A(n) ____________________ operator allows you to make comparisons in a program.
4. The ____________________ operator in C++ is ==.
page-pf4
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 4-4
One-Way Selection
1. Explain the syntax of the if statement, which is the one-way selection structure in
2. Use Examples 4-3 through 4-5 to emphasize the correct syntax for the if statement and
Two-Way Selection
1. Discuss how C++ provides two-way selection through the if…else statement. Explain
the syntax of this statement.
2. Use Examples 4-6 through 4-9 to point out common coding errors when using multiple
selection statements.
int Data Type and Logical (Boolean) Expressions
1. Describe how the int data type is used in C++ to manipulate Boolean expressions.
bool Data Type and Logical (Boolean) Expressions
Logical (Boolean) Operators and Logical Expressions
2. Step through the truth tables (Tables 4-3 through 4-5) for the !, &&, and || operators.
Order of Precedence
1. Discuss the order of precedence of the relational and logical operators using Table 4-6.
Step through some of the more complex expressions in Examples 4-13 and 4-14 to
illustrate these rules.
Teaching
Tip
Students may need a little practice evaluating complex logical expressions. Write
the expressions shown in Examples 4-11 and 4-12 on the board and give the
class a few minutes to evaluate the results.
page-pf5
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 4-5
Relational Operators and the string Type
1. Explain that variables of type string are compared character by character in
sequence. Step through a few of the string expressions in Example 4-15 to illustrate
how strings are compared in varying circumstances.
Teaching
Tip
Emphasize OOP concepts by mentioning that the string comparison operators
are actually overloaded to compare string objects. Discuss how the equality
operator might be implemented.
Compound (Block of) Statements
1. Discuss the use of compound, or block, statements to allow the execution of more than
one statement after a branching decision has evaluated to true. Explain the use of
curly braces for multiple statements.
Teaching
Tip
Emphasize that curly braces are used for any type of block statements in C++,
not just selection structures. As an example, remind your students that the body
of the function main is also enclosed in curly braces, as are all functions.
Multiple Selections: Nested if
1. Define a nested if statement as a selection structure in which one control statement is
located within another.
2. Discuss how to correctly pair an else with an if. Step through some nested if
examples (4-16 through 4-20) to demonstrate the results of incorrectly paired if…else
statements.
Teaching
Tip
Discuss the conventions regarding indentation for multiple selection statements.
Mention that although the computer does not take indentation into account when
evaluating expressions, the code is much more readable and less prone to errors
when indented properly.
Comparing if…else Statements with a Series of if Statements
1. Compare the two if structures in this section and explain when one is preferable to the
other.
Short-Circuit Evaluation
page-pf6
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 4-6
© 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.
1. Introduce short-circuit evaluation and explain that C++ uses this type of evaluation for
logical expressions. Review Example 4-21.
Comparing Floating-Point Numbers for Equality: A Precaution
Associativity of Relational Operators: A Precaution
Avoiding Bugs by Avoiding Partially Understood Concepts and Techniques
1. Stress the importance of avoiding concepts and techniques that are not fully understood.
Input Failure and the if Statement
1. Describe how to stop program execution with an if statement when input failure
2. Step through the program on Pages 220-221 in this section to illustrate program
Confusion Between the Equality Operator (==) and the Assignment Operator (=)
1. Emphasize that inadvertently using an assignment operator instead of an equality
operator is an error that can cause serious problems in a program. Demonstrate with
some examples from this section.
Conditional Operator (?:)
1. Describe how the conditional operator is used to make branching code more concise.
Teaching
Tip
Although the conditional operator does make code more concise, its use
sacrifices the readability that if…else statements provide.
Program Style and Form (Revisited): Indentation
1. Stress the benefits of using proper indentation to show groups of related statements.
Following standard conventions can help to avoid and detect errors.
page-pf7
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 4-7
Using Pseudocode to Develop, Test, and Debug a Program
1. Define the term pseudocode and explain its usefulness in program development.
Emphasize that a walk-through of your pseudocode with test data can uncover both
syntactic and semantic errors.
Teaching
Tip
Mention that as students code more complex expressions, they will probably
encounter more errors while compiling. Assure them that this is a normal
process. Encourage them to write code in smaller segments and compile at
regular intervals for easier debugging.
Quick Quiz 2
1. The conditional expression portion of an if statement is sometimes called a(n)
____________________ because it decides whether to execute the statement that
follows it.
2. What is an action statement?
3. True or False: The if…else statement is a one-way selection structure.
4. A(n) ____________________ operator takes three operands.
switch Structures
1. Introduce the switch structure and describe its syntax in detail. Explain that the
switch structure allows the computer to choose among several alternatives.
2. Explain how a switch structure works using the flowchart in Figure 4-4.
3. Step through Examples 4-22 through 4-24 to demonstrate the use of the switch
page-pf8
1. Review the program listing on Pages 234-235 and explain the errors in the program.
Terminating a Program with the assert Function
2. Note the syntax for the assert function as well the header file in which it is included.
Class Discussion Topics
1. What are the advantages and disadvantages to using the int data type rather than the
bool data type to manipulate Boolean expressions? Why do students think the int
data type is still used for Boolean expressions?
2. You can use the selection structures in this chapter interchangeably for many
circumstances. There are a few occasions when one is clearly preferable to another
based on input data. Some of these situations were touched on in the switch statement
discussion. Review these, give an overview of all of the selection 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. Students have most likely encountered input failure or abnormal program termination
when attempting to run programming examples from previous chapters. Ask students to
2. Ask students to enhance the “Student Grade” Programming Example in Chapter 3 to
output a letter grade for the student after the average has been calculated. The grade
page-pf9
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 4-9
Additional Resources
2. Control Structures:
http://www.cplusplus.com/doc/tutorial/control/
Key Terms
Action statement: the statement following the expression in an if statement
Associativity: the order in which operators are grouped and evaluated
Compound statement (block of statements): consists of a sequence of statements
enclosed in curly braces, { and }
Conditional expression: an expression that uses a conditional operator
Conditional operator: a ternary operator written as “?:”; the three arguments explain

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.