1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical...
-
Upload
darren-ray -
Category
Documents
-
view
213 -
download
0
Transcript of 1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical...
![Page 1: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/1.jpg)
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 5Chapter 5 Selection—Making DecisionsSelection—Making Decisions
![Page 2: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/2.jpg)
2
5-1 Logical Data and Operators
A piece of data is called logical if it conveys the idea of A piece of data is called logical if it conveys the idea of true or false. In real life, logical data (true or false) true or false. In real life, logical data (true or false) are created in answer to a question that needs a yes–are created in answer to a question that needs a yes–no answer. In computer science, we do not use yes or no answer. In computer science, we do not use yes or no, we use true or false.no, we use true or false.
Logical Data in CLogical OperatorsEvaluating Logical ExpressionsComparative Operators
Topics discussed in this section:Topics discussed in this section:
![Page 3: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/3.jpg)
3
FIGURE 5-1 true and false on the Arithmetic Scale
![Page 4: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/4.jpg)
4
FIGURE 5-2 Logical Operators Truth Table
![Page 5: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/5.jpg)
5
FIGURE 5-3 Short-circuit Methods for and /or
![Page 6: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/6.jpg)
6
PROGRAM 5-1 Logical Expressions
![Page 7: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/7.jpg)
7
FIGURE 5-4 Relational Operators (關係運算子 )
![Page 8: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/8.jpg)
8
FIGURE 5-5 Comparative Operator Complements (比較運算子的互補 )
![Page 9: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/9.jpg)
9
Table 5-1 Examples of Simplifying Operator Complements
![Page 10: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/10.jpg)
10
PROGRAM 5-2 Comparative Operators (比較運算子 )
![Page 11: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/11.jpg)
11
5-2 Two-Way Selection
The decision is described to the computer as a The decision is described to the computer as a conditional statement that can be answered either true conditional statement that can be answered either true or false. If the answer is true, one or more action or false. If the answer is true, one or more action statements are executed. If the answer is false, then a statements are executed. If the answer is false, then a different action or set of actions is executed. different action or set of actions is executed.
if…else and Null else StatementNested if Statements and Dangling else ProblemSimplifying if StatementsConditional Expressions
Topics discussed in this section:Topics discussed in this section:
![Page 12: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/12.jpg)
12
FIGURE 5-7 if...else Logic Flow
![Page 13: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/13.jpg)
13
Table 5-2 Syntactical Rules for if…else Statements
![Page 14: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/14.jpg)
14
FIGURE 5-8 A Simple if...else Statement
![Page 15: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/15.jpg)
15
FIGURE 5-9 Compound Statements in an if...else
![Page 16: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/16.jpg)
16
FIGURE 5-10 Complemented if...else Statements
![Page 17: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/17.jpg)
17
FIGURE 5-11 A Null else Statement
![Page 18: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/18.jpg)
18
FIGURE 5-12 A Null if Statement
![Page 19: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/19.jpg)
19
PROGRAM 5-3 Two-way Selection
![Page 20: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/20.jpg)
20
FIGURE 5-13 Nested if Statements
![Page 21: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/21.jpg)
21
PROGRAM 5-4 Nested if Statements
![Page 22: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/22.jpg)
22
FIGURE 5-14 Dangling else
else is always paired with the most recent unpaired if.
NoteNote
![Page 23: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/23.jpg)
23
FIGURE 5-15 Dangling else Solution
![Page 24: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/24.jpg)
24
Table 5-3 Simplifying the Condition
![Page 25: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/25.jpg)
25
FIGURE 5-16 Conditional Expression
![Page 26: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/26.jpg)
26
Table 5-4 Examples of Marginal Tax Rates
![Page 27: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/27.jpg)
27
FIGURE 5-17 Design for Calculate Taxes
![Page 28: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/28.jpg)
28
FIGURE 5-18 Design for Program 5-5 (Part I)
![Page 29: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/29.jpg)
29
FIGURE 5-18 Design for Program 5-5 (Part II)
![Page 30: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/30.jpg)
30
FIGURE 5-18 Design for Program 5-5 (Part III)
![Page 31: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/31.jpg)
31
PROGRAM 5-5
Calculate Taxes
![Page 32: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/32.jpg)
32
• 所得總額• 所得淨額 (可扣稅所
得 )• 預扣稅額 (已繳稅額 )• 應繳、應退稅額• 各級稅率• 基本扣除額
![Page 33: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/33.jpg)
33
![Page 34: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/34.jpg)
34
5-3 Multiway Selection
In addition to two-way selection, most programming In addition to two-way selection, most programming languages provide another selection concept known as languages provide another selection concept known as multiway selection. Multiway selection chooses among multiway selection. Multiway selection chooses among several alternatives. C has two different ways to several alternatives. C has two different ways to implement multiway selection: the implement multiway selection: the switchswitch statement statement and and else-ifelse-if construct. construct.
The switch StatementThe else-if
Topics discussed in this section:Topics discussed in this section:
![Page 35: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/35.jpg)
35
FIGURE 5-19 switch Decision Logic
![Page 36: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/36.jpg)
36
FIGURE 5-20 switch Statement Syntax
![Page 37: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/37.jpg)
37
FIGURE 5-21 switch Flow
![Page 38: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/38.jpg)
38
PROGRAM 5-6 Demonstrate the switch Statement
![Page 39: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/39.jpg)
39
FIGURE 5-22 switch Results
![Page 40: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/40.jpg)
40
FIGURE 5-23 A switch with break Statements
![Page 41: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/41.jpg)
41
PROGRAM 5-7 Multivalued case Statements
![Page 42: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/42.jpg)
42
PROGRAM 5-8 Student Grading
![Page 43: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/43.jpg)
43
FIGURE 5-24 The else-if Logic Design for Program 5-9
![Page 44: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/44.jpg)
44
The else-if is an artificial C construct that is only used when1. The selection variable is not an integral, and2. The same variable is being tested in the expressions.
NoteNote
![Page 45: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/45.jpg)
45
PROGRAM 5-9
Convert Score to Grade
2015/04/02
![Page 46: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/46.jpg)
46
5-4 More Standard Functions
One of the assets of the C language is its rich set of One of the assets of the C language is its rich set of standard functions that make programming much standard functions that make programming much easier. For example, C99 has two parallel but separate easier. For example, C99 has two parallel but separate header files for manipulating characters: ctype.h and header files for manipulating characters: ctype.h and wctype.h.wctype.h.
Standard Characters FunctionsA Classification ProgramHandling Major Errors
Topics discussed in this section:Topics discussed in this section:
![Page 47: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/47.jpg)
47
FIGURE 5-25 Classifications of the Character Type
![Page 48: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/48.jpg)
48
Table 5-6 Classifying Functions
continued
![Page 49: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/49.jpg)
49
Table 5-6 Classifying Functions (continued)
Conversion FunctionsTable 5-7
![Page 50: 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.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f1f5503460f94c37cf8/html5/thumbnails/50.jpg)
50
PROGRAM 5-10
Demonstrate Classification Functions