Learn Javascript Programming #3: Conditionals & Control Flow (If Statements, Logical Operators)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys this is tenderness whatsapp calm and welcome back to another video in this video we're going to be talking about if statements and logical operators and if you smash that like button if you smash that like button we're going to be best buddies for real like for real okay so when I woke up today I had no intention of making a video whatsoever and I had just three hours of sleep and I knew the day was super busy and when I came home I'd be going to sleep right away okay knock out but in the middle of the day is right he goes what's the next video coming man that comment made my day today just the fact that somebody is waiting for my video for my content is very flattering and and it's amazing it's insane and it's amazing okay so if you guys are waiting for something if you guys have any questions if you guys have if you guys are waiting for something and you you want me to produce some video please tell me it's like a juice for me it's like a fuel ok fuel units you got to have fuel units you must have enough fuel units so without further ado here's the video okay so if stadiums in programming are used to check conditions or a test right so if if some condition or a test is true then perform a specific action and if it's not true then do something else right so before we go into the actual code I want to go over some real-world examples first okay so for example if it's raining outside take an umbrella if it's not raining outside don't take an umbrella right if your car is low on gas or the gas station and fill it up if it's close to running low on gas then think about it whether you should keep driving for a little bit or just stop right now and get some gas or if you're not anywhere close to getting low on gas then don't worry about it keep driving forever okay another example is if the weather is over like 80 degrees you want to go to the beach if it's not then you probably don't want to okay because the waters can be too cold all right so I think we went over enough example so let's write them code now so in this video we're going to cover if statements right so I'm going to write a comment here if statements right water if statement and we're also going to learn about what our equality operators and what our Simon operators and logical operators okay so I'm going to walk you guys to some examples right so and then I'm going to start off with some basic example and then level it up from there on okay so for the first example let me just write them comment here for so a long comment here and let's do this okay and in this one I'm just going to say first example okay so I'm going to create a variable here called enum and make that equal to something so in just 20 okay and now how do you write an if statement is with the if keyword and then you have parenthesis and in here you write the condition so my first example I'm going to write a program or an if statement that checks if my number is a positive or a negative okay so let's do if num is greater than zero then print out control dot log the number or the number is a positive okay so let's see if this is if this works out so what we have is we have a number defined and assigned it a value of 20 and then we're saying we're checking if num is greater than zero then run this console dot log okay so let's see if it works notice the number is a positive because num is greater than zero so what's happening is what you need to pay attention to is this parens here whatever is in that parent have to evaluate to true for this code to run for the code inside this braces to run of course we can have multiple lines here say console dot log this is just another line for example right and it's running because this is true this part right there is true okay I'm just great of it now and also we can do is num is less than zero console dot log the number is a negative okay so now right now if I run it again is just going to say that number is a positive because our num is a positive number right twenty and that's clearly greater than zero right but now we have another if statement here and we're checking if the number is less than zero if it's a negative right and our current number right now is twenty but let's say what it what happens if we change this twenty two negative twenty or negative four okay so now it's going to go through here and it's going to go okay this is not true so this is false so this is not going to run and it's just going to skip this altogether and then it's going to go to line fourteen and then check is is num less than zero well numb is negative four and it is less than zero so it's going to log out the number is a negative okay so let's run it and there it is the number is a negative right so this is how you do like two if statements right so what you can do is you can actually write the same thing in a much cleaner way okay so what I'll do is I'll just leave that alone but I'll comment it out okay but I'll just copy this whoops let me copy it first and then comment it out and I'll just write it here so instead of writing Q if statements like this what you can do is actually not have a second condition at all okay so delete it and combine and say else so now what you're doing is essentially you're saying if number is greater than zero console.log the number is a positive else else if anything else just say the number is a negative so now if I run it negative 4 is a negative right if I gave it positive 4 or 6 positive 6 it's going to say the number is a positive right what is 0 what happens then the number is a negative why is that true not really right zero is neither a positive or a negative number right zero is neutral right so what we can actually do is combine another if statements in that else block okay so what you can do is say let me move that down and where you can do this else if so now you're adding one more if in there and you're saying if and we can say num is less than zero we can do console dot log the number is a negative okay and now since we already have this here we want in the in this part we want the number is a zero okay so what we're saying is if the number is greater than zero then counsel well mark this part if the number is less than zero console dot log this part else do this okay so now since number is zero it's going to go here and it's going to check it if number is greater than zero which is not right and then it goes here nine twenty and that's not true either so it's not going to run this and then it goes to else because none of these are true none of these are so then it goes to out if anything else just log it out so then it goes number is a zero okay again if I change it to five it's going to be positive if I change it to negative five it's going to be negative okay so this is how it statements work okay so let's go on to another example but before we do that I'm going to make this a little bit more interactive okay so right now we have console dot log statements and we are hard coding our number in like this right like we're literally just deleting and then like saying 78 or writing it ourselves what we want to be able to do is take that in a more interactive dynamic way okay so let's go over that console dot log first okay so instead of console dot log I'm actually going to use another tool another function but we're going to go over function later in a later video but for now just know that there's a tool called alert and you can use that like this okay so now instead of logging it here what this is going to do is it's going to give you a pop-up and it's going to say the number is a positive okay so let's see how that look so first let me do all this convert all of this to alert so now we have numbers three so I should say the number is a positive so I'm going to run it and there it is the number is a positive what if I give it negative three and the number is a negative right what if I gave it zero [Music] the number is a zero right so that's pretty cool right that's a lot better than console out mark later on when you when you write a bigger program than your console dot log is really good for finding errors and stuff but for now just a fun we're going to use alert because it's more interactive right and also instead of hard-coding numbers in like this we have another tool in JavaScript built-in and which prompt okay and I'll show you what that does in a second so let's say prompt and I'm just going to type in a message here and say it has to be wrapped in double quotes write a string and I'm just going to say entered a number and now if I run it let's see what happens so I'm going to run it and it goes enter a number so now I'm going to enter some number okay so I'm just going to say 56 so let's see what it does the number is a positive I'm going to run it again I'm going to say negative 56 the number is the negative right and then I'm going to now say zero and see what happens the number is a zero so what's happening is this prompt basically gives you a pop-up but instead of just giving you a pop up with some text it gives you an input that you can type in okay and then when you type something in it basically takes it in and assign the value of whatever you type in to num and then it goes down here and it goes it checks the if-else it goes to the if-else if-else and checks if the numbers greater than 0 or less than 0 or 8 0 okay and then it gives you the appropriate alert corresponding alert so with this now our simple if-else became a lot more interactive right so let's move on to another function another example okay so let's see here let's do VAR let's say is raining and I'm going to just say equals do let's see hmm want is it raining okay so here what it does is it basically prompts you is it raining and whatever value you provide in that input field it's going to assign that value to is rating okay so just to see if it works we're just going to say console dot log is raining okay and what I'm going to do is I'm just going to comment all this out this first example okay so let's do this and here I'm just going to run it and now it's saying is it raining and I'm just going to say yes I'm gonna run it again is it raining no it's raining mango okay so but we shouldn't do that okay so we'll just pick yes and since this works I'm just going to delete this combat log okay and here I'm just going to say if it's raining is equal to yes right I'm going to say console dot log or alert go take an umbrella okay so if it's raining we say that and else if it's something other than yes then we say alert it's okay you don't need anything okay so what's happening here is we're prompting the user for a value and then we pass in the value to is raining and then we check if it's raining then if it's yes then we say go take an umbrella if it's something else other than yes then we just do it's okay you don't need anything because it's not raining okay so if I don't run this right now it's actually going to give you an error or not do what we want it to do okay so if I do this it's just going to do is it raining and if I say no it should say it's okay you don't need anything right because it's not raining so now I do this is it still says go take an umbrella right so why why does it say that and that's because of this equal sign right here okay so whenever you're comparing two things you can't use one equal sign okay what you need to do is you have you have to use two equal signs so here you instead do two equal signs okay and in JavaScript there are actually two equality operators and this is the one I was talking about earlier right the equality operators so in JavaScript there are actually two equality operators the three equal signs and then the three equal signs okay so in our case we're just going to use the three equal signs because it's just a lot more safer and you should always use the three equal sign especially in the beginning okay we'll go over what the differences are in a later video when we go over like more complex logic but for now just know that three equal signs will do the job okay so now this should work okay so now if I run this it goes is it raining baidu yes it just says go take an umbrella right now let me run again is it raining no it's okay you don't need anything right so that works and you can also check if something it's not equal to something else by just changing this first character of the three equal signs to an exclamation point so now instead of saying if it's rating is equal to yes you're saying if it's raining is not equal to yes okay so in this case it doesn't make sense so I'm just going to leave it as it is and I'm just going to comment it out and showing it showing an example here okay and if I just say two is equals equals equals two while this is completely true so it's just going to say true okay but if I change the first hair clip to an exclamation point now I'm saying two is not equal to two which is completely false so this will log out false okay and one more thing let me just comment this out so this three equal signs or two equal signs is called an equality operator okay or a comparison operator so you can call anything you want to like let me write here two equal signs for three equal signs are for comparison or equality checking okay one equal sign is for assigning values oops can't go now okay to a variable usually almost all times okay so one equal sign is used for assigning a value to a variable right but when you have two equal signs or three equal signs they're used to check if something is equal to like if a side is equal to B side okay so that's how you check equality with the three equal signs or two depending on what you're trying to do but in most cases you would use again you just use the three equal size okay okay so now let's move forward and now we're actually going to talk about logical operators okay so what are logical operators so we have three logical operators in most languages okay in JavaScript the same way we have and or and then not okay this this stands for and this is or and this is not and let me comment it out let's just say and and I'm just going to put or the parentheses here and then for this one I'll say not okay so this is used to combine logics okay so combine conditions so so far in our if statements we've just been able to like test or check one condition right like if it's raining is equal to yes right just just one but what we want to be able to do is it is raining is equal to yes and some other condition and some other conditions or some other condition right so that's what logical operators help us with okay and there are some rules that you have to follow when it comes to logical operators so I'm just going to put it down for now and we'll go over what that means okay so let's just write here I'm just going to start a comment and then end a comment right there so two and two equals two okay two and false is false two for false and true is false again and let me just write it here because that's okay and we have false and false is false and then for or we have to or 2 is to 2 or false is to again false or true is true and then we have false or false which is false okay and for not it just changes it into an opposite so not true becomes false and then not false becomes true okay so this is these are the rules for logical operators okay let's so what does this actually mean right this doesn't make any sense right now but okay let's take a real-life scenario and let's say we have a we have a person here and I'm just going to call her our name is Victoria I've used Victoria before so let's do Lexi okay and let's say Lexi is 17 years old okay and far she has her high school degree equals to true okay so now what we're saying is we have someone named Alexi she is like 17 and she already graduated high school because she graduated early okay so now let's say she's looking for a job and let's say this is job at Apple okay and let's say the job requirements are let's say job requirements are over 18 and high school degree okay so now if I have to write an if statement for this I can do if right if H is greater than 18 or we can say greater or equal to 18 right and we can do high school is equal to or just use two equal signs true in that case we will alert you are applicable to apply for this job okay so now over saying is if the age is over 18 18 or over and high school is equal to true someone have she has a high school right in that case then that person whoever that person is can actually apply for this job okay so now if I run this right now it's not going to run because Lexi is 17 she's not 18 or over so what this end operator does is it combines two logic right and for this alert to run both of these conditions have to be true both of these tests have to evaluate to true okay so this has to be true and then this has to be true and then only in that case this whole thing becomes true okay and we can verify that by doing two and two is true right because if this was true and if this was true then this whole thing becomes true as well and then this gets run but right now this is false and this is true and false and true is false right right there so this is why these rules matter okay in the beginning you might have to this might be a little confusing you might have to look at it again and like have it by you or you know go back to it and look at it but really later on it just becomes intuitive like right now I just look at it and go okay if age is over 18 or over and high-schools equals true then holy then you're applicable right both of these guys have matched so now if I change this to let's say 18 and let's say Lexi after a year Lexi became 18 and now she goes to Apple again and tries to apply now she's applicable right because she is 18 or over and she has a high school degree so now if I run it this will say you are applicable to apply for this job because both of these guys match right so this is Apple right but now let's say we have another whoops let's do C and comment it out and let's say I have another company here and let's say and let me just do this this is for Amazon okay it was just that easy to get jobs at these companies job kept Amazon so now you have job at Amazon and let's say the requirements job requirements are and let's say it's a little bit more lenient okay so what they want is somebody over eighteen or someone has someone who has a high school degree okay so now somebody could be 17 and still apply for this job okay so let's say you can apply here a little different so now let's say we have John here name let's do new guy equals John and we'll say far and let's say John's age is let's do 20 not 20 like 17 okay and let's say John has or actually let's say let's say John is John dropped out of high school and his age is like 28 right and he doesn't have high school like let's do high school is false okay so he doesn't have a high school degree but he's 28 years old so now if you look at here right we said that it's a little bit more lenient so now we need to change this to or okay so now the difference between and and or is that for and both conditions have to be matched for this whole expression to be true right before or either these conditions can be true and it was the little one so in this case even though he doesn't have a high school right high school is false here and we're saying it high was equal to true right even though I schools not there he's over 18 way over 18 right so he still he can still apply for this job if I run it there that you can apply right but before for job at Apple you couldn't because you needed to be over 18 and have a high school degree right so you can just have one or the other but for Amazon you can have one or the other okay so that's how logical operators really work alright guys so that's it for if statements and logical operators this video turned out to be way and longer than what I had initially planned but I really couldn't help it I even took out two more examples because the video was getting way too long I'm probably going to put them as puzzles on the website so if you're not enrolled make sure you're enrolled so yeah I tried to make it as short as I can in the post edit but I don't think it helps much so if you think that this video was longer than it needed to be if I was talking too much or whatever right just let me know and I'll keep that in mind for my future videos okay so yeah that's it for it statements and in the next video we're going to go over loops for loops and while loops and all that fun stuff so till then take care and we'll talk soon [Music]
Info
Channel: whatsdev
Views: 22,172
Rating: undefined out of 5
Keywords: javascript, learn javascript, learn javascript programming, javascript 2017, learn web development, javascript tutorial, html css and javascript, web programming, what are javascript data types, javascript data types and variables, variables in javascript, most popular programing language, javascript if statements and logical operators, javascript boolean operators, javascript conditionals and control flow, best programming language to learn
Id: 4eqJNLl-03U
Channel Id: undefined
Length: 28min 57sec (1737 seconds)
Published: Thu Apr 27 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.