Hello, and welcome. In this video, we will show you how to utilize conditional statements, “for” loops, and “while” loops, in the R programming language. Suppose that you have a group of movies in a dataset, like the one you see here, and you want to only select the movies released after the year 2000. To do this, we’re going to have to rely on a conditional statement. In other words, we need to examine the dataset row by row, and check if the year value is greater than 2000 or not. In R, we can use an “if” statement to accomplish this. Notice the syntax here. Inside the parentheses we have the conditional statement. If this statement is true, then we execute the code inside the curly braces. Since 2002 is greater than 2000, we do see the printed statement in the output. You can also add an “else” block to your “if” statement. The code inside the “else” block will only execute if the conditional in the “if” statement is false. Notice that since 1997 is less than 2000, the print statement inside the “else” block is executed. Logical operators are used to compare two values, and the output is either “true” or “false”. We’ve already seen the “greater than” operator, but of course there’s a “less than” operator as well. Putting an “equals sign” after the “greater than” sign will make a “greater than or equal to” operator. The same is true for the “less than” sign as well. In R, a single “equals sign” is used for variable assignment, so to check if two values are equal, we need to use a double “equals sign”. To check if two values are not equal, we write an “exclamation point” followed by an “equals sign”. We can also combine multiple conditional statements together. Notice the ampersand that separates the statement on the left from the statement on the right. This is the “and” operator, which will return true only if both of the connecting statements are true. So let’s see what the output should be. The statement on the left is “true” since both strings are the same, but the statement on the right is false. So the final output of the “and” statement will be “false”. You can look here for an overview of the comparison and logical operators in R. The “or” operator works similarly to the “and” operator, except that to return true, only one statement needs to be true, rather than both. The “not” operator is used to negate a Boolean value from true to false, or vice versa. And the “in” operator checks if one operand is contained in the other. “For” loops can be used to cycle through all the values in a vector. Take a look at the code snippet here. This “for” loop will run once for each item of the “years” vector, in order. At each iteration, we can access the current value using the “yr” variable that we defined. By printing this variable at each iteration, we end up outputting the entire vector. We can also combine the “for” loop with the “if else” block from before. Notice at each iteration of the “for” loop, we check if the movie year is less than 1980, and we change our output depending on the result. You can see how that works in the output. “While” loops work a bit differently. A “while” loop will continue to execute so long as the condition inside the parentheses remains true. Notice that the condition is controlled by a variable called “count”, so as long as “count” is less than or equal to five, the loop will continue to run. Since “count” starts at 1, and is incremented by 1 for every iteration of the loop, we’d expect the loop to run 5 times. And that’s exactly what we see in the output. Keep in mind that this conditional statement was relatively controlled, but “while” loops are especially useful when you don’t know how many times the loop will need to run. By now, you should understand the structure and purpose of conditional statements, for loops, and while loops. Thank you for watching this video.