The if and else keywords give you complete flexibility in writing conditional code. But you can sometimes write conditional code more efficiently using the switch statement. A switch statement examines a single value and then compares it to multiple possibilities, and you can control the flow of execution based on when the two values match.
- [Instructor] The if and else keywords give you complete flexibility in writing conditional code. But, you can sometimes write conditional code more efficiently using the switch statement. A switch statement examines a single value, and then compares it to multiple possibilities. You can then control the flow of execution based on when the values match. As with the last exercise, using the if and else statements, I'll start by declaring an integer-based variable. This time I'll call it month, and I'll set it to zero.
Next, I'll type with word switch. Then, I'll use Control + Shift + Enter, or Command + Shift + Return, to complete the statement, and between the parentheses, I'll simply enter the expression I'm evaluating. Within the switch statement, you can use one or more case statements. Each case statement evaluates a single expression. I'll use case zero followed by a colon. And, if that condition is true, then I'll output the text it's January.
Now, as I described in a previous video, I'm assuming that I'm using zero-based indexing for my months. So, January is zero, February is one, and so on. At the end of each case statement's code, add the keyword break. That means, jump to the end of the switch statement. If you don't put in the break keyword, Java will continue to execute the rest of the code in all of the other cases, regardless of whether those cases are true. Now, I'll select these three lines of code, and I'll press Control + d on Windows, or Command + d on Mac, to duplicate them.
And, I'll do that a couple of times. Notice when I duplicate these lines, that IntelliJ IDEA tells me that I have a duplicate label. In a switch statement, each value can only be evaluated once. I'll change this one to one, and this one to two. And, then I'll say it's February, and it's March. At the end of all the case statements, you can add in a default clause. And, this code will be executed if none of the previous case statements were true. And, here I'll say, it's some other month.
And, the response is, it's January. If I change this to a value that's greater than two, I'll get the response it's some other month. Now, here's what happens if you don't put in the break key words. Let's say for example, that I want to evaluate zero, one, and two, and if any of those three values are true, I want to say it's quarter one. To implement this, all I need to do, is comment out the executable code for case zero and case one. And now, any of those three values can be true, and I'll execute the code in case two.
I'll change the value here to one. That's for February. And, I'll run the code. And, there's the result. If none of those three values are true, then Ill once again jump to the default clause. In earlier versions of Java, the values you could evaluate with a switch statement were limited to primitives. Ints, longs, doubles, and so on. So, for example, you might have a string-based value, that I'll call month name, and I'll set it to January. Then, once again, I'll create a switch statement.
And, I'll evaluate month name, and in my case statement, I'll look for the value January. And, if that's true, I'll say it's the first month. I'll make sure I add my break statement in. And then I'll put in a default statement, and I'll say it isn't January. When I evaluate the string January, I see a match, and I get it's the first month. I'll change this initial value to February, and I'll run the code, and the result is, it isn't January.
Now, notice that when I made that change, IntelliJ IDEA shows a warning indicator. It highlights a bit of code, and if you move the cursor over that code, you get a message saying, that code is unreachable. And, that's true, because I'm using literal values. But, most of the time, with this kind of conditional code, you're evaluating a variable that's been set dynamically. Regardless of how you use it, evaluating numbers or strings, the switch statement lets you evaluate a single expression, and compare it to multiple values.
It can result in code that's more readable and maintainable than long if-else statements.
- Reviewing the principles and components of Java
- Choosing a development environment
- Testing simple Java code in JShell
- Creating a project in IntelliJ IDEA
- Working with primitive variables
- Working with Boolean and character values
- Working with String values
- Managing program flow
- Creating reusable code with methods