Download presentation
Presentation is loading. Please wait.
Published bySherman Stanley Modified over 8 years ago
1
Computer Science: A Structured Programming Approach Using C1 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 Chapter 5 Selection—Making Decisions Selection—Making Decisions
2
Computer Science: A Structured Programming Approach Using C2 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:
3
Computer Science: A Structured Programming Approach Using C3 FIGURE 5-1 true and false on the Arithmetic Scale
4
Computer Science: A Structured Programming Approach Using C4 FIGURE 5-2 Logical Operators Truth Table
5
Computer Science: A Structured Programming Approach Using C5 FIGURE 5-3 Short-circuit Methods for and /or
6
Computer Science: A Structured Programming Approach Using C6 PROGRAM 5-1Logical Expressions
7
Computer Science: A Structured Programming Approach Using C7 PROGRAM 5-1Logical Expressions
8
Computer Science: A Structured Programming Approach Using C8 FIGURE 5-4 Relational Operators
9
Computer Science: A Structured Programming Approach Using C9 FIGURE 5-5 Comparative Operator Complements
10
Computer Science: A Structured Programming Approach Using C10 Table 5-1Examples of Simplifying Operator Complements
11
Computer Science: A Structured Programming Approach Using C11 PROGRAM 5-2Comparative Operators
12
Computer Science: A Structured Programming Approach Using C12 PROGRAM 5-2Comparative Operators
13
Computer Science: A Structured Programming Approach Using C13 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:
14
Computer Science: A Structured Programming Approach Using C14 FIGURE 5-6 Two-way Decision Logic
15
Computer Science: A Structured Programming Approach Using C15 FIGURE 5-7 if...else Logic Flow
16
Computer Science: A Structured Programming Approach Using C16 Table 5-2Syntactical Rules for if…else Statements
17
Computer Science: A Structured Programming Approach Using C17 FIGURE 5-8 A Simple if...else Statement
18
Computer Science: A Structured Programming Approach Using C18 FIGURE 5-9 Compound Statements in an if...else
19
Computer Science: A Structured Programming Approach Using C19 FIGURE 5-10 Complemented if...else Statements
20
Computer Science: A Structured Programming Approach Using C20 FIGURE 5-11 A Null else Statement
21
Computer Science: A Structured Programming Approach Using C21 FIGURE 5-12 A Null if Statement
22
Computer Science: A Structured Programming Approach Using C22 PROGRAM 5-3Two-way Selection
23
Computer Science: A Structured Programming Approach Using C23 PROGRAM 5-3Two-way Selection
24
Computer Science: A Structured Programming Approach Using C24 FIGURE 5-13 Nested if Statements
25
Computer Science: A Structured Programming Approach Using C25 PROGRAM 5-4Nested if Statements
26
Computer Science: A Structured Programming Approach Using C26 PROGRAM 5-4Nested if Statements
27
Computer Science: A Structured Programming Approach Using C27 else is always paired with the most recent unpaired if. Note
28
Computer Science: A Structured Programming Approach Using C28 FIGURE 5-14 Dangling else
29
Computer Science: A Structured Programming Approach Using C29 FIGURE 5-15 Dangling else Solution
30
Computer Science: A Structured Programming Approach Using C30 Table 5-3Simplifying the Condition
31
Computer Science: A Structured Programming Approach Using C31 FIGURE 5-16 Conditional Expression
32
Computer Science: A Structured Programming Approach Using C32 Table 5-4Examples of Marginal Tax Rates
33
Computer Science: A Structured Programming Approach Using C33 FIGURE 5-17 Design for Calculate Taxes
34
Computer Science: A Structured Programming Approach Using C34 FIGURE 5-18 Design for Program 5-5 (Part I)
35
Computer Science: A Structured Programming Approach Using C35 FIGURE 5-18 Design for Program 5-5 (Part II)
36
Computer Science: A Structured Programming Approach Using C36 FIGURE 5-18 Design for Program 5-5 (Part III)
37
Computer Science: A Structured Programming Approach Using C37 PROGRAM 5-5Calculate Taxes
38
Computer Science: A Structured Programming Approach Using C38 PROGRAM 5-5Calculate Taxes
39
Computer Science: A Structured Programming Approach Using C39 PROGRAM 5-5Calculate Taxes
40
Computer Science: A Structured Programming Approach Using C40 PROGRAM 5-5Calculate Taxes
41
Computer Science: A Structured Programming Approach Using C41 PROGRAM 5-5Calculate Taxes
42
Computer Science: A Structured Programming Approach Using C42 PROGRAM 5-5Calculate Taxes
43
Computer Science: A Structured Programming Approach Using C43 PROGRAM 5-5Calculate Taxes
44
Computer Science: A Structured Programming Approach Using C44 PROGRAM 5-5Calculate Taxes
45
Computer Science: A Structured Programming Approach Using C45 PROGRAM 5-5Calculate Taxes
46
Computer Science: A Structured Programming Approach Using C46 PROGRAM 5-5Calculate Taxes
47
Computer Science: A Structured Programming Approach Using C47 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:
48
Computer Science: A Structured Programming Approach Using C48 FIGURE 5-19 switch Decision Logic
49
Computer Science: A Structured Programming Approach Using C49 FIGURE 5-20 switch Statement Syntax
50
Computer Science: A Structured Programming Approach Using C50 FIGURE 5-21 switch Flow
51
Computer Science: A Structured Programming Approach Using C51 PROGRAM 5-6Demonstrate the switch Statement
52
Computer Science: A Structured Programming Approach Using C52 FIGURE 5-22 switch Results
53
Computer Science: A Structured Programming Approach Using C53 FIGURE 5-23 A switch with break Statements
54
Computer Science: A Structured Programming Approach Using C54 PROGRAM 5-7Multivalued case Statements
55
Computer Science: A Structured Programming Approach Using C55 Table 5-5Summary of switch Statement Rules
56
Computer Science: A Structured Programming Approach Using C56 PROGRAM 5-8Student Grading
57
Computer Science: A Structured Programming Approach Using C57 PROGRAM 5-8Student Grading
58
Computer Science: A Structured Programming Approach Using C58 PROGRAM 5-8Student Grading
59
Computer Science: A Structured Programming Approach Using C59 FIGURE 5-24 The else-if Logic Design for Program 5-9
60
Computer Science: A Structured Programming Approach Using C60 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
61
Computer Science: A Structured Programming Approach Using C61 PROGRAM 5-9Convert Score to Grade
62
Computer Science: A Structured Programming Approach Using C62 PROGRAM 5-9Convert Score to Grade
63
Computer Science: A Structured Programming Approach Using C63 PROGRAM 5-9Convert Score to Grade
64
Computer Science: A Structured Programming Approach Using C64 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:
65
Computer Science: A Structured Programming Approach Using C65 FIGURE 5-25 Classifications of the Character Type
66
Computer Science: A Structured Programming Approach Using C66 Table 5-6Classifying Functions continued
67
Computer Science: A Structured Programming Approach Using C67 Table 5-6Classifying Functions (continued)
68
Computer Science: A Structured Programming Approach Using C68 Table 5-7Conversion Functions
69
Computer Science: A Structured Programming Approach Using C69 PROGRAM 5-10Demonstrate Classification Functions
70
Computer Science: A Structured Programming Approach Using C70 PROGRAM 5-10Demonstrate Classification Functions
71
Computer Science: A Structured Programming Approach Using C71 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:
72
Computer Science: A Structured Programming Approach Using C72 FIGURE 5-26 Design for Menu-driven Calculator
73
Computer Science: A Structured Programming Approach Using C73 PROGRAM 5-11Menu-driven Calculator—First Increment
74
Computer Science: A Structured Programming Approach Using C74 PROGRAM 5-11Menu-driven Calculator—First Increment
75
Computer Science: A Structured Programming Approach Using C75 PROGRAM 5-11Menu-driven Calculator—First Increment
76
Computer Science: A Structured Programming Approach Using C76 PROGRAM 5-11Menu-driven Calculator—First Increment
77
Computer Science: A Structured Programming Approach Using C77 PROGRAM 5-12Menu-driven Calculator—Third Increment
78
Computer Science: A Structured Programming Approach Using C78 PROGRAM 5-12Menu-driven Calculator—Third Increment
79
Computer Science: A Structured Programming Approach Using C79 PROGRAM 5-12Menu-driven Calculator—Third Increment
80
Computer Science: A Structured Programming Approach Using C80 PROGRAM 5-12Menu-driven Calculator—Third Increment
81
Computer Science: A Structured Programming Approach Using C81 PROGRAM 5-12Menu-driven Calculator—Third Increment
82
Computer Science: A Structured Programming Approach Using C82 PROGRAM 5-12Menu-driven Calculator—Third Increment
83
Computer Science: A Structured Programming Approach Using C83 PROGRAM 5-12Menu-driven Calculator—Third Increment
84
Computer Science: A Structured Programming Approach Using C84 PROGRAM 5-13Menu-driven Calculator—Fifth Increment
85
Computer Science: A Structured Programming Approach Using C85 PROGRAM 5-13Menu-driven Calculator—Fifth Increment
86
Computer Science: A Structured Programming Approach Using C86 PROGRAM 5-13Menu-driven Calculator—Fifth Increment
87
Computer Science: A Structured Programming Approach Using C87 PROGRAM 5-13Menu-driven Calculator—Fifth Increment
88
Computer Science: A Structured Programming Approach Using C88 PROGRAM 5-13Menu-driven Calculator—Fifth Increment
89
Computer Science: A Structured Programming Approach Using C89 PROGRAM 5-13Menu-driven Calculator—Fifth Increment
90
Computer Science: A Structured Programming Approach Using C90 PROGRAM 5-13Menu-driven Calculator—Fifth Increment
91
Computer Science: A Structured Programming Approach Using C91 PROGRAM 5-13Menu-driven Calculator—Fifth Increment
92
Computer Science: A Structured Programming Approach Using C92 PROGRAM 5-13Menu-driven Calculator—Fifth Increment
93
Computer Science: A Structured Programming Approach Using C93 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:
94
Computer Science: A Structured Programming Approach Using C94 PROGRAM 5-14Examples of Poor and Good Nesting Styles
95
Computer Science: A Structured Programming Approach Using C95 Table 5-8Indentation Rules
96
Computer Science: A Structured Programming Approach Using C96 Avoid compound negative statements! Note
97
Computer Science: A Structured Programming Approach Using C97 Table 5-9Complementing Expressions
98
Computer Science: A Structured Programming Approach Using C98 Table 5-10Selection Rules
99
Computer Science: A Structured Programming Approach Using C99 FIGURE 5-27 Structure Chart Symbols for Selection
100
Computer Science: A Structured Programming Approach Using C100 FIGURE 5-28 Multiway Selection in a Structure Chart
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.