In the previous session, you learnt how to use If Else and Else If statements. You also learnt about nested If Else statements. In this session, you will learn about another commonly used conditional operator called ‘Switch’.
Aishwarya will now tell us how Switch case works in Java.
Till now you have seen flsf statements as well as seen how you can utilize nested FLS to check multiple conditions nested inside one another. Now there is another interesting way, an interesting alternative to these IFL statements which we call as the switch case statements. So the switch case statements are also a kind of conditional statements in Java which help you to tackle situations wherein a single variable or a single and a single object might assume different kinds of values. Like you see in the coffee machine example, the variable coffee could have multiple values such as that of black coffee or espresso or milk coffee and so on. So an alternative way of writing these kinds of IFL statements would be to use switch case statements. Suppose that you wish to print that based on which month of the year, what season will that month have, then you can use switch case statements for that. For example, you might say that you have an entity called month and based on what is the value of the month, you need to print what will be the season during that month. So the first case would be say, January. So you say that in January the season will be winter. Similarly you would say that if the month is February, the season will again be winter and so on. You can define for all the months. Say you'll say that July will have the monsoon season or you can say like November is going to be autumn.
So one way to write these kinds of conditions would be to write if statements. But Java also provides you an alternative way to write and print this information. Based on that what month you are in, you print the season of that month. So in order to code this into a Java code, you can write simple switch case statements of which I'm going to show the syntax now. So you can say that suppose I'm storing my value of the month inside a variable called month. Then I'll say I'll first define that variable. I'll say string month like this. Then I begin my switch a set of switch case statements. For beginning any switch case statements I'll first use the keyword switch and then in the bracket I'll provide the value of the variable which I wish to compare. So in this case I'm going to compare the month value, the month value based on whether the month is January or is February or July, November and so on. So inside the switch bracket I'll say switch of month and I open a curly bracket. Now basically this switch month statement indicates that for all the coming case statements it is going to check what is the value of month. So I say that if it is case, if it is the first case that my month is say January, then what should happen? Then it should print winter. Once I'm done writing this case statement, I also need to write a break statement. This break keyword basically ensures that as soon as this condition is met you don't need to execute any of the subsequent case statements and the program can simply break at this point and terminate. Similarly this was case one when the case when my month is January. Now in second case similarly I can say that case February and then I say print Winter and again I write the break statement. Similarly I can write for all of these subsequent months. So in my last case I'll say that case December, again print Winter and Break. So basically the switch statements comes outside and inside you write all of the different cases which this switch variable, which is month, what values it can take and based on the values such as January, February, July or December, you do some actions such as print the season of that particular month and after each one of them you write this break keyword. So this is how a simple switch case works in Java. Now here an interesting thing is that you definitely need to use the break keyword here. Using the break keyword is not all the time necessary. However, it is highly recommended that you do that because when you write the break statement, it automatically tells the program that as soon as you encounter the situation where this case matches with the month which you are providing, then do this action that is, print Winter and simply break. If you wouldn't have written the break keyword here then what would have happened? Well, the program would have fallen hrough even if suppose I did not write the break statement here. Now, if I say that my month is January, then what would have happened is that it would have gone into this case, case January, it would have printed Winter. Now, since it does not see the break keyword, it goes into all of these subsequent case conditions and starts printing Winter and for all the subsequent months it keeps on doing whatever action is enclosed within that month. So we are going to see what happens when we don't write the break statement, when we write a code and execute it in our IDE. But for now, remember that it is always recommended to write the break keyword here. Another important thing is that that this month variable can take twelve values here. So what will happen if I provide an absurd value for the month here? Suppose I say month is equal to ABC and none of these cases match. Now in that case you can write an additional default statement here. That is I'll remove this curly braces here and I will say that if none of these cases match then by default what you print, you print give a valid value.
So basically now our set of switch case statements are complete. You write the switch statement outside, then you write the twelve cases for your months and lastly you say that in case any of these cases are not fulfilled, then you use the keyword default for this. And you say that in the default case what happens is that you print give a valid value.
So you just now saw how Switch case statements help us to solve everyday life scenarios which require us to choose between different alternatives. They are very similar to multiple If Else statements, but the only difference being that Switch case statements are strictly used only when we have to only check the value of a variable and only then take an action. It is recommended to use If Else statements in those situations where you might have to check some additional conditions besides the value of a single variable (which is the case in Switch case statements). Let us now discuss the similarities between If Else and Switch and understand when each one is to be used.
This segment is about the switch case statements in Java, which are used to handle situations where a single variable or object might assume different values. This alternative to if statements helps to print information based on the month of the year, indicating what season it is during that month. The speaker demonstrates the syntax of switch case statements and explains the importance of using the break keyword. They also discuss using a default statement in case none of the cases match. Using this method, one can write all the cases for the twelve months to perform actions such as printing the season for that month.
You now know that Switch has a default case, which is equivalent to the Else condition in the case of If Else statements. The default case is executed if no other case is met. The break statement tells the Switch operator when to stop.
Switch is similar in functionality to Else If as it also checks multiple conditions and then gives the corresponding output. Although it has more readability, it has limited usage because it cannot evaluate the conditions within each case. Switch compares the input against predefined cases.
In the next segment, you will learn how you can write Java code for Switch statements.