- Podcasts
- The Java Developers Hub
- Java Conditional Statements Explained | If, If-Else, Else-If Ladder & Switch
Java Conditional Statements Explained | If, If-Else, Else-If Ladder & Switch
The Java Developers Hub
- December 3, 2026
- 1 h 21 min
- Episode 5
About this episodeConditional statements are fundamental to programming because they allow applications to make decisions and execute different instructions…
- Understanding conditional statements in Java.
- How decision-making works in programming.
- Using the if statement to execute code when a condition is true.
- Understanding the if-else statement.
- Working with multiple conditions using the else-if ladder.
- Understanding nested if statements.
- Using the switch statement for multiple choices.
- Differences between if-else and switch.
- Working with comparison and logical operators.
- Writing practical decision-making programs.
- Common conditional statement mistakes and best practices.
- Beginners learning Java programming.
- Computer science and software engineering students.
- Developers transitioning into Java.
- Students preparing for Java programming examinations.
- Anyone interested in understanding programming logic and decision-making.
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
Java Conditional Statements Explained | If, If-Else, Else-If Ladder & Switch
Yes, so let's start. Welcome back Coder Army to our Java series. And today we will see some more things in the Java syntax. What are the conditional statements, loops, jumps etc. in Java. You will enjoy it a lot. And those who have come from the programming world, they have read some other language. They might know all this, if, switch, else, for loop, while loop, etc. But still, if you watch this video, you will get to know something internal about it. Like we will understand how switch statement works internally. And how switch is better than if else. So we will know all this in this video. And if you don't know all this, even if you don't have any idea, We will understand this from scratch, so it will be clear to you. So, come on the screen quickly. So, first of all, we understand one thing. What is flow of control? So, I will write flow of control first. Flow of control. What does flow of control mean? Flow of control means that let's suppose you have written some code. You have written some code of java. Now when the compiler will convert this code into byte code and then it will read the byte code and will interpret it into machine code one by one. So the question is that when your code is being converted into machine code, when this code is running, So, how and in which flow will it be executed? This is called flow of control. First, let's say, your statement 1 will be executed, then statement 2, then statement 3 and so on. So, this is called flow of control. Now, in any language like Java, generally, this flow of control runs one by one. So, first the first line is executed. So, let's say, you have written a line in this code. So, you have executed the first line. Then you must have executed the second line. Then you executed the third line and so on. Very simple. Right? We had seen the simplest example of this before. Like we used to declare some variable, assign some to it, print some to it. And everything used to be executed line by line. But there are some keywords in Java that break your normal flow of control and cut it in your flow of control. Start. Now, there are some keywords in Java that bring an alternate flow of control other than the normal flow of control. And that is very important too. So, we will discuss each and every flow of control. So, first of all, we talked about the flow of control. So, let's write it again. So I told you that we have three types of java. One is called selection. One is called iteration. And the third one is called jumps. We have these three types of flow of control. Now first we will discuss what is selection, iteration and jumps. And mostly we will discuss this in code because it will be better to see there. So let me tell you about selection first. In selection, two big things come. That is your if statements. In this, your if else letter will also come. And your switch statements come. Okay. If you have heard it before then it's good, if you haven't, then everything will be clear when we discuss it. But let's assume that this comes in the selection statement. Your loops come in the iteration, we will see that later when we finish the selection. Okay? So let's go. First we will go and see what is our if, if-else, switch. Okay? Okay, very good. So now understand this with a story. So what do we do in programming? We try to solve a problem in programming, right? So till now, like we saw the flow of control, so we discussed that it was going straight, line by line. So if you wrote some code, okay? So let's say you would have written in this code, let's say int i equal to 4, right? Then you would have tried to print this, let's say system.out.println and you would have written, print i, right? Then let's say you made one more variable, j equal to 5 and so on up. So what will happen? First, this statement will be executed, then this one, i.e. first i will get the value of 4, then i will get printed, then j will get the value of 5, then let's say j will get printed, and so on, our code will keep running. Very good. But I am saying, The direction of our code is one-directional. What if I break it in two directions? Let's say you were going to your home. This is the way to your home. This way is divided into two parts. You can reach your home from this way and from this way. Which way will you choose? Here. You have to take a choice. You have two choices. Let's say this is choice 1 and this is choice 2. So you have to take two choices. Now this choice can depend on any factor. That factor can be anything. Let's say I am saying a factor. Let's say this road is long. So it will take you a lot of time to reach your house. But it's a good road. It's a nice road. There are trees and plants etc. So let's say there are trees and plants here. So this is a good road. You will enjoy going from here. On the other hand, your second route is a short one, but there is a lot of traffic and potholes here, you won't enjoy going there. But if obviously you have less time, then which route will you prefer? The short one? And you have more time, you have to go in peace, so you will prefer the long route. So here you will have taken a choice, you will have taken a choice by coming to this position, whether you want to go on a long route or a short route. There is a parameter of this choice. What is the parameter? That you have more time, This is because you have less time. If you have more time, then you will obviously go on a longer route because you will enjoy that peaceful route. If you have less time, then you will quickly go on a shorter route. Very good. If we want to represent this condition, this choice in programming, then we represent it using selection statement. And in selection statement, the best statement is your if statement. Now, I had already told you that if we declare some block in Java, code block, then we declare it like this. Right? With what? With curly brackets. So, I opened curly bracket and closed curly bracket. Now, this is an if block. Okay? Let me represent this with a pen. This is an if block. Now, obviously, in simple English, the if we understand is the same in programming. We are saying if. Agar aisa hai, aisa matlab jo bhi humne yaha pe andar likha hai. Agar aisa hai, toh aapko jo bhi if ke andar likha hai, usko execute karna hai. Nahi toh, isko skip kar dena hai. Matlab yehi wali baat, ki do raaste hai, ya toh is raaste pe jaana hai, ya is raaste pe jaana hai. Thik hai? Acha. Toh sabse pehle hum iska basic syntax dikhte hai, ki hum if statement ko likhte kaise hai. Toh iska basic syntax hota hai, hum sabse pehle likhte hai keyword if, phir hum brackets banate hai, aur is bracket ke andar likhte hai hum ek expression. Now I'll tell you what the expression is. We open the curly bracket and close the curly bracket. Now whatever we write in this, I just commented that do something. Now whatever we write in this something, it will be executed only when this expression is true. Now I said this expression will be true. It means that this expression should be evaluated as either true or either false. When I solve this expression, I should either get a true or false answer. And if I use common words, I should get a Boolean answer. If I get an answer, let's say true. then I will execute this statement which I have written in if and if the answer is false, then I will skip this if and execute the code below. What is its simple example? Let's say I declare a variable and not integer, let's say I declare a boolean variable, boolean b and now I call it true. Now, if I write here, if b is equal equal true, now see what I did here, I used comparison operator, if b equal equal to true, if b is true, then do something, we have to do something here, now we are not doing anything, let's assume we will do something, let's say, otherwise, we will do something simple, we will carry on our normal flow, okay, so assume you have written this code, so if we execute this code, then how will it happen, first of all, The control flow will be as follows, we will assign a true value to B, so B becomes true. Very good, B has become true. Then our next line, this if statement will be executed and we will check whether B's value is true. What is its answer? Yes, B's value is true. So its answer is true. Now as soon as B's value is true, then whatever is written in this if, your control flow will go here. If let's say, here we would have replaced true with false, So now we have replaced true with false. So now as we have done false, now we have checked whether the value of b is true. Now its answer is false. So this if will not execute. And we will come out from here and the control flow will jump directly here. Okay. If b is false, then we will not execute it. If b is true, then we will execute this block. As soon as it is finished, we will come back here. I hope you got an idea of if statement. Remember this example. It creates a choice here that you can go in this direction or in this direction. As I told you here, either this code block will be executed or not. Now, there are many variations in this. Like, one is if And after that, you write another word, else. Now, this is a new keyword. Now, it will be able to depict exactly this scenario. This scenario that we discussed, it will be able to depict it exactly. Because till now, we were just saying that either this code block will be executed or not. But now I am saying, now you have exactly two choices. Either you will go to IF or you will go to ELSE. When you will go to IF, the expression written here will be TRUE. So if we write the same expression here, so let's say if it was written here too, B equal equal TRUE. So what does this mean? If this statement is true, then we execute this. And if it is false, then we execute this. It is very simple English. If do this, else do this. Do this means whatever is written in else and whatever is written in if, do it only if it is true. Meaning the above expression is true. Now this is also a if and else. Now there are many variations in this, we will see all of them one by one. So I think it is better to see it in code, then we will understand more things. So let's quickly switch to code and see all the conditions of if else one by one. So see what I did here, I again created an empty project from demo.java and I have written anything, only my boilerplate code is written and we have to pay attention to what is written inside it. So let's take the same example, let's say I took a boolean variable b and now I gave it a value of true. Now I will write an if statement here. So let's first talk about the selection statement and in that too we are talking about the normal if. I have commented this so that we know who we are talking about. So, if I write a statement, if, I will put a curly bracket here and I will give an expression in it. Now, you know what an expression is. Any such statement which can be evaluated. And here I am saying that it should be evaluated either in true or false. So, let's say I gave an expression, if b is equal to true, is the value of b true? This can also be an expression. Let's say, you can write the expression like this. Let's say, I said int i whose value is 5. So, I can also write here that is our variable i equal to 5? Some expression like this. So, this can also be true or false. That is, what is i's value 5? Whenever I ask, what? It will either be true or false. So, I can write anything here. So, let's go with i equal to 5. We will see this later in Boolean. So, what I do? Let's say, I print something. System dot out.println Now I can do anything here. So I printed a command here. Let's say I printed this. I is 5. Very good. Otherwise, I will come out of here and print something simple. system.println And here I will print, let's say, I is not 5. So I printed this. This is my code. Now when I compile this code, I will write my command javac demo.java, then my bytecode will be created. Now suppose my bytecode is created, what will my bytecode do? JVM will run that bytecode and will convert it to machine code with interpreter and just in time compiler. Now, as soon as we convert our code into machine code, our code will start running. Interpreter keeps running line by line. So, first of all, what will happen? Boolean b equal to true. Sign true in b. Int i equal to 5. Sign 5 in i. Then we will see this statement. Is i value 5? Now what is its answer? It is true. So because i value is 5, then this statement will be executed first. i is 5. And then we will come out of it. And then this statement will also be executed. i is not 5. When I run this program, then both will come. i is 5. i is not 5. If you want, then I will run it and show you. One second. If you want, I'll show you how to run it. Both your statements are printed. I is 5, I is not 5. Now, this is wrong. You don't have to execute both the statements. You want only one. Either I is 5 or not. Simple. Your else works there. So, first you wrote if and then you wrote else. And you put this statement inside else. See, you put it inside else. Now, see what will happen. Now, if i is 5, then it will be executed. If it is not 5, then it will be executed. So, you have two choices in your control flow. Either this or this. So, what will be executed now? Now, x is 5. Sorry, i is 5. Absolutely correct. Now, if I do some other value here. Let's say, I do 6. So, what will be executed? Then it will be I is not 5. Very simple. Right? Now I can do some more things in this. For example, this is done. I told you that it is simple. That I can play on one variable. That is, is it 5? But I can also play like this. Let's say, if I is greater than 5. Okay? Then I will write, I is greater than 5. Okay? Else, I is less than or equal to 5. Something like this. Now, if i is greater than 5, then this statement will be executed. If it is smaller than 5 or equal to 5, then this statement will be executed. Now, i is 6. So, what should be printed? i is greater than 5. See, this is done. So, you can write any expression here. So, greater than, equal equal, less than, less than equal. In fact, you can also write with logical operators. I is greater than 5 and I is smaller than 10. So, what are you saying? I should be bigger than 5 and smaller than 10. Means, it should lie between 6 and 9. So, it does lie. So this is how you can write any complicated statement here. But the whole statement should be evaluated in either true or false. Now let's see a simple code, which is very normal, so I'll comment it. Now let's say I tell you a code that you have to do. If else we have to write that if any i, which is our i variable, if it is an odd number, then it should be printed as i is odd. But if it is an even number, then it should be printed as i is even. So how will you do this? So now you will write if. This is our I. Now, do you know how we get odd or even in programming? It's a very simple way. We divide that number by 2. So, let's say I divided 6 by 2. Now, when I divide 6 by 2, I'll see what's its remainder. If it's an even number, then its remainder will always be 0. And if it's an odd number, then its remainder will always be 1. So, basically, I have to check the remainder of someone. So, there we get the modulo operator. Modulo. Do you know what modulo and remainder are? So, I'll do if I modulo to E1. When I divide this 6 by 2, my remainder is 0. Because if this is the case, my i is an even number. So I can print system.out.println that my i is even. Okay? Now I can write else here, if it is not like this, then what should be printed? I is odd. So I print this, I is odd. Okay, so let's check what's happening now. Okay, that bracket was not extra, I guess that bracket is of use. Okay, so let's see first of all, here, yes. First of all, the value of I is 6. Now 6 is an even number, so it should be printed, I is even. And if I make i an odd number, let's say I make oi 9, then 9 is an odd number, so it should be printed as i is odd. So I hope you have understood if and if-else. I hope you have understood this. Now let's see the other small things. The first thing is that the curly brackets that we have put here are only required if there are multiple lines of statements in our if. Now if there is only one statement in it, then we can ignore this curly bracket. We can remove both brackets. Then also the execution will be the same. In the same case, when there is only one statement in else, then we can avoid the curly bracket. It becomes optional. So in this also the code will be executed in the same way. So if it was i9, then our print will be i is odd. So, curly brackets are optional here. But they become necessary when your statement is in multiple lines. So, in one line, we can use curly brackets or not. Whether I used it on if or not, it will work. But remember, a good coding practice is to use curly brackets. Because in case your line of statements increases later, you will have to use it later. So before that, even if it's a single line, applying curly brackets is always considered a good coding practice. So this was one small thing. Secondly, I have another type of condition, which we call nested ifs. Now what is nested if? Nested means, if has another if in it. So what does it mean? Suppose I wrote first if, i is greater than 5. Here, I opened my code block. Now, inside this code block, I can write one more condition. If i is less than 10. Something like this. So, basically, what I want to say here is, first, I check if i is greater than 5. If it is, then I enter inside this code block. Then I ask again, is i greater than 10? Sorry, is i smaller than 10? If it is smaller than 10, then I enter it in this code block. So let's say I write else here. And I write else outside of this as well. Okay? So I hope you are able to understand who is whose else. First of all, we can even close this. First of all, this statement is executed. Is i greater than 5? If not, then you will simply go to this else. Our control flow will simply go to this else. But let's say that i is greater than 5, in that case we will enter this code block. This i greater than 5 code block. In that, another condition is checked. As i is greater than 5, we enter this code block. Then we check whether i is smaller than 10. If i is smaller than 10, then we will enter if. And if i is not smaller than 10, then we will enter else. So whatever else is written is always of its previous if. So this else is of this if and this outside else is of this outside if. This is called nested if. There is another if inside the if and you can do this. In fact, in Java, you can go to any level of nested. So I can write another if inside this. Let's say I can write here I is equal to Equal equal to 7. Although it doesn't make sense. But I can keep doing it like this. In if, second if. In it, third if. In this way, I can go to 4th, 5th. As many nestedness as I can go. Although, again. A good coding practice says this. That you should not nest more than 2-3 levels. In fact, 2 levels are maximum. For nestedness. Okay. Because that makes your code not readable. Now look. If I am reading a code, then how difficult will it be for me? I greater than 5 is good. If it is, then I will go here. If it is less than 10, then I will go inside it. If it is equal to 7, then I will go inside it. Like this. And if it is not anywhere, then I will come outside it. Right? So... It is very difficult to read the code if you have written nested ifs. So your programming becomes a bit complex. So we should avoid it. But just to tell that we can do it. Secondly, every if can have an else or not. For example, in this case, this if has an else and this outside if has an else. But this inside if doesn't have an else. Meaning if this condition is false, then nothing will happen. See what I said. Let's assume in this case, the value of i is 8. So let's see which condition is true. First of all, is i greater than 5? Absolutely right. So we will enter in this here. Sorry. First of all, we will see is i greater than 5? Absolutely right. So we will enter in this if. So we came here in this if. Then we will check in the next statement that is i less than 10? i less than 10 is absolutely right. So we will enter in this if too. Now we are in this block. Then we will check if the value of i is 7. No, the value of i is not 7. So we will come out of this if. Look, first of all, as we come out of this if, we saw that there is nothing else to execute here. That means this if is also finished. So we will come out of this if and stand. So we came out of this if. Now as we come out of this if, Will we enter this else block? The answer is no. Because this else block is the else of this if. And it would have been executed only if this if failed. But this if was true. This if was true, that's why we entered it. The value of i was 8. So it was bigger than 5, that's why we entered here. It was smaller than 10, that's why we entered here. As soon as we entered it, we were working in this block, then this else block will say that I don't need to work, right? Because if executed, even if we did nothing inside, as soon as we went inside, I equal equal 7 failed, then we came out of here and then we came out of here. But now we are standing here. As soon as we are standing here, you know, else will not be executed. So else will not be executed, so we will skip it, we will come here. Okay, I am explaining you an example of how flow works. Now as soon as we come here, So we will also come out of this. Now as soon as we come out of this big if, we will see again if we should execute else block. The answer is no, because this one was true. So we will also come out of this. As soon as we come out of this, we come out of the whole code. This is the main line of our main function. If we come out of this code, then our flow will stop. I hope you understood how all flows work in nested. You can nest if, i greater than 5, one more if in it, one more if in it. You can do this. Every if has its own else block. Like here and here. And it can't be like here. There is no problem. Okay. In fact, I told you that we should avoid this nested. In fact, there is a correct way of it. If you want to check that i is bigger than 5, smaller than 10, and equal to 7. First of all, this condition is wrong. But this If we had to check 7 equally, we would have done it outside. But for example, if you want to check something like this, then you can use logical operator and check it in one if. How? Something like this. You can write if i is greater than 5, And you can write and i is less than 10. So, in the same way, you can write conditions in the same if. Now, you open this. Now, you don't need nested if. Now, you will enter in this if only when the value of i will lie between 5 and 10. This was also happening here. The value of i was bigger than 5 and smaller than 10. So, you would enter here. What did I do better than this? I put it in the same condition. Okay. So this is always better. I hope you understood nested if. We saw simple if, if else and nested if. After this, there is a third variation of if. After this, there is a third variation of if. I have commented everything. From here to here. There are only two things, boolean, b equal to true, i equal to 9. Let's bring it down so that there is no confusion. Let's assume there is no code written at the top. The code starts from here. The code starts from here. Let's assume there is nothing written at the top. Start. Now, the third thing we are going to see is called if else if ladder. Okay, now what is this if-else-if letter? This is also very interesting. You imagine that I gave you an example that if statements give you two choices. One, you can go in this direction. Else, you can go in this direction. If you write else, then there will be exactly two directions. And if you don't write else, then in a way, you will feel that if you go in this direction, then it's okay. Otherwise, it's okay. You can walk on your normal path. It was the same analogy, right? If-else-if letter says, what if you have multiple choices? Right? So, for example, I wrote i equal to 9. What if I check if i is 5. If i is 5, then execute whatever is written here. Let's say execute this. Then I write else if. So I can also put if with else. Else if. Now here also I have to open bracket and write one more condition. Let's say i is 6. If it is. So let's say again I write execute this. Write something here. And let's say instead of execute this I print it. System.out.println. Okay. And let's say I write here I is 5. Right. Let's print it simple. And in the same way let's print here let's say I is 6. Right. Now in this way I can make a ladder. First I wrote I equal to 5 if. Then else if. Then I can write again else if. If i is equal to 7, then write i is 7. Something like this. I can form a ladder here. Now let's say I can write else if i is equal to 8. If i is 8, then write i is 8. Finally, let's write one more. Let's say else if i is equal to 9. So execute it. i is 9. Something like this. Here else will come. Okay. So what did I do here? I made a ladder here. I made a ladder that if the value of i is 5, then execute it. If it is 6, then do this. If it is 7, then do this. If it is 8, then do this. If it is 9, then do this. In this way, we formed a ladder. And we call this if else if ladder. So let's see how it works. So it works like this. First of all, our control flow will go to this line. It will say, okay, you have to assign 9 to i. Done. After that, we will come to this letter. We will check first if the value of i is 5. If the answer is true, then we will execute it. If the answer is true, let's say i is actually 5. Let's say I have written 5 here. If it was written as 5, then it would have been true. As soon as it was true, we would go inside this and print on the screen, I is 5. Now we knew that we have multiple paths and we can take only one path. We took this path, this one. If we took the path of this code block, then we will neither take this path, nor this path, nor this one, nor this one. This means that as soon as our this statement will be executed. We will not execute any other statement from here and new line. I have made it a little smaller so that you can see the whole code at once. So I hope you have understood. Let's say if i is 5, then this statement will be executed and none of these will be there. And we will come here in the last in this statement. And here nothing is written, so we will exit from our program. And none of these will be executed. So if we run it, then your i is 5 should be executed. Absolutely right. And none of our print should be there. But let's say if its place was i is 9, then what would happen? First we check if i is 5. Answer is no. Then we check if i is 6. Answer is no. Then we check if 7 is no. 8 is no. 9 is yes. Then our print is i is 9. And we come out of this if else letter. So we print this and see. i is 9. So basically till our answer is not true. We all went by checking if else if else if condition. But the moment our answer was true. Let's say our answer was true on 7. So we first checked this, checked this, and checked this. As soon as our answer 7 became true, we printed this statement. After that, we didn't even check these conditions. Why? Because we don't have to go inside this. We know that we have chosen this path. So we have to come out of this entire ladder. We have to come inside this line here. Right? I mean, our control flow, the flow of our program, has to come out of this entire ladder. If one of these conditions is true, then... We call this if-else, if-letter. Okay? I hope you have understood how if-else, if-letter works. Now, what is its use? It has many use cases. Look, it will not always happen that you will have only two choices, if or else. Sometimes you will have multiple choices. You will have to see what is the value of let's say i. So, sometimes you can assume that there are many conditions in which you have to test whether any particular condition is true or the second condition is true or third or fourth or fifth. According to that, you can put the IF-ELSE letter here. So, we will see many examples of this. One of them is a very good example that I can show you right now. Assume that Now I will write one more else if in which age is greater than 60 In that case I will write system.out.println and here I will write you are old I can write one more condition 40, it's bigger than 40. It can be between 40 and 60. So, it's bigger than 40. So, let's say, I'll write, let's copy this, you are old. So, you are, what's written? Young can't be written between 40 and 60. So, let's say, let's make this U. And let's make this U too. So, let's write this, let's say, you are becoming old. I'm writing anything useless. Let's say, let's write one more here, else if age is your It is between 20 and 40. It is bigger than 20. So, I will write here. Let's say you are young. Something like this. Okay. Now, I have written. Handled 80. Handled above 60. Handled above 40. Handled above 20. Now, I have not handled below that. So, in if else if letter. We have one more thing. That we can finally write an else. So we wrote if, else if, else if, else if, else if, as long as we wanted to write. After that, what did we do? We wrote an else in the last. That if none of these are true, then we will go to this else block. If none of these are true, then we will go to this else block. And let's say I have written in this else block, you are a child. Something like this. Let's say this is the scenario. Your age is 50. You know what will happen? How will it be checked? The program flow will come here. First, the age variable will be assigned the value of 50. So, our age here will be 50 by this statement. After that, we will check whether the age is greater than 80. No, the age is not older than 80. So this condition failed. Then we'll check again. Is the age older than 60? No, the age is 50 and not older than 60. So this also failed. Then we'll check, is the age older than 40? Yes, this is true. Because our age is 50, so it is older than 40. So we'll execute this, you are becoming old. After that, we won't execute these two statements. And we'll come out straight away. So see, what was the benefit of this if else if letter? If you think carefully, If we don't come out of it, like we printed age greater than 40, you are becoming old. And after that, we check this condition too. So what would happen? See, our age is 50. 50 is bigger than 40, which is true. So we executed it. But if our condition is also checked, that our age is bigger than 20, then its answer is also true. 50 is bigger than 20. So then both our conditions are printed. You are becoming old and you are young too. But if else if letter takes care of it that it doesn't happen. So what will it do? As soon as a condition gets fulfilled, like it got fulfilled here, after that it won't even check these conditions and it will come straight out of here. Okay. So in this case, what should be our print? You are becoming old. You are becoming old. Absolutely right. Right? But think if we don't use if else if letter and keep writing simple if if. So let's say here also a simple if would have been written. Here also a simple if is written. Here also a simple if is written. And here else is written. So whose else is this? This else is of this if. Right? So now we are not talking about an if else if letter. So you know that whatever else block we write, whose else is it? Of our previous if. Right? So I assume that this if has no else. This if has no else. And this if also has no else. Okay? If we talk about if else if letter, then all of this would be connected. But for now, everything is independent if. This is different if. This is different if. This is different. This is different. And sorry. And our final one is also different, which has its own else block. So in this case, think about what will be printed. Now let's see, first of all, the age will get the value of 50, very good. Then we will check, is the age bigger than 80? No. Is it bigger than 60? No. Is it bigger than 40? Yes. So we will print it, you are becoming old. Then we will test this if again. Why? Because this if and this if are independent. We have not made a letter, if else if's letter. So this one will also execute. Then we will check if the age is older than 20. Then again the answer is yes. You will print you are young. Now you are young is printed. So we will not go to the else obviously. Because these two are connected to each other. And they will come out from here. So now see two statements should be printed. You are becoming old and you are young. So let's see once. See this. You are becoming old. You are young. Okay. So I hope you have understood very well. If it is clear. And what else? If-else will be cleared. Okay? You will have nested if cleared. And finally, you will have if-else if's letter also cleared. So, I hope you have cleared these things. Rest, the more you practice, the more you use it in the code. I will give you some example problems also. You can see in the description of this video. If you practice it, then your if, if-else, if, nested, if's crystal will be cleared. Okay? So let's move on to our second selection statement which we call switch statements. What do we call it? We call it switch statements. Now this is very interesting. It is like if else if ladder but it is a little different. So let's first understand what it is, how it works and then use it in code. So now we are going to see our switch statements. Switch statements. Now this is very interesting because it is very much the same as your if else if letter which we discussed last time. It is very much the same but still it is a little different and more optimized. So we can write it as more optimized. Why is that? We will discuss that later. First we will understand what is its syntax and how it works. Again, so let's talk about the same scenario. Let's say you have multiple routes. You were coming from one route. So from here you can go here. You can go here too. You can go here too. And you can go one more route here too. So let's call it like this. Route number 1. This 2. This 3. And this 4. And what was the direction of coming? You were coming from this direction. Now here you take a choice. Here you take a choice. That either you can go there. Or there. Or there. Or there. Something like this. You take a choice. Okay. Now, in this case, for this scenario, what did we discuss? We discussed if-else-if ladder. If you take a choice here, then we write something like this. If we write a code of if-else-if here, then we write like this. We write if choice is equal to 1. So, let's assume this is 1 choice. So, we go to 1 else-if ladder. Let's write choice is equal to 2, go to 2, write some code of 2, and so on. We write for 3, 4, and so on. So here you must have understood how if-else-if-else-if letter works. Now how does switch work? It's pretty much the same. difference in syntax. Now understand this. First of all we write let's say we declare a variable here and i equal to let's say 3. Now I write a switch statement below this. So first of all I will write a keyword switch. This is our keyword. We have to write this. After that, I will write the condition of the switch. Switch on which? On which you are switching. So I am switching on i. Now I will tell you what it means. Then you will open your curly brackets. Let's say I have opened it till here. See what we will write in it. We will write statements in it. Statements means we will write let's say case 1, colon. Now I will tell you what is colon and how we will write it. here we will write a statement let's say we will print it system.out.println we will print here that i is 1 ok then we will write one more keyword which we call break this is also a keyword we will discuss about it later then we will write again case 2 it is very simple you might think it is a little complicated but it is very simple so let's do the same work here we will print it and write break then we will write case 3 then we will do the same work We will print it and write break. Something like this. Let's make it a little lower. Something else. So we have written case 1, case 2, case 3. In the end, I can write another keyword which I call default. And here also I can print something and finally I can do break. This is our complete syntax of writing a switch statement. Now let's go in-depth and understand this syntax. So what does this statement say? This statement says that first of all our control flow will come in this line. And what will it do? It will assign the i variable as 3 literal. So obviously there will be a block, its name will be i and what value will be there? 3. Something like this will work till here. Then we write switch i. Okay. So now see how our control flow will work. It is saying that we have to switch on i. Now we have to switch on this i. Now switching means that you have to compare this i with these cases. Here you have to compare the cases written inside. Okay, very good. Now how will we compare? First of all you have to write case 1. Means compare i with 1. So it means that if I equal equal 1, which you had written there in if else if letter, this is the same, I equal equal 1, if there is, then we used to execute the statement inside it, in the same way, if case 1 is true, then we have written here colon, and whatever is written inside it, before the break, we will execute it, so here we are not doing anything, we are printing it, we are just writing I is 1, After that, we wrote a break. Why did we write this? Let's discuss it in a while. Then we wrote case 2. This time we are saying to compare i with 2. This is equivalent to, first we write if i equals 1. And then we write something here. And then we write else if i equals 2. And then we write something here. So it is exactly the same. First we wrote case 1. Then we wrote case 2. Right. Now if case 2 is true, we will execute it before break Then we wrote case 3, then we will execute it If none of these are true, then we have one more keyword, default And in this if else if ladder, it was equal to the last one else Remember we used to write else if something, then one more else if something And in the last we used to have one else And this else says that if any of the above conditions are not true, then I am the only one who will be executed. By the way, this default says that if any of the above cases are not true, then execute me. So, whatever will be written before the break will be executed. Okay. So, this is the format of the entire switch. Now, the biggest question, two questions, in fact, should come to your mind if we go from the first thought principle. When the first question, If else if ladder ban gaya tha, toh hume switch ki zarurat kyun padhi? Almost same hi toh kaam kar raha hai. Main isi ka if else if ladder se alternative likh sakta hu. Bilkul same code likh duga main. Toh mereko naye keywords ki zarurat kyun padhi? Toh pehla question toh aapka hona chahiye. Doosra question. I said, here we are writing break, break, break. What does this mean? And why am I writing this? Okay, this is the second question. In fact, I will introduce you to the third question. I said in the starting, this switch of yours is more optimized than if, else, if letter. It is more optimized than this. So, the answer to the first question can be that because it is more optimized than this, that's why we use it, that's why we use the switch. But if it is more optimized, then why don't we just use this? Why does our if else if letter exist? So, there are many questions which we will answer one by one. First, we will look at the syntax of this switch statement in code and understand what is its use. And in code itself, we will see what is the use of this break. Okay? Let's go. So what I have done here is to create an empty file from demo2. java Because here we are going to talk about switch statements I could have written in that as well but that file will be too big We will not be able to understand it so we have kept it to understand switch statements independently So let's say I declared a variable i equal to 3 Now we will switch here So what I told you is that we will write the syntax as switch i here we will start the curly bracket and then we will make cases so first of all we have written case1 then we will put colon and then we will write let's say we have printed it system.out.println we have printed i is 1 and then what we get is break now we are writing exactly what we have discussed ok then we used to write case2 that compare i with 2 ok then we used to write this again that do this that print i is 2 Then let's write another case, case 3. So what do we write in case 3? One second. We write our case 3. And in case 3, we write I is 3. Break it here. And in the last, what did we write? Assume case 1, case 2, case 3. Apart from this, if none of these three are true, then we would write one, default. This is also a keyword. And what did we print in this? Sorry. In this, we would write, let's say, i is let's say i write here greater than 3 because if there is no 1 no 2 no 3 then it will be greater than 3 so i printed i is greater than 3 and here also i wrote break so this is our The entire syntax of writing a switch statement. Now, how does the flow of control work? First of all, we go to this line, i gets the value of 3, then we come to switch i. So we have to switch i, we have to compare i with these cases. Now see how our flow of control will be executed. First of all, i will be assigned the value of 3, then we will come to switch i. Now, what I am about to tell you is a little bit wrong. I am telling it wrong so that you can understand it better. And when we will discuss how the switch is more optimized, then I will tell you the correct version of it. So, let's assume that what we think is the first thought principle, First of all, we will compare i with case 1. Is i value 1? No. So we will skip this case and come to case 2. Then we will compare i with case 2. Is i value 2? No. So we will skip this case and come to case 3. Then we will compare i with case 3. Is i value 3? Yes. So we will print i is 3. From here we have written break, which means if we execute case 3 once, then we will come out of it. After that, whatever is written below, there is no need to execute it. And we will come out of this whole switch and stand. So our symbol should be printed, i is 3. Now, what is this break? Okay. Let's understand this later. Okay. So, I hope you understood. If I print it once, then your print will be I is 3. Very good. Now, for example, let's say I is 5. So, neither this is true, nor case 2, nor case 3. Your simple default will be printed, which is I is greater than 3. Okay. Very good. Okay. Now, we have seen its syntax. We understood. Very good. Now, let's answer the first question. What is this break? To explain that, I will do the value of i, first 1. Now what should be printed? i is 1. This should be printed. Let's see once. i is 1. Very good. Now I will do one thing. I will remove the break from this. And I will remove the break from this too. And I will remove the break from this too. I have removed the break from all three. We will learn by doing it. And I will remove the break from default too. I have removed the break from all. I have written something like this. Now what is the expectation? What should be printed on the screen? Let's see directly first. What is printed here? i is 1, i is 2, i is 3, i is greater than 3. Everything is printed here. All the cases are printed here. Yes. So this switch statement is a special thing. If you don't write a keyword break at the end of every case, then it will go into the second case. Whether that condition is true or false. See here the value of i was 1. So the first case was executed. But because you didn't write break here. We avoided writing this. So we didn't write this. So it directly jumped to case 2. It didn't compare i to 2. It directly jumped to it. It directly printed i as 2. Now here also it didn't write break. So it jumped to next case. And it printed i as 2. the break was not written here too, then he jumped into the default as well. So, basically, now he is not comparing these cases with this i. He is entering into these cases, the next one directly, and he is printing it. Okay? So, okay, we understood that break means that we have to stop it in a particular case. So basically if you compare it with if else if, then you want that once if else if is executed, then we get out of that if else if letter. So we have put a break in the default. Very good. But why? Why do we need this break? A syntax like this can be made where there is no need for a break. Now look, the reason for putting a break in Java in this switch case is that sometimes you intentionally want that if your particular case is true, then your multiple statements should be executed. Now, why will this happen? There are many use cases. When we solve the question or after the Java series, the Spring Boot series, you will see that we can use the switch statement in many ways. Sometimes we specifically want that if the case 1 is true, then this should also be executed, this should also be executed, this should also be executed, and this should also be executed. And secondly, if the case 2 is true, then this should be executed, this should be executed, and this should also be executed. Basically, whenever a case is true, all the cases below it should be printed. We can sometimes want this. So for that case, to give us more control, Java has given us a break statement. That you can break anytime you want, you can't break if you want. Okay brother, this is also understood. But I am saying that I have to give this whole switch, this whole switch, I could have denoted it with an if else if letter as well. Let me show you. I wrote the switch statement with an if else if letter. If i is equal to 1, then print i is 1. If i is equal to 2, then print i is 2. If i is equal to 3, then print i is 3. If not, then print this. Else means your default is in the switch. What are all these cases? The cases we made above. Case 1, case 2. Case 3. Something like this. If. Else if. So if this is. This way we can write code. I will comment this for now. So if this is. Then why does our switch exist? First question. Okay. And second, if you say in the next statement that our switch is more optimized, then why does this effusive ladder exist? Okay. It's a very important question. If we think from our first thought principle, then the first question is why does switch exist if there is an effusive ladder? So the answer to this is that our switch is more optimized in some cases than our effusive ladder. And for those cases, we use switch. Okay. For example, the best use case is this one. In this case, if we were to compare it like this, our switch is more efficient, meaning it will execute faster, our code will take less time to execute it, rather than if we write its if else if ladder, which we have written down here. Why? We will discuss that now. This is the first reason why it exists. Okay. Second, we said that even if it exists, then what will happen to this if else if letter? If it is more optimized, then use this only. If else if should not be used. The answer to this is that you cannot use switch everywhere. Okay. So, I will write here. Your switch has a limited use case. It has a limited use case. And do you know what is its limited use case? The first thing, the thing on which you can switch, means the expression you will write here I will write here, you write switch and inside you write an expression Now the expression you have written inside, this should evaluate to either byte, short, care or enumeration Okay. Now, what is enumeration? You can ignore it because we haven't studied it yet. But, now you understand these four. That your expression, when you evaluate this expression, then your final answer should be either byte, or short, or end, or character. Okay. Apart from these four, nothing else should happen. Enumeration can also happen. Okay. But, now we haven't studied it, so we are ignoring it. Apart from these four, if anything happens to you, then your switch statement won't work. Okay. I hope you understood this. Okay. You said that its evaluation should be byte, short, int, or char. So we switched switch to i. i is integer so it will work. Can I write something like this? Switch i is greater than 4. I can't write. See its carer came. Cannot switch on a value of type boolean. So I can't switch on boolean. Okay, very good. So I can switch on i. I hope you understood whose limitation it is. Why are these limitations? You will also understand this when I will discuss with you why the switch is more optimized. Second, there is one more point in this. Limited use case. One is that it should evaluate to bite short and long. Not long, sorry. And care. And another is enumeration, which we will discuss later. The second line is that your case, case 1 and case 2, can never be the same. Everything should be unique. I can't write case 1 here as well. There will be an error. What will be the error? Duplicate case. So case can never be duplicate. You can write here, no duplicate cases allowed. But will this also happen in the if else if letter? Let's see. So this was my if else if ladder If I write here again i equal equal 1 I didn't get any error And if the value of i was 1, would both the statements get printed? Let's see, the value of i is 1 right? Let me close this switch once The value of i is 1 and here we have a ladder So first of all it will test the value of i, it will print i is 1 See I have written i equal equal 1 again So will it print again or not? What do you think? Let's print it and see No, it was printed only once. And it's absolutely right, we had read that if the first statement is true and it is executed, then we will skip everything. Right? And here also it is happening, even if this condition is true, but we are not evaluating it. We skip this whole if. Okay? So you must have understood that we could write duplicate cases there. Okay? If I uncomment this again, yes. But it is not allowed to write duplicate cases here. No duplicate cases allowed. Okay, very good. Now, there is one more thing here. That one more thing says that whatever we have just read about switch, these are all things before JDK 7. When the 7th version of Java Development Kit came, all this was true before that. Nowadays, after JDK 7, so I will write it like this, let's say after JDK 7. First thing, strings can also be used as switch expression. So here you can use a string as well. We haven't read strings yet. But when we read strings, you will understand that you can use strings as a switch expression. And secondly, there are a lot of enhancements in switch. So I will write after, let's say, JDK 14. So after JDK 14, after its 14th version, switch has become very enhanced. You can do a lot of things in it which we haven't discussed in classical switch yet. But for that we will make a different video, when we finish core java, then we will talk about java enhancements after JDK 8. Because in core java, we will read all the things that were used till JDK 8. Because it was a core, there was no drastic change after 8, there were some changes. But there were a lot of changes in JDK 8. If you have understood JDK 8 well, then you have completed the whole core Java. After that, the small features like in C + + or Java, some features are added every year. They are very limited. And generally, they are the enhancements of the old features. Some small things change or a new thing is introduced. We will discuss a separate video on that. In that, we will discuss the enhancements of the switch. But let's assume after JDK 14, switch is enhanced too much. Okay? We will discuss it when we finish our basic code Java. When we finish our basic things, then it will be more fun to understand. But traditional switch works like this. And in fact, traditional switch is always supported. Even today, if you write such a code, it is not wrong. Enhanced means that you can do more things in it. But you can do what you already could do. Right? So I hope you understood this switch too. Okay? Now, Let's quickly see why I said that the switch is more enhanced than our if-else-if ladder. So, we are going to discuss some points here in switch vs if-else-if ladder. And in this, our main point will be why it is more efficient. Generally, many people won't tell you why switch is more efficient than if else if letter. Because again, this is something to discuss internal architecture. And because we discussed that in this whole series we will focus more on internal architecture. So we will discuss that here. So first of all, I will give some pointers. The first pointer is that the difference between these two is that your switch statement can only test equality. But your if else if letter can also evaluate the Boolean expression. What does it mean? Remember what I said. I said that in the switch, I can write like this. Let's say there is an int i above, whose value is already 4. So I can switch to i. Right? Here, something like this. Now, I can make multiple cases in it. But I can never write here that I am switching to i greater than 4. No. Because this is a Boolean variable. That means, the answer that will be given to me after evaluating will be Boolean. And I can't switch to Boolean. I can switch to byte, short, int, and your character. Okay. Now, we ignore strings and enumerations. So, I can switch to these four. But, on the other hand, you know that when I am writing an if-else-if letter, then I can write such a condition. If i is greater than 4. Right? So here we have to do something else if let's say i is less than 8. Whatever I am writing here, I can evaluate that in Boolean as well. And I can also evaluate it in equality. Like we just wrote an example, I can also write a simple one. If I is equal equal 4, I can also write this. Although, this is also a Boolean. Right? I is equal equal 4 is also a question. Is I equal to 4? Its answer will either be true or false. So, this is also a Boolean. But what I mean to say is, here there is only an equality. You are just comparing i with 4 and here you can use less than, greater than, anything. So it is wrong to write a little bit of boolean here. You can write other than equality. So you can test other than equality as well as equality. But here you were only able to test equality. So I hope you have understood this point. So let me write it properly. So I wrote this point correctly that your switch can only test equality but if else it can test your equality and inequality both. Okay? Let's go. What was your second statement? Our second statement was that your switch is more efficient than if else. Okay? More efficient than if else. If else if letter. If else if letter. It is more efficient than this. Why? Let's know the reason for this. And if you know the reason, you will enjoy it. Now here is a little disclaimer. You will understand the reason of this much better. If you already have a little idea of the program, you know what arrays are, then you will understand the meaning of this well. But if you are starting Java for the first time, you don't know what arrays are because we haven't even studied them yet. So, it's okay. You can just listen to this for now. I hope you understand a little bit. Even if you don't understand, it's okay. When we will read the arrays, then you will understand everything. Okay? So, let's go. Now, I will compare you to switch and if-else, if-letter. So what I did here, I wrote the switch statement and the if else if letter side by side and compared them for a particular case. In both of them, I would have declared a variable, let's say in the middle, int i equal to let's say 2. Here this variable is declared in the middle. Now after this, I can write this code or this code. Now both the codes are equivalent. Here I am switching on i and comparing case 1, case 2, case 3 or default. Here too I am using if else if letter and I am comparing it with 1, else if 2, else if 3 or else. Both of these codes are the same. And I can write anything in my Java program, both of them will be correct. But still, This one is more optimized than this one. Why? First of all, we understand what happens in the if else if letter. Whenever your control flow sees this whole code, then how does it evaluate it? First of all, it says, i is assigned as 2. Very good. Done. This step is done. That means, let's make it once. Let's say a variable will be i. Okay. Container will be found and its value will be 2. So, this part will be done. After that, let's move ahead. First of all, it saw this statement. So, it checked if the value of i is 1. No, it is not. So, it skipped this if code block and came to the second block. Now, it tested if the value of i is 2. Yes, it is. So, now it entered inside this block. Here, do something is written. So, whatever it has to do inside this block, it must have done. Let's say, it must have printed something on the console. As soon as it becomes true, it will skip this and this and it will come out from here. So basically, it means that first evaluate this statement, it becomes false. Then evaluate this statement, it becomes true. And as soon as it becomes true, it comes out from here. And it ignores this and this. So in total, two statements are evaluated. This is what we understand. Now, when we took the same example in switch, I thought it was a little bit wrong. Now I will tell you what is the correct way or what happened. First of all, again, i equal to 2, an i variable 2 must have been assigned in the container, very good. After that, as soon as you wrote switch i, the compiler did not compare it one by one, that first i was compared once, if it is correct, it is executed inside, otherwise it came to case 2, it did not do that. What did it do? As you can see, the switch i compared the value of i to the value of 2. It went directly to case 2. Here, it evaluated this first, then this, and then it left these two and came out. But here, what happened in the switch? It directly implemented case 2, 1. Neither this, nor this, nor this, and came out directly from here. That means, only one evaluation. Now, let's assume if the value of i was 3 instead of 2. So, if else, I would have evaluated this too. Then it evaluates this as well. Then it evaluates this as well. 2 and 3. So there are 3 evaluations in total. And then it comes out. That our switch does what? If the value of i is 3, then it goes directly to case 3. Right? So always the evaluation is 1. Now you might say, but how will it do it? How will it go directly? The answer to this is our JVM. Or Java's compiler. See what it does from inside. When it sees a switch statement, then it makes a table from inside. Which we call jump tables. Jump tables. Okay. Now what are jump tables? You can consider a jump table as an array. What does it do? It makes a table from inside. That if one comes. then I have to evaluate case 1. If there is 2, then I have to evaluate case 2. If there is 3, then I have to evaluate case 3. And if there is nothing among these, then I have to evaluate default. So now these conditions that we have written, if I want to write them in array form, then how can I write them? You know, I will write it again in array form. So you will understand it better. Now, how can we do this? You can think of it like this. Let's say you make an array. It's not exactly an array, obviously. But you can think of it like this. Let's say you made an array of 4 indexes, 0, 1, 2, 3, 4 lengths. So what did you do? On its 0th index, you put the code of case 1. On the first index, you put the code of case 2. On the second index, we put the code of case 3 and on the last index, we put the code of default. Now see, what does this array give us? It gives us random access, which means that whatever switch i is, if we look at it randomly, if i is 4, then I know that I have to go to code 4-1, which is the third one. i.e. execute the default. If the value of i is 2, then I have to go to the first code. This is what we are doing. If the value of i was 2, then we would have evaluated case 2. And where is the code of case 2 written? On the first index. So whatever the value of i is, I subtract 1 from it and go to that index. Now you will say, how did you handle the default? In default, the value of i can be anything. If I say more than 3, Let's say 4, 5, 6, 7, up to infinity. It can be anything. I can't do one minus every time. So you can write like this. there will be some condition inside that if the value of i is greater than 3, then we have to go to this place every time. So basically, we call this a jump table. Now, this is not an array, I have told you by representing it, but it is very similar to an array. From inside, your Java compiler makes a jump table like this. Then it knows where to jump. So, it can directly jump to that particular case without comparing the value of i with each other because it has made a table from inside which it It is called jump table So this is the internal of java How to optimize switch Now I will explain you a little more You will enjoy it Maybe someone will tell you this internal That your jump table Jump table doesn't form every time. It depends on the compiler whether it wants to make a jump table or not. Now, how does it get decided? Java will make a jump table only when it can make the switch efficient. So, jump tables are not always efficient. Why? that it is not always efficient. Before that, one more thing. Those who have an idea of time complexity would know that when we were executing each statement one by one, that we first compare this, then this, then this, and then this, like this, right? So, here our complexity is order of n, right? But it switches it to order of 1. Why? Because it directly jumps on i for that particular case. Right? From the jump table we made. Right? So if you have an idea of complexity, then you can see how optimized it is. But here I want to tell you that jump tables are not always efficient. Why? You think for yourself. Can we make jump tables in every case? No. We can make jump tables in those cases where the value of i, the value of i, What does dense mean? Let's say i is equal to 3. You have written switch i below. But let's assume that the developer has written the following cases in it. Case 1, something like that. Next one is case 1000. Then compare i with 1000. Next one is case 1 lakh. Something like that. So when the compiler reads this code and decides to make a jump table, how big will that table become? Because it will try to base on every index. It will keep some code for 1, for 2, for 3, for 1000, even for 1 lakh. Now think, you want random access, you don't want to skip anything. So let's say you want to make an array, if your index is 0, So we need to execute case 1. If index is 1, then let's say you left it null. Case 2 is null. Case 3 is null. By doing this, you will get case 1000. In that, you have to execute case 1000. You must have written something like this. Very good. Now, think carefully. The jump table that you have made, how big is it in length? It will go up to lakh. Because your biggest value is of one lakh. In this case, because our values are not dense. Dense doesn't mean near, they are very far. This is called sparse values. I will write it properly. These values are called sparse values. Your values are very far. So, in this case, your jump table will not be made. Okay? Okay. You might have understood this as well. Now, those who don't have an error guide, they might be a little confused. But, it's okay. You stay there. You might be able to get something out of it. Otherwise, when you finish erasing, we will come back to it. Okay. You might have understood this. Okay? Let's go one step further. Let's apply the first thought principle. That, brother, In this case, where the value of i is, let's say, dense, I mean, sorry, it's sparse, it's 1, it's 1000, it's 10,000, you're saying that normal jump table is not applicable, okay? So, can't any optimization be done? Some optimization can be done, absolutely. If I tell you a little more about it, then look, your jump table also, jump table, that too, Java's compiler makes it in two ways, okay? First, it makes your table switch, And A can be made as a lookup switch. Okay? Now, what is table switch and lookup switch? The same thing I told you earlier, that was a table switch. What did it do? It made a table, like a jump table. And for every case, let's say 0, 1, 2, 3, it gave a code for every case. Or it gave a code block that if there is a 0th index, then go to this code block. If there is a 1st index, then go to this code block. Like we took an example here. Okay? The example we took here, that if you switch to i, see, all are dense, 1, 2, 3, they are around. So if the case is 2, then go directly to the case 2 block. If the index is 3, then go to 3-1 block. In the same way, you can make a formula. So whichever case it is, go directly to that block. We made this and we called it table switch. This is made by Java compiler only if your IE values are all dense. But if IE values are not dense like in this second case, suppose it is 1, then 1000, then lakh, like this, if there are sparse values, then it will make lookup switch. Now lookup switch is also similar to array But it won't make such a big table that you have to put an index for every value No, it will do the same thing that binary search does It will keep putting a case after 1 Let's say if it is 0th index then case 1 If it is 1st index then it will put case 1000 If it is 2nd index then it will put case 100000 So what did he do? He consumed only 3 memory because we had only 3 cases. He didn't make an array of 100,000 spaces and put the values there and left null in the rest. He consumed only 3 values because we have only 3 cases of size. Okay, now here in the example, 3 is a very small number, but let's say there are 1000 cases here, and all of them are very far away, 1 on 1000, 1 on 10000, 1 on 50000, 1 on 10000, something very far away, so in that case, he would have used only 10 indexes, and would have covered all of them, okay, but now how will you find it? Let's say I say the value of i is 3000. Something like this. And you can understand here that there are many cases. Case 1 is case 1. Case 50 is here. Let's write it here. Let's say the value of i is 3000. And what is the case we have? Let's say one case is 1. One case is let's say 1000. One case is 3000. Then one case is let's say 10,000. Something like this. And here let's say the last one lakh. We have all these cases. Now obviously you can't directly jump the java compiler because we don't have the table switch which we had at the top. You can't directly jump it because you can't directly jump a case by looking at the i. Because the value of i is 3000 and there is no index of 3000. What can we do? We can do binary search. Those who know what binary search algorithm is, it's a very good thing. If you don't know then it's okay. You can ignore it. Binary search is in a sorted array. So you know that it is sorted according to the cases. You will go to the middle and see if the index of the case you get in the middle is small or big. According to that, you will either go to the left or right. If you know the binary search algorithm, then it is a very good thing. You will understand this thing. If you don't know, then it's okay. We will understand that algorithm. After that you will understand this. So this is called lookup switch. I hope you have understood that java tries to make two things. Either table switch or either lookup switch. When table switch will be made? When the value is dense. And when lookup switch will be made? When the value is sparse. And why does it make this? To optimize the switch case. If it puts the value of a case on every index, then you can directly jump to the particular case in order of 1. But if you know that your cases are very sparse, then you can make a table of the number of cases and put binary search in it. But your complexity will be in order of log n, which is of binary search. Here it was order of 1. But both of these are better than order of n, which is in your normal if else if ladder. Right? So, I hope you must have understood that how switch is a little efficient if else if ladder. Why? Because it has a lot of optimizations. Java's compiler optimizes. And what do we call those optimizations? We call them jump tables. Okay? Now, what else is left? Yes, one more thing is left. Which we will discuss in the last. That our switch statements, I will write this normally. Our switch statements are we can also nest them, i.e. write nestedSwitch. You know what nestedSwitch means? The second switch inside the switch. In fact, we can nest everything in Java. We will read loops in the next video. You will also understand that we can also nest loops there. So, as we wrote the second if inside the if, similarly, we can also write the second switch inside a switch. We can also do this. Let me show you an example of this. I have come back to my demo2. java. Here we had written a normal switch. And we had written some pointers. Let's remove this. It was an if else if letter. And here I will show you an example of a nested switch. So what did we do above? We played with the i variable. So let's play with j below. So let's make an int j. Let's say 5. So I can write something like this, switch j and here I will take a few examples, I will write only two cases. Let's say I took a case, one is 4 and here we did nothing, we just printed system.out.println and let's say we just printed this, j is 4. Right? I will have to put this in quotation. Now you can ignore this, how things are printed, when we put it in quotation, when not, when we read strings, then you will understand. Here I wrote break. Now let's write case 5 and write the same thing here system.out.println j is 5 and break and you know we can write a default here so I have written only two things here let's say I write here j is not 4 and 5. So I wrote something like this. Whatever is written, I switched to j's value. If 4 is there, then j is 4. If 5 is there, then j is 5. If nothing is there, then j is not 4 and 5 will be printed. Very good. Now what I said was that I can also nest the switch. So let's say j is equal to 5. So in the case of 5, I will not do this simple thing and put one more switch statement here. And let's say this time switch karu ek aur variable pe. Let's say int k equal to let's say 1. To k equal to 1 hai. To is bar me switch kiya k pe. To me kuch aisa bhi kar sakta hu. Ab yaha pe me ala ala case likh sakta hu. Ki if case 1. Aisa. Thik hai. To let's say yaha pe me kuch print kar raha hu. Let's leave this empty. And here I am writing case 2. So I can print something here as well. Something like this. I can also nest the switch. Now why will you do this? Again, it depends on the use case. Sometimes there will be such a use case that you will need multiple switches in one switch. In that case, you will do it nest. In fact, it has no level. I can use another switch in it. Let's say I switched to J again this time. No problem, I can do it. This is something I can do. Now the logic depends on what you want to do. If you want to do something that you have to nest the switch, then you can do it. If you don't want to do it, then you can avoid it. Again, good coding practice is that the more we avoid nestedness, the better it is. Why? Because your code looks less complicated. So here we have shown one more switch and one more switch. No problem, our work will continue as it is. You can also do this. In fact, I have already given you these questions in the exercise. One such question in which you will have to do the switch next. You can also see that. Rest, I hope all these things will be clear to you. We finally call all these our selection statements. And you know what it does? It gives choices to any flow of control. Normal if tells you that you can go here also. If else tells you either one way or second way. And if else if ladder gives you multiple choice and switch also gives you multiple choice. After that what we saw? What is the difference between if else if ladder and switch statement? We also told that why switch is more efficient. And here your whole selection is over. Now I will meet you in the next video. In which we will see another type of statement. Which is called iteration. Or loops also. In that you will also have a lot of fun. You just practice the questions that I have given you. You must practice them. Only then your things will be clear in your mind. Okay. So let's meet you in the next video. Till then thank you so much.
