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 AdministrationMS in Information Technology and Administrative Management MS in Computer Science Master of Business Administration Master of Business Administration-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 ScienceMBA 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 MarketingMSc 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 MarketingMSc 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 FinanceMaster of Business AdministrationBachelor of BusinessBachelor 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 MarketingMSc Global Supply Chain Management (PGMP)MSc Marketing (PGMP)MSc Corporate Finance (PGMP)MSc Data Analytics for Business (PGMP)MS Business AnalyticsMaster of Business AdministrationMS Quantitative FinanceMS Fintech ManagementMS Business Analytics PGMPState University of New York Bachelors Program - STEM MSc Business Intelligence and Data Science (PGMP)MSc International Logistics and Supply Chain Management ( PGMP)MSc International Management (PGMP)MSc Psychology & Management (PGMP)State University of New York Bachelor's Year 1 ProgramMaster of Health Services AdministrationM.A Digital Marketing (PGMP)MS in Technology Leadership and Project ManagementMaster of Health Services Administration and Master of Business Administration (Dual Degree)MSc in Supply Chain ManagementMSc in Hospitality ManagementMaster of Business Administration 60 ECTS UAMMaster of Business Administration 90 ECTSMaster of Computer Science 90 ECTSM.Engg Industrial Engineering 90 ECTSM.A in Management 90 ECTSMS in Data AnalyticsMS in Artificial IntelligenceMaster of Business AdministrationMA International Business & LeadershipMaster of Business Administration 90 ECTS
For College Students

$$/$$

Nested If Else basically means an If Else statement inside another If Else statement. You often have to check sub-conditions within existing conditions. For example, let’s say you’re going to a movie. First, you’ll check if the tickets are available at a theatre near your home. Then, you will check another condition, i.e. whether the price of the ticket is worth it. However, if the tickets aren’t available, you will not check this sub-condition.

 

Let’s now listen to Aishwarya as she talks more about using nested If else statements.

$$/$$

Video Transcript

 

Till now you have seen how to write simple if else statements. And recently you looked at the utility of writing else's statements in Java. So basically there might be some occasions in which you check a condition and after that you need to check more conditions. So what will be the real life scenario for this? Imagine that you are trying to book a movie ticket and for that you look up on a ticket booking website. So first you check if that whether the tickets are available in a theater near you. So you say that in this in the movie booking first you check whether tickets are available. Then you say that if the tickets are available or no. Suppose I say that the tickets yes, tickets are available at a theater near you. Then if you don't want to spend so much money and if you want to book tickets only if they cost you say less than 400 then you again check the condition that whether price is less than 400 or it is greater than 400 then you say that if the price of that ticket is less than 400 then book it. Else don't book it. 

 

So here you will observe that there are two sets of entities whose value you need to check at two different steps. First is whether tickets are available or no. Then after checking that the tickets are available you check the price of those tickets. In case they fall under the price which you want the tickets to be then you go ahead and book the tickets else you don't book them. Now, can you think of a clever way as to how Java would handle such situations where you first check this condition and once this condition is fulfilled you go on and check the next set of conditions. Well, in Java you have an interesting way to do it which we call as the nested if else. 

 

Now, as the name suggests, in nested if else there are IFL statements and they are nested inside each other. This actually means that you write an IFL statement and inside that there are more set of IFL statements. So if you intuitively think about it, you can say that if tickets are available then inside that there is again a set of if else condition based on what the price of the ticket is. So the general syntax of writing nested FLS would be that if some condition say condition one is met then take action one and inside this action one basically what you have is this these set of actions. So this action one will actually comprise of another set of if else conditions. So this action one is nothing but saying that if condition one A is met then take some action one A or else take some other action. Say action one B. Notice carefully that here I have used these terms such as one A and one B. So basically these one a and one B denote that this set of else if condition is actually nested inside this bigger LSIF condition. So basically if you try to translate this into Java code you will say that if tickets are available then you write another set of if else conditions. That is you leave a little space here and then you say that if price is less than 400 then go ahead and book it or else don't book. 

 

So this is what I call as my nested if else condition. So this if else is basically going to get executed only if this outer if condition is true. So basically since I opened a bracket here for the outer if condition, I need to close it here like this. So basically this entire thing is one single outer condition. And inside this there is this smaller set of if else condition. So this is what a nested if else is. Also don't forget to add a complementary else condition for this outer if condition. Well the condition for this that basically if the tickets are available then execute these nested if else conditions or simply say cannot book. 

 

So this cannot book basically is for those situations where my condition fails at the first step itself. That is the tickets are not available in the first place. So basically revisiting this. I say that if tickets are available which is at this point I say yes then I check the price. I say that if it is less than 400 then book it. Or if it is greater than 400 then don't book it. And then I close this if condition and I write an else condition saying that else in case the tickets are themselves not available then you throw an indication that you cannot book. So this is how a simple nested if else works in Java.

 

Video Recap

 

  • Introduction to nested if else statements in Java

  • Real-life scenario of checking multiple conditions while booking movie tickets

  • Need for nested if else statements to check multiple conditions in sequence

  • General syntax of writing nested if else statements

  • The use of if else statements nested inside another if else statement

  • The condition to execute nested if else statement only if the outer condition is true

  • Need to add a complementary else condition for the outer if condition

  • Use of the "cannot book" message for situations where tickets are not available

  • Steps to write a simple nested if else statement in Java

 

So you just now saw how nested If Else statements help us in those situations where there might be successive boolean checks. They are helpful in those cases, where after checking one condition, you need to check another condition successively. For that, you first define the outer condition in the outer If Else block, and then write the inner condition in the inner If Else block.

 

Let us now see how to write a simple code nested If Else conditionals in Java.

$$/$$

 

Let us now get straight to practice and play around with writing our own nested if else statements in Java.

$$/$$

In the next lecture, you’ll learn about another commonly used conditional operator called ‘Switch Case’.