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

9
The switch statement Week 5

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

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

The switch statement

Week 5

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

• The switch statement

Java Method Coding CONCEPTS COVERED THIS WEEK

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

Java Method CodingNESTED 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?

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

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 }  } } } }

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

Java Method CodingSWITCH

// reset the product's price based on the new discount codeswitch (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

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

Java Method CodingSWITCH

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’!

REMEMBERDon’t forget to terminate each case with a break!

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

Java Method CodingSWITCH

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!

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

Java Method CodingBENEFITS 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.

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

Further Reading

• Appendix D2.2 pages 475-477