+ All Categories
Home > Documents > The switch statement: an N-way selection statement

The switch statement: an N-way selection statement

Date post: 14-Jan-2016
Category:
Upload: farren
View: 40 times
Download: 0 times
Share this document with a friend
Description:
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... - PowerPoint PPT Presentation
28
The switch statement: an N-way selection statement
Transcript
Page 1: The switch statement: an  N-way  selection statement

The switch statement: an N-way selection statement

Page 2: 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...

.... but the if-else-statement is not intrinsically an N-way selection statement

• We will now study an intrinsically N-way selection statement: the switch statement

Page 3: The switch statement: an  N-way  selection statement

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

Page 4: The switch statement: an  N-way  selection statement

Syntax and meaning of the switch-statement

• Meaning of the switch statement:

• The INT-EXPRESSION in the switch statement is first evaluated It INT-EXPRESSION must be an integer valued expression

• The result of the INT-EXPRESSION is compared to the integer values given in the case clauses

Page 5: The switch statement: an  N-way  selection statement

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.

Page 6: The switch statement: an  N-way  selection statement

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

Page 7: The switch statement: an  N-way  selection statement

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

Page 8: The switch statement: an  N-way  selection statement

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

Page 9: The switch statement: an  N-way  selection statement

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;

Page 10: The switch statement: an  N-way  selection statement

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;

Page 11: The switch statement: an  N-way  selection statement

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;

Page 12: The switch statement: an  N-way  selection statement

The different integer typed expressions allowed in the switch statement

• Previously discussed:

switch ( INT-EXPRESSION ) <--- must be an integer valued expression { ...

}

Page 13: The switch statement: an  N-way  selection statement

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

Page 14: The switch statement: an  N-way  selection statement

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;

Page 15: The switch statement: an  N-way  selection statement

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").

Page 16: The switch statement: an  N-way  selection statement

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

Page 17: The switch statement: an  N-way  selection statement

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.

Page 18: The switch statement: an  N-way  selection statement

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;

Page 19: The switch statement: an  N-way  selection statement

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

Page 20: The switch statement: an  N-way  selection statement

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 ) ^

Page 21: The switch statement: an  N-way  selection statement

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 Java version SE 7 and later will also allow String typed expressions

(But Java SE 7 has not been released yet)

Page 22: The switch statement: an  N-way  selection statement

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

Page 23: The switch statement: an  N-way  selection statement

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

Page 24: The switch statement: an  N-way  selection statement

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

Page 25: The switch statement: an  N-way  selection statement

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.

Page 26: The switch statement: an  N-way  selection statement

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:

Page 27: The switch statement: an  N-way  selection statement

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

Page 28: The switch statement: an  N-way  selection statement

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


Recommended