Now that you have seen Switch case, let us take a look as to how to write simple Java code for Switch Case.
Download the file below to understand the following video better.
Let us now take a look at switch case in Java. So here again I have declared a string called coffee which has some value such as espresso. So you would recall that when we were discussing else if conditionals then also we wrote a similar logic wherein we gave a string called coffee and based on what the value of string coffee was something the other was getting printed on the console. So now let us check out if there is an alternate way of writing those else if conditions in the form of switch case. So basically here I have declared a string coffee to which I have provided the initial value espresso now here I begin writing my switch keyword and in curved braces in curved brackets I write the variable coffee. So basically depending on the value of the variable coffee one or the other of these cases will be executed. So for now you remember that in if else we wrote separate if else block for each and every other kind of coffee. So here I say that if the value of my coffee variable belongs to the case of cappuccino then I need to print preparing cappuccino. And then I write my break keyword similarly, I say that if my string coffee is espresso then I print preparing espresso on the screen and I write the break keyword similarly I say that when my case is of latte then I print preparing latte and I break the statement similarly for black coffee. And lastly, I'll declare my default statement. This default keyword basically denotes that if none of the above cases get fulfilled, if none of these cases are true, then by default it should do this particular thing. That is, it should print Please select a valid option. So, for example, the user provides any such coffee which does not belong to either Cappuccino case or the Espresso case or the black coffee case or this case, then it should by default print Please select a valid option and then we break from here. So let us now try to execute this file and see what result do we get. So when I run this program it prints preparing espresso here because here my string coffee was espresso. So basically, this case failed. That is, it did not match with the case cappuccino, and therefore it did not print this statement. But as soon as it went on to the next case, which was espresso, it found that this particular espresso case matches with the string coffee which I have provided. Therefore, it prints preparing espresso on the screen. And we break from here. Similarly, let us experiment by providing some invalid value. Suppose I provide t here. So basically, there is no case defined for t string here. It does not match with this case. Neither does it match with this case. And similarly, these two cases will also not be applicable for my string t. Therefore, by default, it should print this particular statement. Let us try to run this and see if we have written a correct program. So here you see that when I provide an invalid string such as T, the default case gets executed because none of the above four cases are valid in this case. Hence it prints out please select a valid option and then breaks from it. Now, an important thing to notice is that for each and every of these cases you need to explicitly specify the break keyword. Now, let us try to see what if we don't specify the break keyword. For example, I delete the break keyword from here from each and every case like this.
Now, let us say that I provide my string coffee as being the third one which is latte. So let us try to see what gets printed on the console. So when I run the program, you would see that when the program encounters string coffee is equal to Latte, it does not match with this case, so it skips this. Then it goes to this case since Espresso does not match with Latte, so it skips this also. But when it comes here, it sees that this case matches with the coffee string I have provided and therefore it prints preparing Latte. However, an important thing which is missing here is the break keyword. Therefore, this program will fall through this matched case and all the subsequent cases will also be executed since we have not asked it to break from the switch case. Therefore, once preparing latte is printed, it checks this case and irrespective of whether this case is matching with the string Latte or no, it prints this preparing black coffee which is here on the console. Similarly, since this is not followed by a break keyword, therefore it again falls through the next case which is the default case and executes it irrespective of the value of the string coffee which we have provided and therefore it prints please select a valid option. Hence, one important thing to notice is that it is very important to specify the break keyword whenever you are trying to write a switch case. Otherwise the whole purpose of switch case is defeated. Also note that here you need to suffix each and every case by a small this particular symbol so as to denote that you are ending this case and in the next line you need to specify what is going to happen in this case. Therefore, we realize that it is very important to specify the break keyword and to use the correct syntax. As we have seen earlier, when you place your cursor near any of these curly braces, it automatically gets highlighted with the matching closing braces so that you can see whether you have closed all your brackets or no. In case you forget to add a bracket here like this, it will automatically show an error. You can see a small curvy red line here denoting that this is an invalid syntax. Hence, as usual, you need to be very careful about closing all your brackets as well as adding semicolons at the end of each and every system dot out sentences.
This segment discusses the concept of switch case in Java programming language. Switch case can be used as an alternative to else if conditionals. It allows the user to select one of many blocks of code to be executed depending on the value of an expression. The switch statement is composed of cases that are executed based on the value of the expression. A break statement is necessary to exit a case and prevent the execution of other cases. If no case matches the expression, the default case is executed. Forgetting to add a break statement can result in falling through multiple cases, leading to unintended consequences. Correct syntax and the use of curly braces and brackets is essential when writing switch case statements.
Let us now take a look at another Switch case example where we try to print the season based on the month.
Please download the file below to understand the following video better.
By now, you know how to use conditional statements to execute your logic. But as you move further, you will be required to check the same logic multiple times. This may become tiresome. To avoid this, you will learn to use loops, which automate the checking of such conditions.