5 1 Logical Data and Operators A piece

  • Slides: 66
Download presentation
5 -1 Logical Data and Operators A piece of data is called logical if

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. Topics discussed in this section: Logical Data in C Logical Operators Evaluating Logical Expressions Comparative Operators Computer Science: A Structured Programming Approach Using C 1

FIGURE 5 -2 Logical Operators Truth Table Computer Science: A Structured Programming Approach Using

FIGURE 5 -2 Logical Operators Truth Table Computer Science: A Structured Programming Approach Using C 2

FIGURE 5 -3 Short-circuit Methods for and /or Computer Science: A Structured Programming Approach

FIGURE 5 -3 Short-circuit Methods for and /or Computer Science: A Structured Programming Approach Using C 3

FIGURE 5 -4 Relational Operators Computer Science: A Structured Programming Approach Using C 4

FIGURE 5 -4 Relational Operators Computer Science: A Structured Programming Approach Using C 4

FIGURE 5 -5 Comparative Operator Complements Computer Science: A Structured Programming Approach Using C

FIGURE 5 -5 Comparative Operator Complements Computer Science: A Structured Programming Approach Using C 5

Table 5 -1 Examples of Simplifying Operator Complements Computer Science: A Structured Programming Approach

Table 5 -1 Examples of Simplifying Operator Complements Computer Science: A Structured Programming Approach Using C 6

PROGRAM 5 -2 Comparative Operators Computer Science: A Structured Programming Approach Using C 7

PROGRAM 5 -2 Comparative Operators Computer Science: A Structured Programming Approach Using C 7

PROGRAM 5 -2 Comparative Operators Computer Science: A Structured Programming Approach Using C 8

PROGRAM 5 -2 Comparative Operators Computer Science: A Structured Programming Approach Using C 8

5 -2 Two-Way Selection The decision is described to the computer as a conditional

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. Topics discussed in this section: if…else and Null else Statement Nested if Statements and Dangling else Problem Simplifying if Statements Conditional Expressions Computer Science: A Structured Programming Approach Using C 9

FIGURE 5 -7 if. . . else Logic Flow Computer Science: A Structured Programming

FIGURE 5 -7 if. . . else Logic Flow Computer Science: A Structured Programming Approach Using C 10

Table 5 -2 Syntactical Rules for if…else Statements Computer Science: A Structured Programming Approach

Table 5 -2 Syntactical Rules for if…else Statements Computer Science: A Structured Programming Approach Using C 11

FIGURE 5 -8 A Simple if. . . else Statement Computer Science: A Structured

FIGURE 5 -8 A Simple if. . . else Statement Computer Science: A Structured Programming Approach Using C 12

FIGURE 5 -9 Compound Statements in an if. . . else Computer Science: A

FIGURE 5 -9 Compound Statements in an if. . . else Computer Science: A Structured Programming Approach Using C 13

FIGURE 5 -10 Complemented if. . . else Statements Computer Science: A Structured Programming

FIGURE 5 -10 Complemented if. . . else Statements Computer Science: A Structured Programming Approach Using C 14

FIGURE 5 -11 A Null else Statement Computer Science: A Structured Programming Approach Using

FIGURE 5 -11 A Null else Statement Computer Science: A Structured Programming Approach Using C 15

FIGURE 5 -12 A Null if Statement Computer Science: A Structured Programming Approach Using

FIGURE 5 -12 A Null if Statement Computer Science: A Structured Programming Approach Using C 16

PROGRAM 5 -3 Two-way Selection Computer Science: A Structured Programming Approach Using C 17

PROGRAM 5 -3 Two-way Selection Computer Science: A Structured Programming Approach Using C 17

PROGRAM 5 -3 Two-way Selection Computer Science: A Structured Programming Approach Using C 18

PROGRAM 5 -3 Two-way Selection Computer Science: A Structured Programming Approach Using C 18

FIGURE 5 -13 Nested if Statements Computer Science: A Structured Programming Approach Using C

FIGURE 5 -13 Nested if Statements Computer Science: A Structured Programming Approach Using C 19

PROGRAM 5 -4 Nested if Statements Computer Science: A Structured Programming Approach Using C

PROGRAM 5 -4 Nested if Statements Computer Science: A Structured Programming Approach Using C 20

PROGRAM 5 -4 Nested if Statements Computer Science: A Structured Programming Approach Using C

PROGRAM 5 -4 Nested if Statements Computer Science: A Structured Programming Approach Using C 21

Note else is always paired with the most recent unpaired if. Computer Science: A

Note else is always paired with the most recent unpaired if. Computer Science: A Structured Programming Approach Using C 22

FIGURE 5 -14 Dangling else Computer Science: A Structured Programming Approach Using C 23

