Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4 Selection Structures: if and switch Statements Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information.

Similar presentations


Presentation on theme: "Chapter 4 Selection Structures: if and switch Statements Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information."— Presentation transcript:

1 Chapter 4 Selection Structures: if and switch Statements Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of Technology

2 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-2 Program Style Cohesive Functions cohesive function –a function that performs a single operation Writing cohesive functions is good programming style, because cohesive functions are easier to read, write, debug, and maintain, and are more likely to be reusable.

3 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-3 Program Style Using Constant Macros to Enhance Readability and Ease Maintenance Use of constant macro names rather than actual values has two advantages. –First, the original statements are easier to understand because they use the descriptive names rather than numbers. –Second, a program written using constant macros is much easier to maintain than one written with constant values.

4 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-4 Modifying a Program with Function Subprograms Often what appears to be a new problem will turn out to be a variation of one that you have already solved. An important skill in problem solving is the ability to recognize that one problem is similar to another solved earlier. If the original program is well designed and modular, the programmer will be able to accommodate changing specifications with a minimum of effort.

5 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-5 CASE STUDY Water Bill with Conservation Requirements Use no more than 95 percent of the amount of water the customer used in the same quarter last year. Otherwise, they will be charged for all their water use at twice the rate.

6 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-6 CASE STUDY Water Bill with Conservation Requirements (Cont’) ADDITIONS TO DATA REQUIREMENTS –Problem Constants OVERUSE_CHG_RATE 2.0 /* double use charge as non-conservation penalty */ CONSERV_RATE 95 /* percent of last year's use allowed this year */ –Problem Inputs int use_last_year /* use for same quarter last year */

7 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-7 CASE STUDY Water Bill with Conservation Requirements (Cont’) ALGORITHM FOR COMP_USE_CHARGE –1. used is current - previous –2. if guidelines are met use_charge is used * PER_1000_CHARGE else notify customer of overuse use_charge is used * overuse_chg_rate * PER_1000_CHG

8 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-8 Figure 4.9 Function comp_use_charge Revised

9 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-9 Figure 4.9 Function comp_use_charge Revised (cont’d)

10 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-10 Nested if Statements and Multiple- Alternative Decisions nested if statement –an if statement with another if statement as its true task or its false task

11 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-11 Nested if Statements and Multiple- Alternative Decisions (Cont’)

12 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-12 Comparison of Nested if and Sequence of ifs if (x > 0) num_pos = num_pos + 1; if (x < 0) num_neg = num_neg + 1; if (x == 0) num_zero = num_zero + 1; Sequence of these if statements may be logically equivalent but is neither as readable nor as efficient. –The sequence does not clearly show that exactly one is executed. –It is less efficient because all three of the conditions are always tested.

13 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-13 Multiple-Alternative Decision Form of Nested if In situations in which each false task is followed by an if-then-else statement, you can code the nested if as the multiple- alternative decision.

14 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-14 Multiple-Alternative Decision Form of Nested if (Cont’)

15 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-15 Multiple-Alternative Decision Form of Nested if (Cont’)

16 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-16 Order of Conditions in a Multiple- Alternative Decision Compare the following segment of codes: /* Display perception of noise loudness */ if (noise_db <= 50) printf("%d-decibel noise is quiet.\n", noise_db); else if (noise_db <= 70) printf("%d-decibel noise is intrusive.\n", noise_db); else if (noise_db <= 90) printf("%d-decibel noise is annoying.\n", noise_db); else if (noise_db <= 110) printf("%d-decibel noise is very annoying.\n", noise_db); else printf("%d-decibel noise is uncomfortable.\n", noise_db);

17 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-17 Order of Conditions in a Multiple- Alternative Decision (Cont’) /* incorrect perception of noise loudness */ if (noise_db <= 110) printf("%d-decibel noise is very annoying.\n", noise_db); else if (noise_db <= 90) printf("%d-decibel noise is annoying.\n", noise_db); else if (noise_db <= 70) printf("%d-decibel noise is intrusive.\n", noise_db); else if (noise_db <= 50) printf("%d-decibel noise is quiet.\n", noise_db); else printf("%d-decibel noise is uncomfortable.\n",noise_db);

18 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-18 Function comp_tax

19 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-19 Function comp_tax (Cont’)

20 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-20 Function comp_tax (Cont’)

21 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-21 Nested if Statements with More Than One Variable

22 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-22 Figure 4.11 Flowchart of Road Sign Decision Process

23 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-23 Nested if Statements with More Than One Variable (Cont’)

24 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-24 Nested if Statements with More Than One Variable (Cont’) C associates an else with the most recent incomplete if.

25 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-25 Nested if Statements with More Than One Variable (Cont’)

26 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-26 Nested if Statements with More Than One Variable (Cont’) To force the else to be the false branch of the first if, we place braces around the true task of this first decision.

27 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-27 The switch Statement The switch statement is especially useful when the selection is based on the value of a single variable or of a simple expression (called the controlling expression). The value of this expression may be of type int or char, but not of type double.

28 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-28 Figure 4.12 Example of a switch Statement with Type char Case Labels

29 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-29 The switch Statement (Cont’) The value of the variable class expression is evaluated; then, the list of case labels is searched until one label that matches. Statements following the matching case label are executed until a break statement is encountered. If no case label matches the value of the switch statement’s controlling expression, the statements following the default label are executed, if there is a default label.

30 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-30 The switch Statement (Cont’) It is important to remember that type int and char values may be used as case labels, but strings and type double values cannot be used as case labels. Another common error is the omission of the break statement at the end of one alternative. In such a situation, execution “falls through” into the next alternative.

31 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-31 The switch Statement (Cont’)

32 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-32 The switch Statement (Cont’)

33 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-33 The switch Statement (Cont’)

34 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-34 Comparison of Nested if Statements and the switch Statement The switch is more readable Case labels that contain type double values or strings are not permitted. You should include a default label in switch statements wherever possible.

35 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-35 Common Programming Errors The following if statement displays “Condition is true” for all values of x. if (0 <= x <= 4) printf("Condition is true\n"); In order to check if x is in the range 0 to 4, you should use the condition (0 <= x && x <= 4) The code fragment that follows always prints “x is 10”, regardless of the value of x. if (x = 10) printf("x is 10");

36 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-36 Common Programming Errors (Cont’) In a compound statement, if the braces are missing, only the first statement will be considered part of the task. if (x > 0) sum = sum + x; printf("Greater than zero\n"); else printf("Less than or equal to zero\n"); In multiple-alternative format, the logic should be constructed so each intermediate condition falls on the false branch of the previous decision.


Download ppt "Chapter 4 Selection Structures: if and switch Statements Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information."

Similar presentations


Ads by Google