Title: Chapter 6: More on the Selection Structure
1Chapter 6More on the Selection Structure
- Introduction to Programming with C
- Fourth Edition
2Objectives
- Include a nested selection structure in
pseudocode and in a flowchart - Code a nested selection structure in C
- Recognize common logic errors in selection
structures
3Objectives (continued)
- Include the switch form of the selection
structure in pseudocode and in a flowchart - Code the switch form of the selection structure
in C - Format numeric output in C
4Nested Selection Structures
- Nested selection structure
- Selection structure within another selection
structure - Used when more than one decision must be made
before the appropriate action can be taken - Primary decision - always made by the outer
selection structure - Secondary decision - always made by the inner
(nested) selection structure
5Nested Selection Structures (continued)
6Nested Selection Structures (continued)
7Logic Errors in Selection Structures
- Logic errors are commonly made as a result of the
following mistakes - Using a logical operator rather than a nested
selection structure - Reversing the primary and secondary decisions
- Using an unnecessary nested selection structure
8Logic Errors in Selection Structures (continued)
9Logic Errors in Selection Structures (continued)
- Test Data for Desk-Checking
- Data for first desk-check
- Status F
- Years 4
- Data for second desk-check
- Status F
- Years 15
- Data for third desk-check
- Status P
- Years 11
10Results of Desk-Checking the Correct Algorithm
11Using a Logical Operator Rather Than a Nested
Selection Structure
- One common error made when writing selection
structures is to use a logical operator in the
outer selection structures condition when a
nested selection structure is needed
12Correct Algorithm and an Incorrect Algorithm
Containing the First Logic Error
13Results of Desk-Checking the Incorrect Algorithm
14Reversing the Primary and Secondary Decisions
- Common error putting the secondary decision in
the outer selection structure, and putting the
primary decision in the nested selection structure
15Correct Algorithm and an Incorrect Algorithm
Containing the Second Logic Error
16Results of Desk-Checking the Incorrect Algorithm
17Using an Unnecessary Nested Selection Structure
- In most cases, a selection structure containing
this error still produces the correct results - However, it is less efficient than selection
structures that are properly structured
18Correct Algorithm and an Inefficient Algorithm
Containing the Third Logic Error
19Results of Desk-Checking the Inefficient Algorithm
20Using the if/else Form to Create Multiple-Path
Selection Structures
21Two Versions of the C Code for the Grade Problem
22Two Versions of the C Code for the Grade
Problem (continued)
23Using the switch Form to Create Multiple-Path
Selection Structures
24 Using the switch Form
- Switch statement
- Begins with switch followed by an open brace
- Ends with a closing brace
- Switch clause - keyword switch followed by a
selectorExpression enclosed in parentheses - selectorExpression
- Can contain any combination of variables,
constants, functions, methods, and operators - Must result in a bool, char, short, int, or long
25Using the switch Form (continued)
- Each clause in the switch statement contains a
value followed by a colon - Data type of the value should be compatible with
the data type of the selectorExpression - Break statement - tells the computer to leave
(break out of) the switch statement at that
point
26Formatting Numeric Output
- Use setiosflags stream manipulator to display a
programs numeric output in fixed-point notation
only - Must use include ltiomanipgt directive and
- using stdios and using stdsetiosflags
- Use setprecision to control the number of decimal
places displayed
27Formatting Numeric Output (continued)
28Formatting Numeric Output (continued)
29Summary
- Nested selection structure
- Selection structure within another selection
structure - Used when more than one decision must be made
before the appropriate action can be taken - Three common logic errors in selection structures
- Logic errors are commonly made as a result of the
following mistakes - Using a logical operator rather than a nested
selection structure - Reversing the primary and secondary decisions
- Using an unnecessary nested selection structure
30Summary (continued)
- The switch form of the selection structure is
often simpler to use if there are many paths from
which to choose - Format output using stream manipulators