Presentation is loading. Please wait.

Presentation is loading. Please wait.

Switch statement.

Similar presentations


Presentation on theme: "Switch statement."— Presentation transcript:

1 Switch statement

2 Problem Write a program that inputs dates in the form month day year (ex ) and outputs the date in the form: 13-jun-1988. Note: Days are in the range Months are in the range

3 Problem Steps: 1. prompt for a date 2. read the date 3. echo the input
4. convert the date 5. output the converted date

4 Problem /* File: Convert.java Author: George J. Grevera, Ph.D.
Date: today Description: This program inputs dates in the form month/day/year (ex. 6/13/1988) and outputs the date in the form: 13-jun Note: Days are in the range Months are in the range */ import java.util.Scanner; public class Convert { public static void main ( String s[] ) { Scanner in = new Scanner( System.in ); //prompt for a date //read the date //echo the input //convert the date //output the converted date in.close(); }

5 public static void main ( String s[] ) {
Scanner in = new Scanner( System.in ); //prompt for a date //read the date //echo the input //convert the date //output the converted date in.close(); }

6 public static void main ( String s[] ) {
Scanner in = new Scanner( System.in ); //prompt for a date System.out.print( "Enter date --> " ); //read the date //echo the input //convert the date //output the converted date in.close(); }

7 public static void main ( String s[] ) {
Scanner in = new Scanner( System.in ); //prompt for a date System.out.print( "Enter date --> " ); //read the date //echo the input //convert the date //output the converted date in.close(); }

8 int month = in.nextInt(); int day = in.nextInt();
public static void main ( String s[] ) { Scanner in = new Scanner( System.in ); //prompt for a date System.out.print( "Enter date --> " ); //read the date int month = in.nextInt(); int day = in.nextInt(); int year = in.nextInt(); //echo the input //convert the date //output the converted date in.close(); }

9 //echo the input public static void main ( String s[] ) {
Scanner in = new Scanner( System.in ); //prompt for a date System.out.print( "Enter date --> " ); //read the date int month = in.nextInt(); int day = in.nextInt(); int year = in.nextInt(); //echo the input //convert the date //output the converted date in.close(); }

10 System.out.println( "You entered m=" + month
public static void main ( String s[] ) { Scanner in = new Scanner( System.in ); //prompt for a date System.out.print( "Enter date --> " ); //read the date int month = in.nextInt(); int day = in.nextInt(); int year = in.nextInt(); //echo the input System.out.println(); System.out.println( "You entered m=" + month + " d=" + day + " y=" + year ); //convert the date //output the converted date in.close(); }

11 Run: Enter date --> 12 1 2008 You entered m=12 d=1 y=2008
public static void main ( String s[] ) { Scanner in = new Scanner( System.in ); //prompt for a date System.out.print( "Enter date --> " ); //read the date int month = in.nextInt(); int day = in.nextInt(); int year = in.nextInt(); //echo the input System.out.println(); System.out.println( "You entered m=" + month + " d=" + day + " y=" + year ); //convert the date //output the converted date in.close(); } Run: Enter date --> You entered m=12 d=1 y=2008

12 //convert the date public static void main ( String s[] ) {
Scanner in = new Scanner( System.in ); //prompt for a date System.out.print( "Enter date --> " ); //read the date int month = in.nextInt(); int day = in.nextInt(); int year = in.nextInt(); //echo the input System.out.println( “\nYou entered m=" + month + " d=" + day + " y=" + year ); //convert the date //output the converted date in.close(); }

13 //convert the date String ms; public static void main ( String s[] ) {
Scanner in = new Scanner( System.in ); in.useDelimiter( "[/,\n, ,\t]" ); //prompt for a date System.out.print( "Enter date --> " ); //read the date int month = in.nextInt(); int day = in.nextInt(); int year = in.nextInt(); //echo the input System.out.println( “\nYou entered m=" + month + " d=" + day + " y=" + year ); //convert the date String ms; //output the converted date in.close(); }

14 //convert the date String ms; if (month==1) ms = "jan"; …
public static void main ( String s[] ) { Scanner in = new Scanner( System.in ); in.useDelimiter( "[/,\n, ,\t]" ); //prompt for a date System.out.print( "Enter date --> " ); //read the date int month = in.nextInt(); int day = in.nextInt(); int year = in.nextInt(); //echo the input System.out.println( “\nYou entered m=" + month + " d=" + day + " y=" + year ); //convert the date String ms; if (month==1) ms = "jan"; //output the converted date in.close(); }

15 //convert the date String ms; if (month==1) ms = "jan";
public static void main ( String s[] ) { Scanner in = new Scanner( System.in ); in.useDelimiter( "[/,\n, ,\t]" ); //prompt for a date System.out.print( "Enter date --> " ); //read the date int month = in.nextInt(); int day = in.nextInt(); int year = in.nextInt(); //echo the input System.out.println( “\nYou entered m=" + month + " d=" + day + " y=" + year ); //convert the date String ms; if (month==1) ms = "jan"; else if (month==2) ms = "feb"; else if (month==12) ms = "dec"; else ms = "???"; //output the converted date in.close(); }

16 public static void main ( String s[] ) {
Scanner in = new Scanner( System.in ); in.useDelimiter( "[/,\n, ,\t]" ); //prompt for a date System.out.print( "Enter date --> " ); //read the date int month = in.nextInt(); int day = in.nextInt(); int year = in.nextInt(); //echo the input System.out.println( “\nYou entered m=" + month + " d=" + day + " y=" + year ); //convert the date String ms = "???"; if (month==1) ms = "jan"; else if (month==2) ms = "feb"; else if (month==12) ms = "dec"; //output the converted date System.out.println( day + "-" + ms + "-" + year ); in.close(); } We have checked the month. Additionally, we could check for reasonable days and years.

17 Once again, recall the date format conversion problem…
Write a program that inputs dates in the form month day year (ex ) and outputs the date in the form: 13-jun-1988. Note: Days are in the range Months are in the range How did we convert the month from an integer to a string (6 to “jun”)?

18 How did we convert the month from an integer to a string (6 to “jun”)?
if (month==1) System.out.print( “jan” ); else if (month==2) System.out.print( “feb” ); else if (month==3) System.out.print( “mar” ); else if (month==12) System.out.print( “dec” ); else System.out.print( “invalid month” );

19 Introducing the switch statement
switch (controlling_expression) { case case_label_1: statement_sequence_1 break; case case_label_2: statement_sequence_2 case case_label_n: statement_sequence_n default: statement_sequence_default }

20 How can we use the switch instead?
if (month==1) System.out.print( “jan” ); else if (month==2) System.out.print( “feb” ); else if (month==12) System.out.print( “dec” ); else System.out.print( “bad month” ); switch (controlling_expression) { case case_label_1: statement_sequence_1 break; case case_label_2: statement_sequence_2 case case_label_n: statement_sequence_n default: statement_sequence_default }

21 How can we use the switch instead?
if (month==1) System.out.print( “jan” ); else if (month==2) System.out.print( “feb” ); else if (month==12) System.out.print( “dec” ); else System.out.print( “bad month” ); switch (controlling_expression) { case case_label_1: statement_sequence_1 break; case case_label_2: statement_sequence_2 case case_label_n: statement_sequence_n default: statement_sequence_default }

22 How can we use the switch instead?
if (month==1) System.out.print( “jan” ); else if (month==2) System.out.print( “feb” ); else if (month==12) System.out.print( “dec” ); else System.out.print( “bad month” ); switch (month) { case case_label_1: statement_sequence_1 break; case case_label_2: statement_sequence_2 case case_label_n: statement_sequence_n default: statement_sequence_default }

23 How can we use the switch instead?
if (month==1) System.out.print( “jan” ); else if (month==2) System.out.print( “feb” ); else if (month==12) System.out.print( “dec” ); else System.out.print( “bad month” ); switch (month) { case case_label_1: statement_sequence_1 break; case case_label_2: statement_sequence_2 case case_label_n: statement_sequence_n default: statement_sequence_default }

24 How can we use the switch instead?
if (month==1) System.out.print( “jan” ); else if (month==2) System.out.print( “feb” ); else if (month==12) System.out.print( “dec” ); else System.out.print( “bad month” ); switch (month) { case 1: statement_sequence_1 break; case case_label_2: statement_sequence_2 case case_label_n: statement_sequence_n default: statement_sequence_default }

25 How can we use the switch instead?
if (month==1) System.out.print( “jan” ); else if (month==2) System.out.print( “feb” ); else if (month==12) System.out.print( “dec” ); else System.out.print( “bad month” ); switch (month) { case 1: statement_sequence_1 break; case case_label_2: statement_sequence_2 case case_label_n: statement_sequence_n default: statement_sequence_default }

26 How can we use the switch instead?
if (month==1) System.out.print( “jan” ); else if (month==2) System.out.print( “feb” ); else if (month==12) System.out.print( “dec” ); else System.out.print( “bad month” ); switch (month) { case 1: statement_sequence_1 break; case 2: statement_sequence_2 case case_label_n: statement_sequence_n default: statement_sequence_default }

27 How can we use the switch instead?
if (month==1) System.out.print( “jan” ); else if (month==2) System.out.print( “feb” ); else if (month==12) System.out.print( “dec” ); else System.out.print( “bad month” ); switch (month) { case 1: statement_sequence_1 break; case 2: statement_sequence_2 case case_label_n: statement_sequence_n default: statement_sequence_default }

28 How can we use the switch instead?
if (month==1) System.out.print( “jan” ); else if (month==2) System.out.print( “feb” ); else if (month==12) System.out.print( “dec” ); else System.out.print( “bad month” ); switch (month) { case 1: statement_sequence_1 break; case 2: statement_sequence_2 case 12: statement_sequence_n default: statement_sequence_default }

29 How can we use the switch instead?
if (month==1) System.out.print( “jan” ); else if (month==2) System.out.print( “feb” ); else if (month==12) System.out.print( “dec” ); else System.out.print( “bad month” ); switch (month) { case 1: statement_sequence_1 break; case 2: statement_sequence_2 case 12: statement_sequence_n default: statement_sequence_default }

30 How can we use the switch instead?
if (month==1) System.out.print( “jan” ); else if (month==2) System.out.print( “feb” ); else if (month==12) System.out.print( “dec” ); else System.out.print( “bad month” ); switch (month) { case 1: System.out.print( “jan” ); break; case 2: statement_sequence_2 case 12: statement_sequence_n default: statement_sequence_default }

31 How can we use the switch instead?
if (month==1) System.out.print( “jan” ); else if (month==2) System.out.print( “feb” ); else if (month==12) System.out.print( “dec” ); else System.out.print( “bad month” ); switch (month) { case 1: System.out.print( “jan” ); break; case 2: statement_sequence_2 case 12: statement_sequence_n default: statement_sequence_default }

32 How can we use the switch instead?
if (month==1) System.out.print( “jan” ); else if (month==2) System.out.print( “feb” ); else if (month==12) System.out.print( “dec” ); else System.out.print( “bad month” ); switch (month) { case 1: System.out.print( “jan” ); break; case 2: System.out.print( “feb” ); case 12: statement_sequence_n default: statement_sequence_default }

33 How can we use the switch instead?
if (month==1) System.out.print( “jan” ); else if (month==2) System.out.print( “feb” ); else if (month==12) System.out.print( “dec” ); else System.out.print( “bad month” ); switch (month) { case 1: System.out.print( “jan” ); break; case 2: System.out.print( “feb” ); case 12: statement_sequence_n default: statement_sequence_default }

34 How can we use the switch instead?
if (month==1) System.out.print( “jan” ); else if (month==2) System.out.print( “feb” ); else if (month==12) System.out.print( “dec” ); else System.out.print( “bad month” ); switch (month) { case 1: System.out.print( “jan” ); break; case 2: System.out.print( “feb” ); case 12: System.out.print( “dec” ); default: statement_sequence_default }

35 How can we use the switch instead?
if (month==1) System.out.print( “jan” ); else if (month==2) System.out.print( “feb” ); else if (month==12) System.out.print( “dec” ); else System.out.print( “bad month” ); switch (month) { case 1: System.out.print( “jan” ); break; case 2: System.out.print( “feb” ); case 12: System.out.print( “dec” ); default: statement_sequence_default }

36 How can we use the switch instead?
if (month==1) System.out.print( “jan” ); else if (month==2) System.out.print( “feb” ); else if (month==12) System.out.print( “dec” ); else System.out.print( “bad month” ); switch (month) { case 1: System.out.print( “jan” ); break; case 2: System.out.print( “feb” ); case 12: System.out.print( “dec” ); default: System.out.print(“bad month”); }

37 switch notes Unlike if, the controlling expression must evaluate to int, char, short, byte. switch (52+7/n) { is fine if n is an int switch (s) { is not fine if s is a String switch (d) { is not fine if d is a double

38 switch notes You can’t repeat a case label. case 7 : blah; break;

39 switch notes Multiple statements may be associated with a case w/out the need for a block. case 17 : blah1; blah2; blah3; break; The default case is optional.

40 switch notes The case labels must be constants.
case “fred” : is not OK case : is not OK case d : is not OK (even if d is an int)

41 switch notes Don’t forget the break! Unexpected things may happen. The following is perfectly fine but may not be what you intended: case 992: case -1: System.out.println( “here” ); case 57: System.out.println( “there” ); System.out.println( “everywhere”); break;

42 Multiway if-else Statement
if (Boolean_Expression) Statement_1 else if (Boolean_Expression) Statement_2 else if (Boolean_Expression_n) Statement_n else Statement_For_All_Other_Possibilities . . .

43 The switch Statement The switch statement is the only other kind of Java statement that implements multiway branching When a switch statement is evaluated, one of a number of different branches is executed The choice of which branch to execute is determined by a controlling expression enclosed in parentheses after the keyword switch The controlling expression must evaluate to a char, int, short, or byte

44 The switch Statement Each branch statement in a switch statement starts with the reserved word case, followed by a constant called a case label, followed by a colon, and then a sequence of statements Each case label must be of the same type as the controlling expression Case labels need not be listed in order or span a complete interval, but each one may appear only once Each sequence of statements may be followed by a break statement ( break;)

45 The switch Statement There can also be a section labeled default:
The default section is optional, and is usually last Even if the case labels cover all possible outcomes in a given switch statement, it is still a good practice to include a default section It can be used to output an error message, for example When the controlling expression is evaluated, the code for the case label whose value matches the controlling expression is executed If no case label matches, then the only statements executed are those following the default label (if there is one)

46 The switch Statement The switch statement ends when it executes a break statement, or when the end of the switch statement is reached When the computer executes the statements after a case label, it continues until a break statement is reached If the break statement is omitted, then after executing the code for one case, the computer will go on to execute the code for the next case If the break statement is omitted inadvertently, the compiler will not issue an error message

47 The switch Statement switch (Controlling_Expression) {
case Case_Label_1: Statement_Sequence_1 break; case Case_Label_2: Statement_Sequence_2 case Case_Label_n: Statement_Sequence_n default: Default_Statement Sequence } //Show Demo program . . .


Download ppt "Switch statement."

Similar presentations


Ads by Google