6.9 Check compound statements - Video Tutorials & Practice Problems
Video duration:
5m
Play a video:
Video transcript
<v Voiceover>Sometimes in working with if</v> or ifelse statements, a simple boolean check won't work, such as, one value is equal to another. You want to check a few things, like one test is true, and another one is true, or one is true, or another one is false. So that's where compound statements come in. To check that let's create two vectors, A and B. So now we have both these vectors, let's do an ifelse check, we'll say, "ifelse, A is equal to 1, "and, "B is equal to 1, "we run, 'Yes', otherwise, 'No'." So what this means is that, both vector A has to be 1 and vector B has to be 1, so right here in this case, A is 1 but B is not, so it should print out, "No". In this case they're both 1 so it should print out, "Yes". But for 0 it should print out, "No", but for 1 it should print out, "Yes". Running this we get, "No, Yes, No, Yes." Just as we expected. Notice, we used a single ampersand to test equality, that's very important because a single ampersand goes through and tests each element of the vector, if we rewrote this, and I will copy and paste, and do, and we put in a double ampersand, all that checks, is the very first element of each vector, and it completely short circuits what we want it to do. So while the ampersand means, "And", the vertical pipe means, "Or". Now both can be used in a single or double format. When using an ifelse function, only the single format should be used. A single ampersand or a single vertical pipe. When using a regular if statement you can use either one and have different affects. For instance, standard if statement, not an ifelse but a regular if, that's a situation where you might want to use a double ampersand or a double pipe, let's take a look, let's say, X gets 1, and Y gets 2. Let's say we do the statement, "If, "X is equal to 0, "and Y, "is equal to 3, "print out, "'Hello'." So in this case the if statement will go through and check, does X equal to 0, and even though, that fails, it's not true, it still goes and checks whether Y is equal to 3, if we had put in a double ampersand, once it failed the first check, it wouldn't even bother checking the second statement, and that's very important for two reasons, one, that can save computation time. Which in this case, the simple little program that doesn't matter, but for complex program that's being ran thousands of times, that can matter. More importantly there are some instances where you have a compound check like this and if the first one isn't true, the second one, even testing for it will fail. So in that situation it's very good to use a double ampersand, because if the first one isn't true, don't even bother with the second, so that way the program won't fail. Likewise, let's go build one with a double pipe, "If X equals 1, "or, Y equals 3, "print, 'Hello'." In this case, if the first statement is found to be true, it doesn't even bother checking the second statement because who needs it, it's an or statement. If there were only one pipe then it would check both, which, it might not hurt, but it could theoretically slow things down in a looped environment. So using the double ampersand and the double pipe can be very powerful. The first statement in this situation, the first check proves to be true, "x equals 1." It doesn't even bother with the second check. If you use the single pipe, then it would check both, even if the first one is true. While this doesn't seem like a big deal, again if this thing was being run thousands of times, that extra check could seriously add up. So using these compound checkers, is very very useful. Another type of compound test, is a nested ifelse statement. So for instance, we have B, we do, "ifelse B is equal to 1, "say, 'Hi'." Then in the else portion and this can be done in the if portion too, we do, "ifelse "B is equal to 0, "say, 'Hello', "otherwise, 'Goodbye'." This just allows further complex checks sort of like a nested else if but it's also a way to check multiple values being true, and this can be done with multiple vectors for a compound check. We could have used A in here. So understanding how to properly nest statements, and use ampersands and vertical pipes, really let's you make complex checks, and check for multiple things being true, or maybe different things being true, or perhaps one is true, or another one, either would suffice. Another important distinction is between the single ampersand and single pipe and the double ampersand and double pipe, which have different affects. It is important to remember however, that when using the ifelse function you need to use a single ampersand or a single pipe otherwise you will get unexpected results.