Comparing code, like the examples listed below, can help:
students understand switch statements by mapping the code with them back to code without them.
motivate switch statements by helping students see how they make code easier to understand.
Code Comparison Examples:
Example code with switch statements:
// EXAMPLE of SWITCH FROM: // http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html public class SwitchDemo { public static void main(String[] args) { int month = 8; String monthString; switch (month) { case 1: monthString = "January"; break; case 2: monthString = "February"; break; case 3: monthString = "March"; break; case 4: monthString = "April"; break; case 5: monthString = "May"; break; case 6: monthString = "June"; break; case 7: monthString = "July"; break; case 8: monthString = "August"; break; case 9: monthString = "September"; break; case 10: monthString = "October"; break; case 11: monthString = "November"; break; case 12: monthString = "December"; break; default: monthString = "Invalid month"; break; } System.out.println(monthString); } }
Example code without switch statements:
// Example of IF: public class IfDemo { public static void main(String[] args) { int month = 8; String monthString; if (month == 1){ monthString = "January"; } else if (month == 2){ monthString = "February"; } else if (month == 3){ monthString = "March"; } else if (month == 4){ monthString = "April"; } else if (month == 5){ monthString = "May"; } else if (month == 6){ monthString = "June"; } else if (month == 7){ monthString = "July"; } else if (month == 8){ monthString = "August"; } else if (month == 9){ monthString = "September"; } else if (month == 10){ monthString = "October"; } else if (month == 11){ monthString = "November"; } else { //NOTE: if (month == 12){ monthString = "December"; } System.out.println(monthString); } }