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.
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.
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.