FIGURE 5 -14 Dangling else Computer Science: A Structured Programming Approach Using C 23

FIGURE 5 -15 Dangling else Solution Computer Science: A Structured Programming Approach Using C

FIGURE 5 -15 Dangling else Solution Computer Science: A Structured Programming Approach Using C 24

Table 5 -3 Simplifying the Condition Computer Science: A Structured Programming Approach Using C

Table 5 -3 Simplifying the Condition Computer Science: A Structured Programming Approach Using C 25

FIGURE 5 -16 Conditional Expression Computer Science: A Structured Programming Approach Using C 26

FIGURE 5 -16 Conditional Expression Computer Science: A Structured Programming Approach Using C 26

Table 5 -4 Examples of Marginal Tax Rates Computer Science: A Structured Programming Approach

Table 5 -4 Examples of Marginal Tax Rates Computer Science: A Structured Programming Approach Using C 27

FIGURE 5 -18 Design for Program 5 -5 (Part III) Computer Science: A Structured

FIGURE 5 -18 Design for Program 5 -5 (Part III) Computer Science: A Structured Programming Approach Using C 28

PROGRAM 5 -5 Calculate Taxes Computer Science: A Structured Programming Approach Using C 29

PROGRAM 5 -5 Calculate Taxes Computer Science: A Structured Programming Approach Using C 29

PROGRAM 5 -5 Calculate Taxes Computer Science: A Structured Programming Approach Using C 30

PROGRAM 5 -5 Calculate Taxes Computer Science: A Structured Programming Approach Using C 30

PROGRAM 5 -5 Calculate Taxes Computer Science: A Structured Programming Approach Using C 31

PROGRAM 5 -5 Calculate Taxes Computer Science: A Structured Programming Approach Using C 31

PROGRAM 5 -5 Calculate Taxes Computer Science: A Structured Programming Approach Using C 32

PROGRAM 5 -5 Calculate Taxes Computer Science: A Structured Programming Approach Using C 32

PROGRAM 5 -5 Calculate Taxes Computer Science: A Structured Programming Approach Using C 33

PROGRAM 5 -5 Calculate Taxes Computer Science: A Structured Programming Approach Using C 33

PROGRAM 5 -5 Calculate Taxes Computer Science: A Structured Programming Approach Using C 34

PROGRAM 5 -5 Calculate Taxes Computer Science: A Structured Programming Approach Using C 34

PROGRAM 5 -5 Calculate Taxes Computer Science: A Structured Programming Approach Using C 35

PROGRAM 5 -5 Calculate Taxes Computer Science: A Structured Programming Approach Using C 35

PROGRAM 5 -5 Calculate Taxes Computer Science: A Structured Programming Approach Using C 36

PROGRAM 5 -5 Calculate Taxes Computer Science: A Structured Programming Approach Using C 36

PROGRAM 5 -5 Calculate Taxes Computer Science: A Structured Programming Approach Using C 37

PROGRAM 5 -5 Calculate Taxes Computer Science: A Structured Programming Approach Using C 37

PROGRAM 5 -5 Calculate Taxes Computer Science: A Structured Programming Approach Using C 38

PROGRAM 5 -5 Calculate Taxes Computer Science: A Structured Programming Approach Using C 38

5 -3 Multiway Selection In addition to two-way selection, most programming languages provide another

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. Topics discussed in this section: The switch Statement The else-if Computer Science: A Structured Programming Approach Using C 39

FIGURE 5 -20 switch Statement Syntax Computer Science: A Structured Programming Approach Using C

FIGURE 5 -20 switch Statement Syntax Computer Science: A Structured Programming Approach Using C 40

PROGRAM 5 -6 Demonstrate the switch Statement Computer Science: A Structured Programming Approach Using

PROGRAM 5 -6 Demonstrate the switch Statement Computer Science: A Structured Programming Approach Using C 41

PROGRAM 5 -7 Multivalued case Statements Computer Science: A Structured Programming Approach Using C

PROGRAM 5 -7 Multivalued case Statements Computer Science: A Structured Programming Approach Using C 42

Table 5 -5 Summary of switch Statement Rules Computer Science: A Structured Programming Approach

Table 5 -5 Summary of switch Statement Rules Computer Science: A Structured Programming Approach Using C 43

PROGRAM 5 -8 Student Grading Computer Science: A Structured Programming Approach Using C 44

PROGRAM 5 -8 Student Grading Computer Science: A Structured Programming Approach Using C 44

PROGRAM 5 -8 Student Grading Computer Science: A Structured Programming Approach Using C 45

PROGRAM 5 -8 Student Grading Computer Science: A Structured Programming Approach Using C 45

PROGRAM 5 -8 Student Grading Computer Science: A Structured Programming Approach Using C 46

