Switch statements

Intermediate Java

Jim White

Java Developer

What is a switch?

  • Switch serves a purpose similar to if-else statements
    • Does some actions based on some condition
    • Used in place of multiple if-then-else conditionals
  • Considered more concise than multiple if-then-else
if (bonus == 10000) {
    System.out.println("Let's buy a car!");
} else if (bonus == 5000) {
    System.out.println("Let's take a trip!");
} else if (bonus == 1000) {
    System.out.println("Let's save!");
}
Intermediate Java

Switch syntax

switch (expression) {
    case value1:  // if (expression == value1)
        // statements
    case value2:  // if (expression == value2)
        // statements
    // use as many case statements as needed
}
Intermediate Java

Switch example

char direction = 'N';

switch (direction) {
    case 'E':  
        System.out.println("We are headed east.");
    case 'S':  
        System.out.println("We are headed south.");
    case 'W':  
        System.out.println("We are headed west.");
    case 'N':   
        System.out.println("We are headed north."); // This would be printed
}
Intermediate Java

Break

  • Stops Java continuing through the rest of cases
    • Without break, Java execute statements in the next case until it hits a break
char direction = 'W';

switch (direction) {
    case 'E':
        System.out.println("We are headed east.");
    case 'S':
        System.out.println("We are headed south.");
    case 'W':
        System.out.println("We are headed west."); // This would be printed
    case 'N':
        System.out.println("We are headed north."); // This would also be printed
}
Intermediate Java

break allows combining multiple cases together:

char direction = 'N';

switch (direction) {
    case 'E':     
    case 'W':  
        System.out.println("We are headed east or west.");
        break;
    case 'S':  
    case 'N':
        System.out.println("We are headed south or north.");
        break;       
}
We are headed east or west.
Intermediate Java

Default

  • An optional default case can be added to the end of a switch
    • The default case has no value to compare to the expression
    • If no case value matches the expression, the default statements get executed
    • default must be the last case in a switch
  • default is a way to have an "all other values" case
char direction = 'z';

switch (direction) {
  case 'E':     
  case 'W':  
    System.out.println("To east or west.");
    break;
  case 'S':  
  case 'N':
    System.out.println("To south or north.");
    break; 
  default:
    System.out.println("We are lost.");
}
We are lost.
Intermediate Java

Conditionals versus switch

  • Conditionals could be used to complete the same task
  • Work with every data type
char character = 'A';
if (character == 'A') {
    System.out.println("It's A");
} else if (direction =='B') {
    System.out.println("It's B");
} else {
    System.out.println("Unknown");
}
  • Switch can be easier to read
  • Works only with some data types like int, byte, short, char, or String
char character = 'A';
switch (character) {
  case 'A':
    System.out.println("It's A");
    break;
  case 'B':  
    System.out.println("It's B");
    break;
  default:
    System.out.println("Unknown");
}
Intermediate Java

Let's practice!

Intermediate Java

Preparing Video For Download...