MBAData Science & AnalyticsDoctorate Software & Tech AI | ML MarketingManagement
Professional Certificate Programme in HR Management and AnalyticsPost Graduate Certificate in Product ManagementExecutive Post Graduate Program in Healthcare ManagementExecutive PG Programme in Human Resource ManagementMBA in International Finance (integrated with ACCA, UK)Global Master Certificate in Integrated Supply Chain ManagementAdvanced General Management ProgramManagement EssentialsLeadership and Management in New Age BusinessProduct Management Online Certificate ProgramStrategic Human Resources Leadership Cornell Certificate ProgramHuman Resources Management Certificate Program for Indian ExecutivesGlobal Professional Certificate in Effective Leadership and ManagementCSM® Certification TrainingCSPO® Certification TrainingLeading SAFe® 5.1 Training (SAFe® Agilist Certification)SAFe® 5.1 POPM CertificationSAFe® 5.1 Scrum Master Certification (SSM)Implementing SAFe® 5.1 with SPC CertificationSAFe® 5 Release Train Engineer (RTE) CertificationPMP® Certification TrainingPRINCE2® Foundation and Practitioner Certification
Job Linked
Study Abroad
Master of Business Administration (90 ECTS)Master of Business Administration (60 ECTS)Master in Computer Science (120 ECTS)Master in International Management (120 ECTS)Bachelor of Business Administration (180 ECTS)B.Sc. Computer Science (180 ECTS)MS in Data AnalyticsMS in Project ManagementMS in Information TechnologyMasters Degree in Data Analytics and VisualizationMasters Degree in Artificial IntelligenceMBS in Entrepreneurship and MarketingMSc in Data AnalyticsMS in Data AnalyticsMaster of Science in AccountancyMS in Computer ScienceMaster of Science in Business AnalyticsMaster of Business Administration MS in Data ScienceMS in Information TechnologyMaster of Business AdministrationMS in Applied Data ScienceMaster of Business AdministrationMS in Data AnalyticsM.Sc. Data Science (60 ECTS)Master of Business AdministrationMS in Information Technology and Administrative Management MS in Computer Science Master of Business Administration MBA General Management-90 ECTSMSc International Business ManagementMS Data Science MBA Business Technologies MBA Leading Business Transformation Master of Business Administration MSc Business Intelligence and Data ScienceMS Data Analytics MS in Management Information SystemsMSc International Business and ManagementMS Engineering ManagementMS in Machine Learning EngineeringMS in Engineering ManagementMSc Data EngineeringMSc Artificial Intelligence EngineeringMPS in InformaticsMPS in Applied Machine IntelligenceMS in Project ManagementMPS in AnalyticsMS in Project ManagementMS in Organizational LeadershipMPS in Analytics - NEU CanadaMBA with specializationMPS in Informatics - NEU Canada Master in Business AdministrationMS in Digital Marketing and MediaMS in Project ManagementMaster in Logistics and Supply Chain ManagementMSc Sustainable Tourism and Event ManagementMSc in Circular Economy and Sustainable InnovationMSc in Impact Finance and Fintech ManagementMS Computer ScienceMS in Applied StatisticsMS in Computer Information SystemsMBA in Technology, Innovation and EntrepreneurshipMSc Data Science with Work PlacementMSc Global Business Management with Work Placement MBA with Work PlacementMS in Robotics and Autonomous SystemsMS in Civil EngineeringMS in Internet of ThingsMSc International Logistics and Supply Chain ManagementMBA- Business InformaticsMSc International ManagementMBA in Strategic Data Driven ManagementMaster of Business AdministrationMSc Digital MarketingMBA Business and MarketingMaster of Business AdministrationMSc Digital MarketingMSc in Sustainable Luxury and Creative IndustriesMSc in Sustainable Global Supply Chain ManagementMSc in International Corporate FinanceMSc Digital Business Analytics MSc in International HospitalityMSc Luxury and Innovation ManagementMaster of Business Administration-International Business ManagementMS in Computer EngineeringMS in Industrial and Systems EngineeringMSc International Business ManagementMaster in ManagementMSc MarketingMSc Business ManagementMSc Global Supply Chain ManagementMS in Information Systems and Technology with Business Intelligence and Analytics ConcentrationMSc Corporate FinanceMSc Data Analytics for BusinessMaster of Business AdministrationMaster of Business Administration 60 ECTSMaster of Business Administration 90 ECTSMaster of Business Administration 60 ECTSMaster of Business Administration 90 ECTSBachelors in International ManagementMS Computer Science with Artificial Intelligence and Machine Learning Concentration
For College Students

What is an Switch Case in Java?


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.


Video Transcript


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.


Video Recap


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.