chapter 5 selection making decisions - bumsoo kim · computer science: a structured programming...

101
Computer Science: A Structured Programming Approach Using C 1 Objectives To understand how decisions are made in a computer To understand the logical operators: and, or, and not To understand how a C program evaluates a logical expression To write programs using logical and comparative operators To write programs that use two-way selection: if ... else statements To write programs that use multi-way selection: switch and else ...if To understand C’s classification of characters To write programs that use C’s character functions To be able to design a structure chart that specifies function selection Chapter 5 SelectionMaking Decisions

Upload: others

Post on 12-Oct-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 1

Objectives ❏ To understand how decisions are made in a computer

❏ To understand the logical operators: and, or, and not

❏ To understand how a C program evaluates a logical expression

❏ To write programs using logical and comparative operators

❏ To write programs that use two-way selection: if ... else statements

❏ To write programs that use multi-way selection: switch and else ...if

❏ To understand C’s classification of characters

❏ To write programs that use C’s character functions

❏ To be able to design a structure chart that specifies function selection

Chapter 5

Selection—Making Decisions

Page 2: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 2

5-1 Logical Data and Operators

A piece of data is called logical if it conveys the idea of

true or false. In real life, logical data (true or false)

are created in answer to a question that needs a yes–

no answer. In computer science, we do not use yes or

no, we use true or false.

Logical Data in C

Logical Operators

Evaluating Logical Expressions

Comparative Operators

Topics discussed in this section:

Page 3: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 3

FIGURE 5-1 true and false on the Arithmetic Scale

Page 4: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 4

FIGURE 5-2 Logical Operators Truth Table

Page 5: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 5

FIGURE 5-3 Short-circuit Methods for and /or

x && y++ x || y++

Page 6: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 6

PROGRAM 5-1 Logical Expressions

Page 7: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 7

PROGRAM 5-1 Logical Expressions

Page 8: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 8

FIGURE 5-4 Relational Operators

Page 9: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 9

FIGURE 5-5 Comparative Operator Complements

Page 10: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 10

Table 5-1 Examples of Simplifying Operator Complements

Page 11: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 11

PROGRAM 5-2 Comparative Operators

Page 12: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 12

PROGRAM 5-2 Comparative Operators

Page 13: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 13

5-2 Two-Way Selection

The decision is described to the computer as a

conditional statement that can be answered either true

or false. If the answer is true, one or more action

statements are executed. If the answer is false, then a

different action or set of actions is executed.

if…else and Null else Statement

Nested if Statements and Dangling else Problem

Simplifying if Statements

Conditional Expressions

Topics discussed in this section:

Page 14: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 14

FIGURE 5-6 Two-way Decision Logic

Page 15: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 15

FIGURE 5-7 if...else Logic Flow

Page 16: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 16

Table 5-2 Syntactical Rules for if…else Statements

Page 17: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 17

FIGURE 5-8 A Simple if...else Statement

==

Page 18: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 18

FIGURE 5-9 Compound Statements in an if...else

Page 19: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 19

FIGURE 5-10 Complemented if...else Statements

Page 20: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 20

FIGURE 5-11 A Null else Statement

Page 21: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 21

FIGURE 5-12 A Null if Statement

Page 22: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

De Morgan’s Rule

!(x && y) !x || !y

!(x || y) !x && !y

Is it a good style?

Positive logic is easier to read and understand than negative logic.

Computer Science: A Structured Programming Approach Using C 22

Page 23: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 23

PROGRAM 5-3 Two-way Selection

Page 24: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 24

PROGRAM 5-3 Two-way Selection

Page 25: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 25

FIGURE 5-13 Nested if Statements

Page 26: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 26

PROGRAM 5-4 Nested if Statements

Page 27: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 27

PROGRAM 5-4 Nested if Statements

Page 28: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 28

else is always paired with the most recent unpaired if.

Note

Page 29: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 29

FIGURE 5-14 Dangling else

Page 30: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 30

FIGURE 5-15 Dangling else Solution

Page 31: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 31

Table 5-3 Simplifying the Condition

Page 32: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 32

FIGURE 5-16 Conditional Expression

Page 33: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 33

Table 5-4 Examples of Marginal Tax Rates

Page 34: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 34

FIGURE 5-17 Design for Calculate Taxes

Page 35: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 35

FIGURE 5-18 Design for Program 5-5 (Part I)

Page 36: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 36

FIGURE 5-18 Design for Program 5-5 (Part II)

Page 37: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 37

FIGURE 5-18 Design for Program 5-5 (Part III)

Page 38: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 38

PROGRAM 5-5 Calculate Taxes

Page 39: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 39

PROGRAM 5-5 Calculate Taxes

Page 40: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 40

PROGRAM 5-5 Calculate Taxes

Page 41: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 41

PROGRAM 5-5 Calculate Taxes

Page 42: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 42

PROGRAM 5-5 Calculate Taxes

Page 43: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 43

PROGRAM 5-5 Calculate Taxes

Page 44: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 44

PROGRAM 5-5 Calculate Taxes

Page 45: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 45

PROGRAM 5-5 Calculate Taxes

Page 46: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 46

PROGRAM 5-5 Calculate Taxes

Page 47: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 47

PROGRAM 5-5 Calculate Taxes

Page 48: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 48

5-3 Multiway Selection

In addition to two-way selection, most programming

languages provide another selection concept known as

