Loading
Lesson 27
Courses / JavaScript for Complete Beginners
Using the if Statement to Control the Flow in JavaScript

Video Transcript

Welcome to NBK Attack Well. In this lesson we're going to be introduced to control flow. We're gonna learn about the if statement. So imagine your program is a road and you're going down this road but it's only one way right? Now imagine if you want to be able to change the course of your program. Imagine if that road you end up in an intersection where you can go this way or that way. So how can we accomplish that in our program? How can we change the course of the program based on some condition? You can make the decision to go this way or to go that way and that's control flow. You can control which way your program is going to go and to do that we're gonna learn about the if statement. Okay? So basically if a certain condition is true you're gonna go this way otherwise if the condition is false you're gonna go the other way. In the programming world the terms to think about are usually if this condition is true do this else that's a keyword do this other thing. So let's get started. So let's say you are at some place where you can only enter if you're at least 18 years of age and older. So you can enter if you are at least 18. Now how can we do this in our program? Let's imagine let's make it happen here. Let's make a variable called age to hold our age. Let me make it let so I can change it later. So let's say my age is I'm just gonna make it out 35. So I'm storing a number inside the variable age. Now in my program somewhere down the road I want to be able to ask the question are you at least 18 years old if so go this way otherwise go the other way. To do that we're gonna use the if statement. So if you are at least 18 go this way I'm just making it up some pseudocode for you here otherwise go the other way. So if the condition that you are at least 18 you're gonna do go this way otherwise you're gonna go another way if you're not at least 18. So to do this the condition right here is going to be a boolean variable boolean value okay. So remember boolean value is either true or false. So if this condition right here is true okay true the result of this condition is true you're gonna go this way you're gonna do something otherwise if this condition turns out to be false you're gonna do something else you want to go the other way okay. Now that of the way let me just give a concrete example how to do this. So let's say we want to say a message console log remember console log to print a message to the console you are at least 18. Let's say we want to say this message you may enter because you are at least 18. So I want to display this message if the variable age holds the value at least 18. So we're gonna say if and open parentheses and then the condition here the condition as the age has to be greater than or equal to 18. So this is the condition okay and then we're gonna do the following open some braces right here break some line add some indentation for a style we're gonna say the console log right here and I'm gonna add a semicolon by convention okay so let me explain what I just wrote the condition of the if statement is right here if this condition turns out to yield the value true everything in between the braces will be executed in this case is going to print out to the console this message that you may enter. Now I use space here space here by convention style convention and I add indentation because I open a new block using braces so all these are style conventions that I highly encourage you to also follow. So the condition right here age is a variable right now the value is 35 so 35 is gonna go here 35 is that greater than or equal to 18 that is going to be true so this guy is gonna become true okay because of that everything inside within the following brace block this code block right here everything in between the opening and closing curly braces will be executed it can be one or more statements okay so let me just revert and just I'm gonna enter and you'll see what happens I get this message right here because the condition is true now what happens if the age is now set to 16 for example I set the age to 16 now let's try the same thing I can press the up arrow to go back to what I wrote before by the way then let's see what we get let's just age has 16 now so 16 is gonna go here 16 is greater than or equal to 18 that is going to be false so false this condition is false because of that this block will not be executed therefore we're not gonna get any message let's try it out so we got nothing out of that okay because this value turned out to be false and therefore this block was not executed now what if we want to execute something in case the condition is false we're gonna learn that next and that's called the else block so see you on the next lesson
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: