6.6 Stagger "if" statements with "else" - Video Tutorials & Practice Problems
Video duration:
5m
Play a video:
<v Voiceover>Sometimes,</v> a simple if statement won't suffice. Perhaps, an action must be taken if the if statement fails. That's where else comes into play. To demonstrate this, we will build a simple function that has the if and else statement written in the function. That's because we're going to call this a lot and it'll make things more reproducible. So let's start off by calling it check.bool and this gets a function that has just one argument, x. So in here, we do a check. If x is equal to one, print Hello. Now, we want the else action so we write else and in R, it is very important that the else part of the statement comes next to the closing curly brace of the if statement. It cannot be on its own line because when R gets to the end of the line, it assumes a statement is done unless there's some sort of continuing keyword like a plus sign, a multiplication or else. If else was one line below, it'd read the if statement, think it's done then all of a sudden see else and not know what to do. It's a bit frustrating but that's one of the things you have to work with in R and that's very important to remember. So else print Goodbye. Close off the curly brace for the else statement and close off the curly brace for the whole function. If we look at this, we have check.bool. It gets function and there's a nice compound if statement. We will run this to get into memory and now, we can go ahead and use it. We could do check.bool and put in one and see what it says. We get Hello. If we do check.bool and put in zero, we get Goodbye. Let's put in a string and see how it reacts to a string. Just a little bit of text and it says Goodbye because it's not equal to one. Let's see what happens when we'd use a logical such as true. It returns Hello. That's because true is represented as the numeric number one so when it gets passed through, you checked if x equals one, true is the same thing as one. Now, sometimes, a simple else statement isn't enough. You're testing a few things. If one thing, else if another thing, else another thing so that's where else if comes in. So let's rebuild check.bool to handle a few options. So check.bool has to be rewritten as a function with one input x. The first thing we do is the first if statement. If x equals one. Now as things become more complicated, it might be a good idea to leave notes for our self. Like any good programming language, R has comments. By using either the pound or hash symbol depending what country you're from, we can leave a comment so anything after this symbol is a comment. It's there for humans and the computer will ignore it. So ignored by computer. Comments are very very important. They really help with the code writing process and they help even more with the code reading process so please use comments liberally as they will make your work flow a lot better. Assuming that x equals one, we will print Hello then we will say else. Remember, on the same line as the closing if brace. And we say else if because if x doesn't equal one, check something else. In this case, does x equal zero? We can put something here. We could say print Goodbye and then we could have yet another fall over, a catch all saying if anything else is true, print Confused. Close off our curly braces. Now, this function's getting pretty big so I'll adjust my screen so it's easier to see. So remember, this is a function that takes in one argument, checks if it's equal to one. If that fails, checks if it's equal to zero. If that fails, it prints some default. So again, let's run this and it's now in memory. I'll resize my screen again and now we can go ahead and run this function so let's start with check.bool of one. It says Hello. If we do check.bool of zero, we get Goodbye. Let's try check.bool 2. It says Confused. And for good measure, let's try some text. There you go. So it works pretty much as we expected. And the reason this function is called check.bool because bool is a shorthand term for boolean which is another way of saying a logical variable just use in other languages. If statements provide a powerful way to control the flow of a program and combining that with else and else if really allows you to control over special circumstances and have a complex flow to your program.