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.
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.
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
In the next lecture, you’ll learn about another commonly used conditional operator called ‘Switch Case’.