hey what’s going on guys Thunder for Telesco learnings and in In this video tutorial we’re going to be taking a practical approach and understand the if-else control statements in JavaScript so in the previous video we saw out of control statements and we saw the different types of control statements and we took a theoretical approach so if you have missed that video you can check it out in this playlist but in this tutorial we are going to be seeing three different programs based on if-else control statements so let’s just quickly get into the topic and open up your visual studio code or any text editor that you’re using and as you can see I have already opened up mine and I also have opened up the Chrome browser and we are live so anything that I change over your is immediately visible in the output so that’s that live server plug-in that is active and as you can see on the screen these are the three different questions that we are going to be writing program for so let me just read it out quickly so the first question is find whether a number is even or odd second is fine if a number is positive negative or zero and the third one is fine if a number is positive and even so these are three different variants of offense control statements and we see the different types so let’s start off with the first one that is fine whether a number is even or odd okay so as you can see on this line I have created a variable where x equals 6 now for simplicity purposes I am NOT taking values from the user because you still have to understand what is Dom manipulations and how to take values from textbox we still haven’t reached that level so as of now I’m just hard-coding the values that are I am directly giving values in the code so I have a variable X mu storing a value of 6 over here as you can see and now we are going to do if-else control statements so these if-else control statements help us determine a particular condition so what is the condition in question number one we have to find out whether the number is even or order so this is all the syntax goes you have to write if that’s the syntax so that’s the keyword and then opening and closing round brackets and inside that you have to say X mod 2 equal to 0 then open and closing curly braces so this is that if block and I will explain to you what this condition is so what we are doing is we are checking my the modulus operation performed with the variable X is giving us a result of 0 now we know that even numbers, when divided by 2, will always give us the result of 0 right that is the remainder is going to be 0 let’s say 6 8 10 12 16 20 any the number that is even if we divide by 2 the remainder is going to be 0 now the modulus operator so this is that modulus sign always gives us the remainder so that is what we are checking so we are seeing if this variable X is taken and if you perform a modulus operation with 2 and if it results in 0 obviously it is going to be an even number 8 so here what I’m going to write is document dot right in the opening and closing round brackets the text that I am gonna write is even number so in fact as you can see immediately the output is also being shown over your let me just put it in h1 tag in fact I’ll do it in h3 and the reason why immediately we got even number as output is that yes we can see that this variable is even and that’s why this if block got executed so what if this variable was not even let me just change to 3 so there you cannot see any output now but we also have to find out for the odd sign iterate we also might need to find out whether the number is odd also so in this case simply I can say else and I’m just going to copy this entire statement I forgot to give a semicolon and Here I am going to see an odd number so now you can see we are getting the output of odd number over here if I change this value to 2 again we will get an even number so this is the if-else control statements so either the if the block is going to be executed or the else block is going to be executed depending upon what the condition is now noted that you can always exclude else block so we don’t need to have it so if you have only one condition we check for that is only if you want to check for even numbers then you can do it in a block itself right but if you are checking for multiple conditions then you can have if else if and else so that else if the part we’ll see in a minute but you cannot have an else block in without if block ok so that is not valid you can see that immediately we are getting an error okay so this was the first program now let’s actually get to the second problem Quigley and it’s just a simple modification in the first program itself so the second program is saying is we need to find out whether the number is positive negative or zero so now let’s see our variable is x equals to two as of now what we have to check in the if block we will see if X greater than zero I’m gonna say it is a positive number so these are those comparison operators we’ve talked about in the operator’s video right so these are the comparison operators which help us in performing the comparison now you also have to check for negative numbers so I’m gonna say else if again opening and closing round brackets so this is our else–if block goes I’m going to say X less than zero and inside this I’m gonna write negative so if X value is less than zero it is going to be negative let’s change the value over here I’m going to say minus one you can see immediately we got the value of negative so lastly we also have to check for zero right so this is where the last else block can be used I’m gonna say the number is neither positive nor negative so if I make the x value as0 let’s see what we get so they work out the output number is neither positive nor negative so here we had three different conditions that’s why we used three different blocks we used if block we used else if block and we also used else block so this is how you use else–if block now you can have n number of conditions and then just the else if blocks will be increased right so if you want to check for one more condition you just need to add one more else’s block so this was program number two let’s quickly jump to program number three that is find if a number is positive and even so we have two conditions but they are linked with each other with the and clause which means that we need to check the number which is positive as well as even okay and not just a positive number so let’s see how that goes so let’s say our number is five so in the if block
what I can do is I can have nested if-else blocks which means first I can check whether the number is positive so this condition six for that right so if X greater than zero which means that it is going to be a positive number but I also need to check for even number eight
so inside this if block I can see if X mod 2 equal to 0 which means that it is also an even number and I can again print even number
and inside the bigger if that is this if block this a block I can also have else block so this else the block will be for this inner if block okay so here I can see the number is positive but it is odd correct right because you can see our number is positive but it is odd so first I am checking whether the number is positive or not in this larger if since it is positive this internal block is executed otherwise it would be excluded so inside this first I am printing positive yes we have our positive number but we are checking for the next condition which is if X mod 2 equal to 0 which means is it an even number or not if not then the internal else block is going to be executed and it is going to see the number is positive but it is odd now for this outer, if I can also have an else block when I can see the number is negative so if I had minus 5 or directly we would have
the got number is negative so I hope you are getting how to use if else if and else control statements and you can see that we saw in different variants in three different versions of how you can use them now one last variation that you could have done in the last program that is question number 3 is instead of using nested if-else we can have both conditions in the first step itself so let me just show you how that works so this is where we can use the logical operators and the logical operator to be used over here is and because we need to find if the number is positive and even right so first condition is going to check for X greater than 0 we can combine and Clause over your or and
operator this is a logical operator which we have seen in the operators video soI’m going to say and X mod 2 equal equal to 0 so this is that condition for even or odd and this is the condition for positive right so now I can see even and positive number so in one single if statement I am just checking for both the conditions so if I had the number of six or eight you can see even and positive number one printed because it is even also and it is also positive so both these conditions were true and that’s why this a block got executed so ultimately what this if block is looking for is a boolean value okay so if the condition or the statements that we write inside this if parentheses that is round brackets is true only then these brackets that is the if block is going to be executed and if this is false then this entire block would be ignored it will not be instituted so if I add anything else so if I say seven you can see we are not getting this output so this entire block is not executed because this entire statement becomes false okay so this was just a quick practical approach on if else if and else control statements and three different variations of the fence control statement program all theseprograms I will link in the description you can find the code and also some theory related to effects control statements so go check out the description as well so that’s it for this video guys in the next video we will cover some other control statements and we’ll see a practical approach for the switch case control statement so thanks for watching cy is in the next video peace.