"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 |
---|---|
|
|
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) |
---|---|---|
|
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 |
---|---|
|
|
"I wouldn't say that it's more readable."
"Okay, but what about this example?"
Example with switch | Equivalent code |
---|---|
|
|
"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?"
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!"
GO TO FULL VERSION