JAVA SCRIPT BASICS
Java script Switch Statement.
(00:00)
Hello guys, welcome to our series Java script. In our last segment we saw if statements, including if…else and if…else…if, we covered all three. So I hope all the practical which we performed in last segment, three practical for three statements, I hope you have practiced it all. If you have any question and queries regarding our last segment or practical you can tell us on forums.learnvern.com we will revert back with solutions.
Let’s move ahead in our today’s segment, today we will see how to use switch statement Java script, if you have already learned C, Python or Java then you might have rough idea about switch statement. We are going to study switch statement from basic in our today’s segment, like how to work switch statements in Java script? Types of syntax of switch statement in Java script. And we are also going to study the difference between if else statement and switch statement.
Let’s start today’s segment, firstly, the Java script switch statement is used to execute one code from multiple expressions. Means we have multiple expressions available, to execute only one expression from multiple expressions we use switch statement. We have seen third statement from if statements, i.e. if…else…if ,switch statement can be considered as updated version of that only, in if…else…if we have to add multiple conditions and every time that condition gets checked, in switch statement we give condition only once and according to that our relevant segment gets executed.
How is switch statement better that if…else…if? Condition checks are less in switch statements, it happens only once and our output gets executed on basis of one condition only. Line of execution is better in switch statements which enhances its performance. Here it is written also, it is convenient that if…else…if because it can be used with numbers and characters etc., in if…else…if we check each and every condition, instead of that in switch we can do this by checking only character, number or Boolean.
How is switch executed? So here syntax is given for that, it is a very simple syntax, means switch and after that as it was in case of if it is same, switch after that we have to pass expression, in expression we can pass variables, whatever values are stored, true or false, or any number we can pass, in if we saw if we pass x then also our execution takes place in a we wrote 1 or 2 or 10 still our expression was true and execution was taking place.
Similarly, in switch also expression is there, but here execution is performed on the basis of actual value. In if what was the case? If we have written x=10 and then if we write if x then in bracket it is 10 but 10 is actually considered as true value, means our block was getting executed on the basis of value of x. But in switch it is not the case if value of x is 10 then whatever case we have taken in relation to 10 that will be executed here. So this is the difference in if and switch, if we pass value in if then it is necessary for us to pass condition, if we pass any value in switch the on basis of that value our related case gets executed.
So here you can see in switch, case value 1 and then break, we will see later what is break, case value 2 and again execute its portion and then break it. Break means, let’s say case value 1 has been executed then break the execution from there so that it will directly come outside, means it will come directly out of switch statement, if this is not the condition the execute case 2 then break it and come outside.
Writing break after every case is mandatory because we don’t want another line to be executed once our code has been executed, for that purpose we add break keyword. In switch statement last case is default, in if…else…if we saw in last statement we wrote only else we did not add any if in last, the reason behind that was if none of the above conditions are fulfilled then we should have such else statement which will inform our user that either you have entered wrong input or the above given condition has not been fulfilled.
(05:25)
Similarly, in switch what facility do we have? We have a default keyword, means whatever is written I our default that will be executed, when? Only when the above mentioned cases are not executed the only. So this is our switch statement. In switch we mostly have cases we have break, so that we can break and come outside and we have default so that if no case matches then we can separately execute the default portion.
So here also we have explained break and default keyword, this if for break, when Java script reaches a break keyword, it breaks out of the switch block. Means as soon as we reach break our execution will stop and we will come outside. So immediately our execution stops, another thing is given here, it is not necessary to break the last case in a switch block. The block breaks (ends) there anyway.
Means we don’t need to write break in our last case, our code has already been executed, our switch statement has already been executed. As you can see here default is written, if we do not add default and last case is suppose case 10 but even after that we don’t need to write break, we can come out by default.
Lastly, the default keyword specifies the code to run if there is no case match. Means if none of the case matches then you can execute default portion. So these were our break and default keyword. Here we have taken an example, here we are checking grade, based on the grade of student we are printing the statement. Let’s say I will open my sublime text first, html, from here we will copy plain text , example switch statement, save(10 seconds gap), here we will write script and inside script what will we write?
In script we have to check grade, we can also check based on the condition marks are less than 35 or greater than 35, we will perform that program here, so let’s say variable marks equal to, right now we will leave it as it is as of now, document.write, in h3, example of switch statement and from here(4 seconds gap) here you can see our program running here.
Here I have to use switch example, I have taken variable marks so here I will write switch in small letters here bracket in parenthesis I have to pass my variable value so here which variable will I pass? Marks, as in if we used to place condition, in case of switch we don’t have to place any condition, it gets executed on its own. So here we will write grade, okay so here will be case, now according to our grade’s value, now If grade is ‘A’, for that what will we execute, document.write “the student has secured” and from here we can pass whatever our grade is, grade means our variable itself.
Next comes if our case is A, here writing A will be more feasible, or else it will be like if case is A then this portion will be executed but what if this is not the case still our case will be executed by taking value from here, so right now we will finalize giving rigid output, we will see another program which will be dynamic compared to this one. So this is our case A and after that break statement, then for case B (3 seconds gap) document.write B and here break statement, now we have two case either our student has secured A grade or our student has secured B grade, if condition is not from above mentioned statements then in that case we will write default, in default you can see we can go back to slide here you can see in default after colon we have to write simply result equal to No Grade, so here I will write default, (4 seconds gap) okay so student has not secured any grade.
Here after last statement we don’t have to put any break statement it will get executed on its own. So I will and this an reload, here you can see I have not passes any value in grade that’s why my default portion is getting executed, the student has not secured any grade. But let’s say I pass A in grade okay my value has changed to student has secured A.
So what has changed here? As soon as I write A my case A gets executed. So based on this, my this much portion has been executed, student has secured A , then my execution reaches break, as soon as it reaches break my case B and default is skipped and the execution directly comes outside switch.
(12:01)
So this was our switch in which, in if…else…if you can see if we have to write this much portion then we have to write 3 conditions if it is grade A then condition for A and if it is grade B then condition for B and if it is nothing then else condition. So instead of checking 3 conditions we simply came to switch grade. Here we have not written grade equal to equal to A you can see whatever the value of grade was it came directly to the case related. In switch what you have to keep in mind is if you know which values are going to pass then only you can write case. Like here I know grade can be A so that’s why I have written A here.
Let’s say I write small a here, and now I reload it now what is my output? The student has not secured any grade, because value of capital A and small a is different and that’s why it is mandatory for me to write capital A, I won’t be able to execute it with small a. so here capital A and here also capital A. if I write some other value, so in all the cases apart from A and B in every case only default will be passed.
So this is our program, here we have one more program, which we checked in if…else…if. Our ‘day’ program, like print what day is it today? So here we will generate same program with help of switch, so write a Java script program to display today’s ‘Day’. So here we have some in built program in Java script’s system related to date, from which one is getDay, we also have one method called as Date, so from class Date’s getDay method’ help we will take day and on basis of that we will print output.
So here you can see getDay returns us what? It returns us value from 0 to 6and on basis of that we will write this program. Example switch statement for day, save getDay, (4 seconds gap)okay, now what will we write in this? We have switch statement, so switch in switch what will we write? We will directly use our method so for using this method, when we saw operators we saw NEW keyword, NEW keyword is for generating object of any class and to take its value. So currently you can see here in slide we want to use our class date and its method getDay, now how will we get that? As this class already exists so we have create its object and get its day its data, so for that we will use NEW keyword.
It will be like new Date dot getDay , so this is our function, we have written exactly same so we will write here in switch, let’s say we have to check what does date return us? We will print the same thing here temporarily, after br here we will copy newDate as it is pass it here and we will comment it here and save reload, so you can see here value is getting passed in number.
Means our class date, i.e. class date’s getDay method, passes value as number of the day as it is Thursday today when I am explaining you this program, so that’s why on the basis of Thursday we are getting number 4 as value, how is the calculation done? Monday, Tuesday, Wednesday, Thursday, means 1 2 3 4, in this way calculation is done. If I use getDate instead of getDay, then you can see it is showing 27 so we have done this for checking purpose, with getDay we are retrieving number 4 , now what I will do? I don’t need this so I will remove it we will come back to switch, switch new Date then getDay, so we got day because of getDay , so we saw we will get output like 1 2 3 4 5 6 etc.
Now this output we are taking from system not from user, so if we are taking something from system so definitely we will not get value above 7 and value less than 1, that’s why what we will do? First we will write case, we are getting 1 2 3 in output, so we will write 1 give colon and document.write it’s Monday and then we will break it. now we have to repeat this seven times, okay so here 2, 3, 4, 5, 6, 7 and Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday. Now here you can see in last statement we don’t usually need break but as I said we are taking data from system so in every case it will give output from 1 to 7 only, but still if we have any doubt or confusion that some other data or input might come so for that we can write default statement as it is.
(18:44)
Default statement would be like this, incorrect input, save. So this is done, we have taken case from 1 to 7 and default. Here I have not passed any variable manually, so let’s see what will be the output, the output is, its Thursday, because it is Thursday today when I am recording this. So because it is Thursday today so output is Thursday, if I run this program tomorrow then I will get Friday as my output, if I have recorded this day before yesterday then my value would have been Wednesday. So this was our switch case.
In this program we don’t need default, but I have still added it so that in vase input is wrong so we will be able to handle it, our cases are executing on the basis of our given condition. Guys you saw the bracket after switch or the conditional expression syntax portion, there we did not add any external thing, for example we have not added any variable, or like in previous program I added grade before passing it, so I have done no such thing here, I have simply created a object and passed it.
So in switch you can see in switch we have an facility also that we don’t have to write comparison, by default comparison will be done on the basis of cases which we have written and output will be generated. So this was our switch, we saw this with help of two examples. Guys after this there is one more thing in switch which we need to understand i.e. strict comparison, means the comparison is done on strict basis.
When we saw operators we saw two types of operator, double equals and triple equals. How does double equal works? It only checks the value, but in switch, our default portion which we saw here works on only triple equals, it does not work on double equals. Means if I have written A in grade, if I have passed value A, then here in string A will be passed, let’s say here I am passing A string and here I will write A and now I will execute it and now I will reload, so you can see the output has gone , we will press F12, uncaught reference error: A is not defined.
Means here A is not defined, means if I am passing here string A then here also I need string output compulsory. So this was our strict comparison in switch. Whatever comparison is done in switch expression it will be on triple equals only and it will always be strict. Means value will also be checked and type will also be checked. Here an example is given on strict comparison, we have taken a variable x and in that 0 is passed, if it is 0 then off and if it is 1 then on, it will work on basis of that.
So let’s say I generate same program here, we will take variable x and I will pass 0 in string, which is a number and here 1 so document.write off and here document.write on, of and on, so here will be x, document.write, incorrect input we will save this under name on off. This my on off program so, so here you can see output Incorrect Input , I have take value of x in string, and case I have written in numbers so none of my case is getting fulfilled my conditions are not getting fulfilled.
If I write this 0 then it will be off and if I write 1 then it will be on. But if I write in string then output will be incorrect input, means our check has been performed, this means in strict expression checks strict comparison, triple equals i.e. value with type will be checked. so this was our switch statement, we can use this instead of if…else…if, switch is quite useful while performing multiple executions, we have to perform execution on basis of multiple comparisons, if we have to execute a piece of code then in place of if…else…if our switch is more useful.
Guys I request, today we saw three different programs one was with date with day and one with simple grade and last one was on off which was for strict checking, I request you to practice all three programs, If you have any question and queries regarding our last segment or practical you can tell us on forums.learnvern.com we will revert back with solutions.
Our next segment is on how loops work in Java script.
Thank you.
(video duration 24 minutes 31 seconds)
Share a personalized message with your friends.