COURSES
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
Law
Job Linked
Bootcamps
Study Abroad
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 AnalyticsMS 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 Administration | STEMMS 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 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 MediaMSc Sustainable Tourism and Event ManagementMSc in Circular Economy and Sustainable InnovationMSc in Impact Finance and Fintech ManagementMS Computer ScienceMS in Applied StatisticsMaster 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 ManagementMSc Digital MarketingMBA Business and MarketingMaster of Business AdministrationMSc in Sustainable Global Supply Chain ManagementMSc 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 AdministrationMaster of Business AdministrationMSc in International FinanceMSc in International Management and Global LeadershipMaster of Business AdministrationBachelor of BusinessMaster of Business Administration 60 ECTSMaster of Business Administration 90 ECTSMaster of Business Administration 90 ECTSBachelor of Business AnalyticsBachelor of Information TechnologyMaster of Business AdministrationMBA Business AnalyticsMSc in Marketing Analytics and Data IntelligenceMS Biotechnology Management and EntrepreneurshipMSc in Luxury and Fashion ManagementMaster of Business Administration (90 ECTS)Bachelor of Business Administration (180 ECTS)B.Sc. Computer Science (180 ECTS) MSc in International Corporate Finance MSc in Sustainable Luxury and Creative IndustriesMSc Digital Marketing
For College Students

What is a Real Life Example of a Switch Statement in Java?

$$/$$

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.

$$/$$

Video Transcript

 

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.

 

Video Recap

 

 

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.