switch, case, default - 1

"Hi, Amigo!"

"Hi, Bilaabo! So good to see you again. Only your lectures are so good and understandable. Not like this Java Memory Model."

"Yes, Bilaabo knows how to choose lessons. Today I'm going to tell you about the switch statement."

"I think someone already told me about it."

"Ellie did. So, Amigo doesn't want to hear a lesson about the switch statement? Maybe you'll start teaching your own?"

"No, I want to, I want to. Let's hear about the switch statement."

"OK. In Java, there's something called a switch statement. It's convenient when you need to perform certain actions depending on various values ​​of some variable."

Example with switch Equivalent code
int i = 5;
switch(i)
{
 case 1:
  System.out.println("one");
  break;
 case 2:
  System.out.println("two");
  break;
 case 3:
  System.out.println("three");
  break;
 default:
  System.out.println("many");
}
int i = 5;
if (i == 1)
{
 System.out.println("one");
}
else if (i == 2)
{
 System.out.println("two");
}
else if (i == 3)
{
 System.out.println("three");
}
else
{
 System.out.println("many");
}

The switch statement lets you jump to the desired piece of code if the variable passed to it matches the value that follows the keyword case.

If i is 1, then execution will jump to the line marked «case 1».

If i is 2, then execution will jump to the line marked «case 2».

If i is 3, then execution will jump to the line marked «case 3».

"If there is no jump to any of the cases, then the «default» block is executed."

"I see. And on the right is the same logic, but implemented using if statements?"

"Yep."

"And what's with the word 'break'? You said it can only be used in loops?"

"Yes, and here. When the break statement is executed, we immediately exit the switch."

"But if the break statement is removed, then all the lines inside the switch will be executed until the end."

Example Output (for i = 1) Output (for i = 2)
switch(i)
{
 case 1:
  System.out.println("one");
 case 2:
  System.out.println("two");
 case 3:
  System.out.println("three");
 default:
  System.out.println("many"); }
one
two
three
many
two
three
many

"Actually, case is a label in the code. In the switch statement, we jump to the next label and start executing all the code until the end of the switch, or until we meet a break statement."

"So, if we don't write break, then the line we jump to will be executed, followed by all the other lines until the closing brace. Is that right?"

"Yes."

"Piece of cake. But I like using if statements better. They don't have these pointless break statements."

"It's true that if statements are very often more compact. But a switch statement is sometimes more readable."

"Compare:"

Example with switch Equivalent code
public String getName(int i)
{
 switch(i)
 {
  case 1:
   return "one";
  case 2:
   return "two";
  case 3:
   return "three";
  default:
   return "many";
}
public String getName(int i)
{
 if (i == 1)
  return "one";

 if (i == 2)
  return "two";

 if (i == 3)
  return "three";

return "many"
}

"I wouldn't say that it's more readable."

"Okay, but what about this example?"

Example with switch Equivalent code
public String getName(int i)
{
 switch(i)
 {
  case 1:
  case 2:
   return "one or two";
  case 3:
  case 4:
  case 5:
   return "three to five";
  default:
   return "many";
}
public String getName(int i)
{
 if (i == 1 || i == 2)
  return "one or two";

 if (i == 3 || i == 4 || i == 5)
  return "three to five";

return "many"
}

"Bilaabo, your example doesn't look quite right. So, I can omit the break statement if I use return?"

"That's right. A return statement will immediately exit the method."

"It seems that if statements are always more compact. But the switch statement turned out to be more readable this time."

"Phew, at last."

"One more thing. You don't have to write default at the end. If you don't, then simply nothing will happen if none of the labels match."

"Uh, exactly. Like if-else, but readable—much more readable!"

"Great. I'm glad you liked my lesson."

"Oh, I almost forgot. Initially, you could only use primitive types and enums in switch statements. But now they've added support for Strings."

"You mean that I write this?"

Example
public int getNumber(String number)
{
 switch(number)
 {
  case "one":
   return 1;
  case "two":
   return 2;
  case "three":
   return 3;
  default:
   return -1;
 }
}

"Yep. Convenient, right?"

"Yes. Switch statements are great!"