multiway selection. Multiway selection chooses among

several alternatives. C has two different ways to

implement multiway selection: the switch statement

and else-if construct.

The switch Statement

The else-if

Topics discussed in this section:

Page 49: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 49

FIGURE 5-19 switch Decision Logic

Page 50: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 50

FIGURE 5-20 switch Statement Syntax

Page 51: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 51

FIGURE 5-21 switch Flow

Page 52: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 52

PROGRAM 5-6 Demonstrate the switch Statement

Page 53: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 53

FIGURE 5-22 switch Results

Page 54: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 54

FIGURE 5-23 A switch with break Statements

Page 55: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 55

PROGRAM 5-7 Multivalued case Statements

Page 56: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 56

Table 5-5 Summary of switch Statement Rules

Page 57: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 57

PROGRAM 5-8 Student Grading

Page 58: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 58

PROGRAM 5-8 Student Grading

Page 59: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 59

PROGRAM 5-8 Student Grading

Page 60: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 60

FIGURE 5-24 The else-if Logic Design for Program 5-9

Page 61: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 61

The else-if is an artificial C construct that is only used when

1. The selection variable is not an integral, and

2. The same variable is being tested in the expressions.

Note

Page 62: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 62

PROGRAM 5-9 Convert Score to Grade

Page 63: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 63

PROGRAM 5-9 Convert Score to Grade

Page 64: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 64

PROGRAM 5-9 Convert Score to Grade

Page 65: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 65

5-4 More Standard Functions

One of the assets of the C language is its rich set of

standard functions that make programming much

easier. For example, C99 has two parallel but separate

header files for manipulating characters: ctype.h and

wctype.h.

Standard Characters Functions

A Classification Program

Handling Major Errors

Topics discussed in this section:

Page 66: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 66

FIGURE 5-25 Classifications of the Character Type

Page 67: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 67

Table 5-6 Classifying Functions

continued

Page 68: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 68

Table 5-6 Classifying Functions (continued)

Page 69: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 69

Table 5-7 Conversion Functions

Page 70: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 70

PROGRAM 5-10 Demonstrate Classification Functions

Page 71: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 71

PROGRAM 5-10 Demonstrate Classification Functions

Page 72: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 72

5-5 Incremental Development Part II

In Chapter 4, we introduced the concept of

incremental development with a simple calculator

program. We continue the discussion by adding a

menu and calculator subfunctions.

Calculator Design

Calculator Incremental Design

Topics discussed in this section:

Page 73: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 73

FIGURE 5-26 Design for Menu-driven Calculator

Page 74: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 74

PROGRAM 5-11 Menu-driven Calculator—First Increment

Page 75: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 75

PROGRAM 5-11 Menu-driven Calculator—First Increment

Page 76: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 76

PROGRAM 5-11 Menu-driven Calculator—First Increment

Page 77: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 77

PROGRAM 5-11 Menu-driven Calculator—First Increment

Page 78: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 78

PROGRAM 5-12 Menu-driven Calculator—Third Increment

Page 79: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 79

PROGRAM 5-12 Menu-driven Calculator—Third Increment

Page 80: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 80

PROGRAM 5-12 Menu-driven Calculator—Third Increment

Page 81: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 81

PROGRAM 5-12 Menu-driven Calculator—Third Increment

Page 82: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 82

PROGRAM 5-12 Menu-driven Calculator—Third Increment

Page 83: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 83

PROGRAM 5-12 Menu-driven Calculator—Third Increment

Page 84: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 84

PROGRAM 5-12 Menu-driven Calculator—Third Increment

Page 85: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 85

PROGRAM 5-13 Menu-driven Calculator—Fifth Increment

Page 86: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 86

PROGRAM 5-13 Menu-driven Calculator—Fifth Increment

Page 87: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 87

PROGRAM 5-13 Menu-driven Calculator—Fifth Increment

Page 88: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 88

PROGRAM 5-13 Menu-driven Calculator—Fifth Increment

Page 89: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 89

PROGRAM 5-13 Menu-driven Calculator—Fifth Increment

Page 90: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 90

PROGRAM 5-13 Menu-driven Calculator—Fifth Increment

Page 91: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 91

PROGRAM 5-13 Menu-driven Calculator—Fifth Increment

Page 92: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 92

PROGRAM 5-13 Menu-driven Calculator—Fifth Increment

Page 93: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 93

PROGRAM 5-13 Menu-driven Calculator—Fifth Increment

Page 94: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 94

5-6 Software Engineering

In this section, we discuss some software engineering

issues related to decisions.

Dependent Statements

Negative Logic

Rules for Selection Statements

Selection in Structure Charts

Topics discussed in this section:

Page 95: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 95

PROGRAM 5-14 Examples of Poor and Good Nesting Styles

Page 96: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 96

Table 5-8 Indentation Rules

Page 97: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 97

Avoid compound negative statements!

Note

Page 98: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 98

Table 5-9 Complementing Expressions

Page 99: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 99

Table 5-10 Selection Rules

Page 100: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 100

FIGURE 5-27 Structure Chart Symbols for Selection

Page 101: Chapter 5 Selection Making Decisions - Bumsoo Kim · Computer Science: A Structured Programming Approach Using C 65 5-4 More Standard Functions One of the assets of the C language

Computer Science: A Structured Programming Approach Using C 101

FIGURE 5-28 Multiway Selection in a Structure Chart