Presentation is loading. Please wait.

Presentation is loading. Please wait.

Selection—Making Decisions

Similar presentations


Presentation on theme: "Selection—Making Decisions"— Presentation transcript:

1 Selection—Making Decisions
Chapter 4 Selection—Making Decisions 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 Computer Science: A Structured Programming Approach Using C

2 Topics discussed in this section:
4-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

3 FIGURE 4-1 true and false on the Arithmetic Scale
Computer Science: A Structured Programming Approach Using C

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

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

6 PROGRAM 4-1 Logical Expressions
Computer Science: A Structured Programming Approach Using C

7 PROGRAM 4-1 Logical Expressions
Computer Science: A Structured Programming Approach Using C

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

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

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

11 Comparative Operators
PROGRAM 4-2 Comparative Operators Computer Science: A Structured Programming Approach Using C

12 Comparative Operators
PROGRAM 4-2 Comparative Operators Computer Science: A Structured Programming Approach Using C

13 Topics discussed in this section:
4-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

14 FIGURE 4-6 Two-way Decision Logic
Computer Science: A Structured Programming Approach Using C

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

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

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

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

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

20 FIGURE 4-11 A Null else Statement
Computer Science: A Structured Programming Approach Using C

21 FIGURE 4-12 A Null if Statement
Computer Science: A Structured Programming Approach Using C

22 PROGRAM 4-3 Two-way Selection
Computer Science: A Structured Programming Approach Using C

23 PROGRAM 4-3 Two-way Selection
Computer Science: A Structured Programming Approach Using C

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

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

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

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

28 FIGURE Dangling else Computer Science: A Structured Programming Approach Using C

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

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

31 FIGURE 4-16 Conditional Expression
Computer Science: A Structured Programming Approach Using C

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

33 FIGURE 4-17 Design for Calculate Taxes
Computer Science: A Structured Programming Approach Using C

34 FIGURE 4-18 Design for Program 5-5 (Part I)
Computer Science: A Structured Programming Approach Using C

35 FIGURE 4-18 Design for Program 5-5 (Part II)
Computer Science: A Structured Programming Approach Using C

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

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

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

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

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

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

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

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

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

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

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

47 Topics discussed in this section:
4-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

48 FIGURE 4-19 switch Decision Logic
Computer Science: A Structured Programming Approach Using C

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

50 FIGURE switch Flow Computer Science: A Structured Programming Approach Using C

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

52 FIGURE 4-22 switch Results
Computer Science: A Structured Programming Approach Using C

53 FIGURE 4-23 A switch with break Statements
Computer Science: A Structured Programming Approach Using C

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

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

56 PROGRAM 4-8 Student Grading
Computer Science: A Structured Programming Approach Using C

57 PROGRAM 4-8 Student Grading
Computer Science: A Structured Programming Approach Using C

58 PROGRAM 4-8 Student Grading
Computer Science: A Structured Programming Approach Using C

59 FIGURE 4-24 The else-if Logic Design for Program 5-9
Computer Science: A Structured Programming Approach Using C

60 Note 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. Computer Science: A Structured Programming Approach Using C

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

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

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


Download ppt "Selection—Making Decisions"

Similar presentations


Ads by Google