Presentation is loading. Please wait.

Presentation is loading. Please wait.

The switch statement Week 5. The switch statement Java Method Coding CONCEPTS COVERED THIS WEEK.

Similar presentations


Presentation on theme: "The switch statement Week 5. The switch statement Java Method Coding CONCEPTS COVERED THIS WEEK."— Presentation transcript:

1 The switch statement Week 5

2 The switch statement Java Method Coding CONCEPTS COVERED THIS WEEK

3 Java Method Coding NESTED SELECTION: if … else Note that else links with the preceding nearest if statement, from the deepest level of nesting outwards, e.g. if ( condition1 ) if ( condition2 ) statement1; else statement2; else statement3; Q1. If condition1 is true, and condition2 is false, which statement will be executed? Q2. If condition1 is false, and condition2 is true, which statement will be executed?

4 Nested if…else Statements // reset product's price based on the new discount code if (discountCode == 'A') { price = cost * 2; // 100% profit } else { if (discountCode == 'B') { price = cost * 1.8; // 80% profit } else { if (discountCode == 'C') { price = cost * 1.6; // 60% profit } else { if (discountCode == 'D') { price = cost * 1.4; // 40% profit } else { if (discountCode == 'E') { price = cost * 1.2; // 20% profit } else { price = cost; // no profit }

5 Java Method Coding SWITCH // reset the product's price based on the new discount code switch (discountCode) { case 'A' : price = cost * 2; break; // 100% profit case 'B' : price = cost * 1.8; break; // 80% profit case 'C' : price = cost * 1.6; break; // 60% profit case 'D' : price = cost * 1.4; break; // 40% profit case 'E' : price = cost * 1.2; break; // 20% profit default : price = cost; // sold at cost, no profit } default acts like a final else clause, i.e., none of the above

6 Java Method Coding SWITCH NOTE case ‘A’: price = cost * 2; case ‘B’: price = cost * 1.8; break; This will execute both case ‘arms’, and therefore mistakenly set price to 80% profit when the discount code is ‘A’ as well as when its ‘B’! REMEMBER Don’t forget to terminate each case with a break !

7 Java Method Coding SWITCH switch (studentGrade) // student degree grade between 1-16 { case 16: case 15: case 14:degreeClass = "1st";break; case 13: case 12: case 11:degreeClass = "2:1";break; case 10: case 9: case 8:degreeClass = "2:2";break; case 7: case 6: case 5:degreeClass = "3rd";break; case 4: degreeClass = "Pass"; break; case 3: case 2: case 1:degreeClass = "Fail";break; } //end switch Sometimes the result of multiple case arms can result in the same action – The above is a better way to code such situations – much easier to read/understand!

8 Java Method Coding BENEFITS OF SWITCH  CASE labels are easier to use than conditional expressions.  The alternative nature of the action is usually clearer to see.  Alternative options in cases are mutually exclusive. This means that the order of the alternatives doesn’t matter.  Nested if statements will rarely be as easy to read.  CASE statements make the programmer’s intention clear. It isn’t just that it takes less lines of code but that a long sequence of if statements could lead to an unnecessarily complicated segment of code.

9 Further Reading Appendix D2.2 pages 475-477


Download ppt "The switch statement Week 5. The switch statement Java Method Coding CONCEPTS COVERED THIS WEEK."

Similar presentations


Ads by Google