1 / 28

The switch statement: an N-way selection statement

The switch statement: an N-way selection statement. Previously discussed. The if-else-statement is a 2-way selection statement The if-else-statement can be used to write a N-way selection statement in Java...

farren
Download Presentation

The switch statement: an N-way selection statement

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The switch statement: an N-way selection statement

  2. Previously discussed • The if-else-statement is a 2-way selection statement • The if-else-statement can be used to write a N-way selection statement in Java... .... but the if-else-statement is not intrinsically an N-way selection statement • We will now study an intrinsicallyN-way selection statement: the switch statement

  3. Syntax and meaning of the switch-statement • Syntax of the switch-statement: switch ( INT-EXPRESSION ) { case INT-VALUE1: STATEMENT11; STATEMENT12; ... break; // Marks the end of case INT-VALUE1 case INT-VALUE2: STATEMENT21; STATEMENT22; ... break; // Marks the end of case INT-VALUE2 ... (more cases if desired) ... [default: STATEMENTd1; // Optional clause STATEMENTd2; ... break; ] }

  4. Syntax and meaning of the switch-statement • Meaning of the switch statement: • The INT-EXPRESSION in the switch statement is first evaluated It INT-EXPRESSIONmust be an integer valued expression • The result of the INT-EXPRESSION is compared to the integer values given in the case clauses

  5. Syntax and meaning of the switch-statement (cont.) • If the result of the INT-EXPRESSION is equal to some INT-VALUE in a case clause, the statements following the case clause upto the break statement are executed • If the result of the INT-EXPRESSION is not equal to any INT-VALUE in the case clauses, then the statements in the default case are executed if it is specified If the default case is not specified, then the switch statement will terminate without executing any statement.

  6. Syntax and meaning of the switch-statement (cont.) • Note: the execution of the switch statement is terminated by: • A break; statement,        or • When execution reaches the end of the switch statement

  7. Syntax and meaning of the switch-statement (cont.) • Programming example: translate a numeric month to a string name import java.util.Scanner; public class Switch01 { public static void main(String[] args) { int a; String name = ""; Scanner in = new Scanner(System.in); // Construct Scanner object System.out.print("Enter a numeric month (1-12): "); a = in.nextInt(); // Read in next number into a

  8. Syntax and meaning of the switch-statement (cont.) switch ( a ) { case 1: name = "January"; break; // For brevity, I put case 2: name = "February"; break; // the "break" statement case 3: name = "March"; break; // on the same line. case 4: name = "April"; break; case 5: name = "May"; break; case 6: name = "June"; break; case 7: name = "July"; break; case 8: name = "August"; break; case 9: name = "September"; break; case 10: name = "October"; break; case 11: name = "November"; break; case 12: name = "December"; break; default: name = "Invalid month"; break; // <-- This break statement // is redundant } System.out.println("Name of month = " + name); } }

  9. Syntax and meaning of the switch-statement (cont.) • Explanation: • The INT-EXPRESSION in the switch statement is a • Therefore, based on the value of a, one of the cases will be executed • When the variable a is equal to 1, the statements: • are executed. name = "January"; break;

  10. Syntax and meaning of the switch-statement (cont.) • When the variable a is equal to 2, the statements: are executed. And so on. name = “February"; break;

  11. Syntax and meaning of the switch-statement (cont.) • If the value of the variable a is not one of the 12 values (1, 2, ..., 12) listed in the cases, then the statements of the default case: are executed. name = “Invalid month "; break;

  12. The different integer typed expressions allowed in the switch statement • Previously discussed: switch ( INT-EXPRESSION ) <--- must be an integer valued expression { ... }

  13. The different integer typed expressions allowed in the switch statement (cont.) • The following types are automatically converted into an integer typed value in expressions: Therefore, we can also use them in the switch statement !! • byte • short • char

  14. The different integer typed expressions allowed in the switch statement (cont.) • Example: public class Switch02 { public static void main(String[] args) { char x; String name = ""; x = 'C'; switch ( x ) // char is converted to int automatically ! { case 'A': name = "January"; break; case 'B': name = "February"; break; case 'C': name = "March"; break; case 'D': name = "April"; break;

  15. The different integer typed expressions allowed in the switch statement (cont.) case 'E': name = "May"; break; case 'F': name = "June"; break; case 'G': name = "July"; break; case 'H': name = "August"; break; case 'I': name = "September"; break; case 'J': name = "October"; break; case 'K': name = "November"; break; case 'L': name = "December"; break; default: name = "Invalid month"; break; } System.out.println("Name of month = " + name); } } This program will compile correctly and run (prints "March").

  16. The different integer typed expressions allowed in the switch statement (cont.) • Example Program: (Demo above code)   • Prog file: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/06/Progs/Switch02.java    • How to run the program: • Right click on link and save in a scratch directory • To compile:   javac Switch02.java • To run:          java Switch02

  17. The long typed integer expression is not allowed in the switch statement • A long typed integer value is not automatically converted to int • Therefore, you cannot use a long typed integer value in a switch statement without a casting operation.

  18. The long typed integer expression is not allowed in the switch statement • Example: public class Switch03 { public static void main(String[] args) { long a; String name = ""; a = 10; switch ( a ) // Illegal ! { case 1: name = "January"; break; case 2: name = "February"; break; case 3: name = "March"; break; case 4: name = "April"; break; case 5: name = "May"; break;

  19. The long typed integer expression is not allowed in the switch statement (cont.) case 6: name = "June"; break; case 7: name = "July"; break; case 8: name = "August"; break; case 9: name = "September"; break; case 10: name = "October"; break; case 11: name = "November"; break; case 12: name = "December"; break; default: name = "Invalid month"; break; } System.out.println("Name of month = " + name); } }

  20. The long typed integer expression is not allowed in the switch statement (cont.) • You will get the following compile error: Switch03.java:11: possible loss of precision found : long required: int switch ( a ) ^

  21. String typed values are allowed in the switch statement in Java SE 7 and later... • According the the Java development website: http://download.oracle.com/javase/tutorial/java/nutsandbolts/switch.html (This is not the case in Java 6 and other languages such as C/C++) • The switch statement in Javaversion SE 7 and later will also allowString typed expressions • (But Java SE 7 has not been released yet)

  22. String typed values are allowed in the switch statement in Java SE 7 and later... (cont.) • Example: the following program converts a month name (string) into an integer using a switch statement import java.util.Scanner; public class Switch04 { public static void main(String[] args) { String name = ""; int a; Scanner in = new Scanner(System.in); // Construct Scanner object System.out.print("Enter the name of a month (lower case): "); name = in.next(); // Read in name

  23. String typed values are allowed in the switch statement in Java SE 7 and later... (cont.) switch ( name ) { case "january": a = 1; break; case "february": a = 2; break; case "march": a = 3; break; case "april": a = 4; break; case "may": a = 5; break; case "june": a = 6; break; case "july": a = 7; break; case "august": a = 8; break; case "september": a = 9; break; case "october": a = 10; break; case "november": a = 11; break; case "december": a = 12; break; default: a = 0; break; } System.out.println("Index of month = " + a); } }

  24. String typed values are allowed in the switch statement in Java SE 7 and later... (cont.) • We cannot compile and run this program because Java SE7 is not yet available !!!

  25. Programming example: convert number grade to letter grade • We can use a switch statement to assign a letter grade to a number grade (read from input) • Algorithm: • We must use integer numeric grade (floating point numbers are not allowed in a switch statement !) • Assign letter grade A for numeric grades 90, 91, ..., 100 • Assign letter grade B for numeric grades 80, 81, ..., 89 • Assign letter grade C for numeric grades 70, 71, ..., 79 • Assign letter grade D for numeric grades 60, 61, ..., 69 • Default: assign letter grade F otherwise.

  26. Programming example: convert number grade to letter grade (cont.) • Java program: public class Switch05 { public static void main(String[] args) { int ng; String lg = ""; Scanner in = new Scanner(System.in); // Construct Scanner object ng = in.nextInt(); // Read in next number switch ( ng ) { case 100: .... // I left out a lot of "cases" to save space ! case 90: lg = "A"; break; case 89:

  27. Programming example: convert number grade to letter grade (cont.) ... case 80: lg = "B"; break; case 79: ... case 70: lg = "C"; break; case 69: ... case 60: lg = "D"; break; default: lg = "F"; break; } System.out.println("Letter grade = " + lg); } }

  28. Programming example: convert number grade to letter grade (cont.) • Example Program: (Demo above code)   • Prog file: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/06/Progs/Switch05.java    • How to run the program: • Right click on link and save in a scratch directory • To compile:   javac Switch05.java • To run:          java Switch05

More Related