Write a Conditional Statement: if3:37 with Guil Hernandez
There are many times in programming where you'll need to make a program behave differently based on conditions within the program.
You can make a program react to different situations by using what are called conditional statements. In this video, you'll first learn to write an
So now below the movies array, I'll write a short program that displays a message 0:00 based on the value of the status property. 0:05 So if status equals 'available' the code will print a message 0:08 to the console display and the movie title and showtime. 0:13 Otherwise, if status is equal to 'unavailable' 0:15 it will print a message saying that the movie is sold out. 0:19 So I need the program to look through each object inside the movies array and 0:23 check if the value of status is either available or unavailable. 0:28 I know that a loop offers a way to repeat the same actions a certain 0:35 number of times, so I can use any of the two loops I talked about earlier. 0:38 I'll go with a for up loop. 0:43 So below the movies array. 0:44 Type for (var movie of movies). 0:47 Inside this loop, I'll need to create the actions to be carried out for 0:53 each object that gets passed into the loop. 0:58 So if status is available, display a message. 1:01 If status is unavailable, display a different message. 1:04 There are many times in programming where you'll need to make a program behave 1:08 differently based on conditions within the program. 1:12 You can make a program react to different situations 1:15 by using what are called conditional statements. 1:18 A conditional statement can be as simple as if this is true, then do that. 1:20 Conditional statements begin with the if keyword and after 1:25 if comes a set of parentheses and inside of the parentheses comes the condition. 1:30 The condition is just a simple test whose answer is either true or false. 1:35 I'll follow that with a set of curly braces. 1:41 And inside the parentheses, I'll test if 1:43 the status property is equal to available by typing 1:48 movie.status === the string 'available'. 1:55 So now inside the curly braces, 2:01 you put the code that runs only if the condition is true. 2:03 So here I'll add a console.log, 2:07 And once again I'll use the plus sign to combine several strings together, 2:14 with the value of an object's title and time properties. 2:20 Remember to access the value of an object's property you use dot notation. 2:25 So first I'll use a set of quotes that says, the movie, followed by space. 2:29 Outside of that I'll use the plus symbol, then I'll access the object's title 2:35 property with movie.title, follow that with a plus sign and then a set of quotes. 2:39 Inside the string we'll pass in space, then the words plays at, and 2:46 another space. 2:51 And outside this string we'll type a plus sign and 2:53 then access the time property with movie.time. 2:56 So this message, for example, might say, The movie Avengers plays at 12 PM. 3:02 So this condition here is like a test, it asks if the value 3:09 of the status property is equal to the string available. 3:14 The triple equal sign here is called a strict equality operator, and 3:19 it's used to test if two values are exactly the same. 3:23 And, if they are the same, then the condition is true, and 3:27 the code inside the curly braces runs. 3:30 You can read more about equality operators and 3:32 other comparison operators in the teacher's notes with this video. 3:34
You need to sign up for Treehouse in order to download course files.Sign up