Let's suppose you have 10 conditions right you have 10 multiple paths of action okay so at that time instead of using else if you should rather go for switch case okay we'll cover that thing as well so now let's talk about the example of how to define this thing, okay this can be also nested right so there's an if the condition that if that condition is true again there's if condition then we perform the else part of it so this is called nested if okay nested if-else okay there can be if without where else right but vice versa is not true what do you mean by this thing if 2 is equal to 2 console.log they are equal there cannot be such case that's else is only i mean to say there is only else part right it will throw an error that missing if statement right so something like that it will throw you an error so but it can be used without else.
Now let's see the example so if you see we have defined here variable two variables right with the help of our keyword an equal to five and b equal to six now what I'm doing here if a plus b is less than 11 right it is not less than 11 it is exactly 11. so console.log less than 11. else if it's greater than then console log more than 11 right but both these conditions are not true then it will automatically go to the else part right if there would have been any other condition that then we will have we would have checked that thing for example if there would have been another condition let's suppose a plus b equal to double equal to 11 then that thing will have been executed the else part would have to be executed now lets in this example we don't have the another else if statement so else part will be executed so that's why we get the output as equal.
This is how you define an if-else statement we talked about multiple else if statements right uh we talked about nested if we talked about how that it can be alone right there there is not specifically you have to define the else part always so if can be alone but vice versa is not true else cannot be alone okay now i told you when you have multiple conditions or multiple actions that you need to be performed right at that time it's better to use which case so it's an elegant way to replace multiple else if statements right so what is the syntax of this you basically write switch then you write the expression then the based on this expression you will execute one of these cases so after a case is executed right this val will represent some value, for example, uh if a is equal to one let's suppose the expression is an expression came as a right so if case one let's suppose case one is truly a is one right.
Case first will be executed then after it's executed you need to explicitly write the break statement okay so uh if you don't write a break statement then all the case uh cases after that will be executed unless and until there is a break okay and by default you have to explicitly define a default case right if none of these conditions match then the default case will be executed it's just like the else part of the if else statement right so here depending upon the answer evaluated by the condition case code gets executed simple right every case must be followed by a break unless it is required not to as per logic okay if your logic allows you to break only then you will be like right so if your logic doesn't allow to break then it's not explicitly that you have to define it always so you can if you want multiple cases to be executed then you should then you should not use break statement okay otherwise all the cases will start executing from the matched case till there is either a break encounter or all the cases are exhausted.
If you have forgotten sometimes maybe you have forgotten all the break statements then all these cases will be executed after the after that when all the cases are executed you will get out of the switch case okay default is optional right the default is optional you don't have to explicitly write always right you don't have to explicitly if you sometime forgot to write the default statement it allows it okay if there is no error thrown at that time okay and holds the code which should be executed if none of the none of the cases will match okay if none of the case is matched then at that time that default case will be executed now this value or the what is the value that case val case value we saw right it can be an integer it can be a character or it can be a string okay right so this is all about switch case now let's see the example okay so right here and we have defined where day is equal into sunday so day is a string right so switch day now in day what will be present it will be Sunday.
Once it will check case month is the first case and the second k whatever the value of the day is does it match with the case one no does it matter which tweezes no red no thus no fry no set, no then Sunday comes right okay son will be executed today Sunday after that we will just write the break statement and that's it the output will be today's Sunday okay now before we move ahead into loops let's try to now let's see we'll define a variable right we saw how to define a variable right can i put this as a variable no 90 cannot be a variable right because it's starting from a digit so let's suppose let's define some variable let's name it to name okay so name equal to one let's name it integer let's put some number in it so an equal to let's suppose one and let's define another variable you can define it in the same line right or initialize it in the same line but I'm using two different lines for it.
Now let's say if a plus b is double equal to what let's do two-three f's okay is greater than two is it greater than 2 yes it is let's suppose make it 4 okay if it's greater than 2 then write console dot log what print a plus bright this will be printed now if that is not the case then let's suppose we will go into the LZ part as if a plus b is less than 1 that's not also the case right it's greater than 1 then print console dot log what a plus b again okay else and then we go into the else part so both these statements will not be executed right so we go into the right into else part what we will do will print a plus b okay now i haven't written any curly braces on the last part right the else part because if there is only one statement in the if-else condition right if there is only one in one single statement associated with the if-then these braces or these curly braces are optional.
Now let's save this fight and when we go into a browser okay let's refresh it and then press ctrl shift i okay i pressed n just a minute let me ctrl shift i then the console will be print uh console will be open and you see what i printed uh what i got the output as three now let's make it a little bit let's make a little bit change okay let's open it again and let's try to print hi here okay so that we know this statement was executed okay okay now let's refresh it okay hi is not defined okay okay okay yeah it thought that it is a variable right high is a variable because we try to print uh we have to write the braces.

Post a Comment
If you require any additional clarification, please contact me.