#12 Conditional Statements If Else in JavaScript

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome back aliens my name is davin rady and let's continue with the series on javascript and in this video we'll talk about conditional statement now see if we talk about the world of programming if we talk about the world of computer everything is based on data and the decisions we take based on that data right so when you say you want to take a decision it's simply based on true or false and we have talked about it right we have talked about the boolean values the true and false and when you compare two values it results in a boolean value okay so that makes sense right but then how can we use that boolean value to take some decisions example if we talk about a flow chart if you talk about the flow of execution it is based on the conditions example if this condition is satisfied i will go on this way if this is false i will go other way round so this is possible with the help of conditional statement so let's get started now whenever you talk about this concept uh conditional statement this is possible with the help of a concept called if else okay okay so there are other varieties as well we'll talk about those things later but as of now let's focus on if and else now how can we focus on that it's very simple let's take an example and doesn't matter which language you learn if you want to learn if else we normally go with simple examples see there is one reason why i'm sticking to very simple examples because we are not here to learn complicated examples we are here to learn a language right so it is easier to learn a language with easier examples so let's get started so what we're going to do is we want to find a given number is even or odd okay that's the thing we do or maybe we can go with even simpler so let's compare two values and we'll print the greater one just to keep it very simple so what i will do is let me take two variables here so i will say num1 is equal to and the value of num1 is let's say 6 and we got num2 and the value of num2 is 4. so we got two variables here i want to compare this two values and print which is the greatest okay so in this case i know it is num1 but let's do that programmatically so what we will do here is how will you compare them it's very simple right we have done that before so we simply say num1 is greater than num2 okay and we can save this in a result so we can say let result is equal to num1 is greater than num2 if this is true of course it will give you true because 6 is greater than 4 here in this case of course 6 is always greater than 4 but in this case a number is greater than num2 so that result is getting stored in a result variable and then based on what's the value of a result if it is true i will point num1 is greater okay it's very simple so how do we do that so we have to use a statement called if now if is a statement basically you have to pass a parameter which is result so if result is true that's how it works okay it works with true or false so it will check if result is true then execute a statement okay so i want to write a statement inside if condition so let's keep a space let's give a tab a proper indentation just to show that whatever i'm mentioning now is a part of if block not a compulsion but a convention good to follow okay so if it is true i will print uh num1 is greater simple stuff right otherwise it will not print anything and at the end i will print by okay again a semicolon is not compulsory but it's a good habit to have semicolon so what we'll do from now is let's get this habit of putting semicolon now if you watch the earlier videos there was an intention of not putting semicolon just to show you there is no need to put semicolons in javascript so it's optional now if you're coming from other language that says c c plus java it's compulsion to have a semicolon javascript says it's your choice so from now let's put semicolon it's a good practice okay and at the end we'll print by that's it okay and now let's run this code and see what happens so if you're on this code we can see we got num1 is greater and we also could buy okay so how this is working so basically you got initial two values it is checking for the results so num1 is greater than num2 yes result is true so of course remember it will give you a value which is boolean which is true or false and then we are checking if it is true execute this statement okay now let's do some twist let's say this is 3 now we are doing is 3 is less than 4. so 3 is less than 4 now we are comparing the first value and the second value of course this will be false because 3 is not greater than 4 so result would be false so if false so what happens is if the value here is false it will not get executed so the block inside if will not get executed okay so let's see what the output is so if you can see the output is simply bi now why we got by it's because it is outside if condition this is outside the if block so that's very important simple right so the if block so this is the block here if this will get executed only when if this is true okay in fact what you can also do is you can write okay we'll do that later uh what if i want to print num2 is greater than num2 is greater in this case num2 is not greater right or it is it is greater so i want to print num2 is greater in that case we can also apply if condition and we can check the result so in this case we can again apply a if condition but that doesn't make sense right why to check two times we know if no one is not greater it is not two so in that case we can say else so else is on another block here and here we are printing the num2 is greater simple and let's see if this works let's run this code and you can see we got num2 is greater see it doesn't matter what's the result the bar will get printed because it is outside the condition so these two statements are depend on the condition right so if it is true it will print the first block if it is false it will execute the second block so else will get executed only when the result is false okay make sense right okay this is cool uh but can we write this condition inside see ultimately if only checks for true and false right so why to create a new variable and why to do those things we can simply cut this part and put it here so we can actually perform the operation or we can evaluate the expression inside the condition itself that's what we're doing here so we are solving this expression here which will give you true or false if it is true it will execute the if block that is false it will execute the else block that makes sense let's go forward now what if i have two statements here example here i will say i want to print yey okay so the moment you get num 2 is good i also want to print yeah i know that doesn't make any sense but let's say you have a thing where you want to execute multiple statements in the if block or else block will it work let's try now if you can see i am following indentation here so what is indentation is giving a space or a tab after in every block okay it makes your code look good okay so here what i will do is i will rest on this code and you can see we got num2 is greater yay and by does that make sense right because 4 is greater but what if i will just make this 6. now in this case 6 is greater so what it should print it should print num1 is greater that's it it should not print yeah it should not print num2 is better because that's a part of else block let's see if this behaves the same way we are expecting so if you run this code oh that's weird if you got someone is greater that makes sense but why we got yay a should be a part of the else block right now what is happening is by default every condition let's say if or else every block will have only one statement oh that's tricky that means else will also have this one statement the a part belongs to the the outer block not the else block how do we put that into else block so we have to mention hey both the statements belongs to else block in that case you can use curly packets open and curly package close so now what we are saying is this bunch of statements here belongs to else block the same thing can be done for the if condition and so that's the if block so this is the if block and this is the else block so the curly brackets is optional when you only have one statement so if you have multiple statement that's why you have to use uh the curly brackets now let's try and you can see we got now it's getter we don't have a because that's a part of else block make sense cool so that's how we can use if else conditions now let me add a twist here the twist is now instead of having two values let's say we have three values we have done that before okay but let's do that so we'll say num3 as well so we got num3 is equal to let's say 7 and i want to check which of this or i want to find the greatest number of these three values so when you want to compare these three values what we will do here is we'll compare the first two values here which is num1 and num2 and then we have to also compare the num1 with num3 right that's where we will know that num1 is a greatest number so basically here in this if condition we will not just compare num1 and num2 let's also compare num1 and num3 but the thing is when you have two conditions and we want both to be true which logical operator we are going to use so pause this video in the comment section let me know which operator do you think is logical or logical operator which is logical here okay so i hope you answered let's go with the and because we want both this expression to be true so we'll say num1 is greater than num3 so we have to compare both if that is the case will print num1 is greater that makes sense right okay but what a for the second condition so let's say num1 is not greater what next then we have to compare num2 if num2 is greater but then do we okay how do we compare that so in that case we can use something called else if okay so when you again want to compare after the first if condition you have to use else if and this is where you will put the condition so what condition you have to put you have to say num to is greater than num1 and num2 is greater than num 3 cool now once you check that so now if after this of course we know that num 2 is greater so it will print num2 is greater i don't want to print e anymore it was just for the experiment and once we do that what about num 3 because what if num 2 is greater so in that case you have to put else block here where you will mention log num3 is greatest in fact it should be greatest right we are comparing multiple values okay so that's how it works right so first we are comparing if number is greatest second then we are comparing if num 2 is greatest see if now we know that num1 and num2 both are not greatest of course it is num 3 and that's why we are printing num 3's getting this here let's try if this works let's run this and you can see num 3 is getters that is right what if i make num 2 as 9 in this case num 2 is greatest what if i make num 1 as 10 now is greatest so this works uh there is one more thing i want to mention is when you have so see in the first if we are checking if num one is greatest right if this is false that simply means num1 is not greatest then the only competition remains between num2 and num3 then why to even compare num1 and num2 again so the first condition is actually not required we can directly compare num203 and then in the s block we know it is num3 so that's how we can use if else if else so first we talked about if then we talked about if else and then we talked about if else if else so i hope that makes sense uh if you have any more questions let me know in the comment section and let me give you an assignment the assignment is you have to use if else of course but then for the given value you have to find if the number is even or odd try that and answer that in the comment section we'll see you in the next video bye [Music]
Info
Channel: Telusko
Views: 12,128
Rating: 4.971292 out of 5
Keywords: telusko, navin, reddy, tutorial, java, python, blockchain, django
Id: o_iO9WuoWaM
Channel Id: undefined
Length: 12min 16sec (736 seconds)
Published: Tue May 25 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.