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 an Else If Statements in Java?

$$/$$

In the last lecture, you learnt how to use If Else statements that check a single condition and gives an output based on the condition being TRUE or FALSE.  But in practical scenarios, you may have to check multiple conditions to generate an output. Based on what these multiple conditions together evaluate to, you will have different outputs.

$$/$$

You can extend the simple If Else logic to check more conditions. Let us see how in the coming video.

$$/$$

Video Transcript

 

So till now you have learnt how to write simple if else statements in Java. So those IFL statements were suitable for situations where there were exactly two kinds of alternatives. For example, you saw that in the switch example, a switch can have only two states. That is, it can either only be on or it can only be off. So you put one condition in if and one condition in else. Similarly, for the password login example you saw that again there can be only two states. Either the password is incorrect or the password is correct. Now, there might be situations in real life where there might be more than two alternatives. For example, consider the coffee vending machine at your office or at your university. Then there are several different options. In this coffee vending machine there might be that you want to select Cappuccino, or if you select Espresso and press on the Espresso button, then you get espresso coffee out of the vending machine. Similarly, there might be other options such as that of black coffee or latte. So in that case, if you use simple IFEL statements then they are sort of insufficient. Why is that so? Well, let us see. If you try to convert this into a Java code, what would you say? You would say that if I select Cappuccino, then the coffee vending machine provides me with a Cappuccino coffee or Else.

 

Now here, notice that there are only two kinds of conditions which can be taken in inside the if else condition. One condition was Cappuccino and we are done writing it. Now in this Else bracket, what are you going to write? Well, if you write say Espresso here so I say that else Espresso, then do you think this is the correct way of writing this Java code? Well, this is sort of incorrect because if you select Cappuccino you get this kind of coffee, but in all other situations you get Espresso only. So this means that even if I press the black coffee button, I would again be getting espresso coffee. Because since black coffee does not match with this Cappuccino option here, then it directly goes into this Else condition and gives you espresso coffee. Well, this would be a bad situation to be in because there might be several other kinds of options and you're always going to get the espresso coffee. So this basically means that in many situations there might be more than two alternatives. And so Java has an interesting way to deal with such situations. Well, in Java we call them the LSIF statements. So the LSF statements basically help you to tackle these kind of situations. You can say that if I select Cappuccino then give me Cappuccino, else if I select Espresso then give me Espresso, else if I select black coffee then give me black coffee and so on. So in Java we call them the Else If statements and they are useful when there are more than two alternatives. So I say that if condition one is met then action one is taken. In our case, condition one is selecting the cappuccino option. And action one is that the coffee machine gives you cappuccino or you say that else if condition two, then action two happens. Basically here it would translate to that if condition two, that is if you select espresso then espresso coffee comes out of the coffee machine. So if there are N such options or there are several such options then for any Nth option I can say that. Else, if condition N is met then take action N. So basically this is how the else if statements work. And in the last you can say that if any of these conditions are not met then you say else some action takes place. So this action will take place only if all the above conditions are false. So basically whatever options you want to explore here in the coffee machine you provide them in these Elf and else if conditions. And lastly when you reach the end you say that else if none of these conditions are met then you can throw an indication saying that please select an option or any other error. So let us now try to translate this coffee machine example into a working Java code.

 

Video Recap

 

 

  • Java if else statements are suitable for situations with only two alternatives

  • Simple if else statements are insufficient for situations with more than two alternatives

  • The LSIF (else if) statements help to tackle situations with more than two alternatives in Java

  • Else if statements work by checking if a certain condition is met and then taking a specific action

  • For multiple options, else if statements can be used for each option with a final else statement for any options not covered

  • If none of the conditions are met, an indication or error can be thrown

  • The coffee vending machine example can be translated into a working Java code using else if statements.

 

So in the video, you saw how we can use Else If statements to solve some real-life scenarios. In situations where there might be several possible alternatives and only one of those alternatives need to be executed at any given point of time, we can use Else If statements, wherein each alternative is mentioned within one else if block.

Let us now take a look at how the Else If statements can be represented via Java code.

Please download the following file used in the video:

$$/$$

Let us take a look at another Java code example using Else If statements. Please download the file below to understand the video well.

 

$$/$$

You now know how to use "If Else" and "Else If" conditionals. In the next lecture, you will learn how to use a nested If Else statement, i.e If Else statement inside another If else statement.

$$/$$

Now that you have learnt how to play around with Else If statements, it's now time to learn about nested If Else in the next lecture.