PROGRAM 5 -8 Student Grading Computer Science: A Structured Programming Approach Using C 46

PROGRAM 5 -9 Convert Score to Grade Computer Science: A Structured Programming Approach Using

PROGRAM 5 -9 Convert Score to Grade Computer Science: A Structured Programming Approach Using C 47

PROGRAM 5 -9 Convert Score to Grade Computer Science: A Structured Programming Approach Using

PROGRAM 5 -9 Convert Score to Grade Computer Science: A Structured Programming Approach Using C 48

PROGRAM 5 -9 Convert Score to Grade Computer Science: A Structured Programming Approach Using

PROGRAM 5 -9 Convert Score to Grade Computer Science: A Structured Programming Approach Using C 49

5 -4 More Standard Functions One of the assets of the C language is

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, C 99 has two parallel but separate header files for manipulating characters: ctype. h and wctype. h. Topics discussed in this section: Standard Characters Functions A Classification Program Handling Major Errors Computer Science: A Structured Programming Approach Using C 50

FIGURE 5 -25 Classifications of the Character Type Computer Science: A Structured Programming Approach

FIGURE 5 -25 Classifications of the Character Type Computer Science: A Structured Programming Approach Using C 51

continued Table 5 -6 Classifying Functions Computer Science: A Structured Programming Approach Using C

continued Table 5 -6 Classifying Functions Computer Science: A Structured Programming Approach Using C 52

Table 5 -6 Classifying Functions (continued) Computer Science: A Structured Programming Approach Using C

Table 5 -6 Classifying Functions (continued) Computer Science: A Structured Programming Approach Using C 53

Table 5 -7 Conversion Functions Computer Science: A Structured Programming Approach Using C 54

Table 5 -7 Conversion Functions Computer Science: A Structured Programming Approach Using C 54

PROGRAM 5 -10 Demonstrate Classification Functions Computer Science: A Structured Programming Approach Using C

PROGRAM 5 -10 Demonstrate Classification Functions Computer Science: A Structured Programming Approach Using C 55

PROGRAM 5 -10 Demonstrate Classification Functions Computer Science: A Structured Programming Approach Using C

PROGRAM 5 -10 Demonstrate Classification Functions Computer Science: A Structured Programming Approach Using C 56

5 -5 Incremental Development Part II In Chapter 4, we introduced the concept of

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. Topics discussed in this section: Calculator Design Calculator Incremental Design Computer Science: A Structured Programming Approach Using C 57

PROGRAM 5 -13 Menu-driven Calculator—Fifth Increment Computer Science: A Structured Programming Approach Using C

PROGRAM 5 -13 Menu-driven Calculator—Fifth Increment Computer Science: A Structured Programming Approach Using C 58

PROGRAM 5 -13 Menu-driven Calculator—Fifth Increment Computer Science: A Structured Programming Approach Using C

PROGRAM 5 -13 Menu-driven Calculator—Fifth Increment Computer Science: A Structured Programming Approach Using C 59

PROGRAM 5 -13 Menu-driven Calculator—Fifth Increment Computer Science: A Structured Programming Approach Using C

PROGRAM 5 -13 Menu-driven Calculator—Fifth Increment Computer Science: A Structured Programming Approach Using C 60

PROGRAM 5 -13 Menu-driven Calculator—Fifth Increment Computer Science: A Structured Programming Approach Using C

PROGRAM 5 -13 Menu-driven Calculator—Fifth Increment Computer Science: A Structured Programming Approach Using C 61

PROGRAM 5 -13 Menu-driven Calculator—Fifth Increment Computer Science: A Structured Programming Approach Using C

PROGRAM 5 -13 Menu-driven Calculator—Fifth Increment Computer Science: A Structured Programming Approach Using C 62

PROGRAM 5 -13 Menu-driven Calculator—Fifth Increment Computer Science: A Structured Programming Approach Using C

PROGRAM 5 -13 Menu-driven Calculator—Fifth Increment Computer Science: A Structured Programming Approach Using C 63

PROGRAM 5 -13 Menu-driven Calculator—Fifth Increment Computer Science: A Structured Programming Approach Using C

PROGRAM 5 -13 Menu-driven Calculator—Fifth Increment Computer Science: A Structured Programming Approach Using C 64

PROGRAM 5 -13 Menu-driven Calculator—Fifth Increment Computer Science: A Structured Programming Approach Using C

PROGRAM 5 -13 Menu-driven Calculator—Fifth Increment Computer Science: A Structured Programming Approach Using C 65

PROGRAM 5 -13 Menu-driven Calculator—Fifth Increment Computer Science: A Structured Programming Approach Using C

PROGRAM 5 -13 Menu-driven Calculator—Fifth Increment Computer Science: A Structured Programming Approach Using C 66