Censoring Words - Using Logical ORs4:31 with Craig Dennis
In this video, we will learn how to check multiple conditions in one statement using a logical OR statement.
Now that we know how to block on a single specific word. 0:00 Let's expand our reach, and block more than just one offensive word. 0:03 Now one trait that we have at our disposal, is that we can check 0:07 multiple conditions by using what is known as a logical or expression. 0:09 Which means we can do something like this, if the noun is dork, or 0:14 the noun is jerk, exit the application. 0:19 In fact you could chain these or statements on forever. 0:22 Let's go introduce a logical OR 0:26 into your application to block the user from entering either dork or jerk. 0:28 Blocking against the word jerk in our code is actually pretty easy. 0:34 In fact the only thing I think I need to show you is what OR syntax looks like. 0:37 So let's go to our IF statement when we were checking for dork in the past. 0:41 Let's add an OR statement in here, so we're gonna do a double pipe symbol. 0:45 Pipe is above the enter key. 0:50 That's what OR looks like so it's gonna say noun 0:53 equals ignore case or noun equals lowercase on jerk. 0:58 Great. 1:05 Now this line is getting a little bit long and in fact if we added one more OR on 1:06 here probably go off the screen and then somebody might not see that it's there. 1:10 So one way that we can do to battle that is because we're inside of this 1:13 parenthesis here. 1:16 We can add a new line and take advantage of that space. 1:18 So now looks much better. 1:22 While we were writing that, one thing that you might have thought was that you could 1:25 do the same thing with two if statements, right? 1:28 You could write this if statement here you could say equalsIgnore "dork" or 1:30 you can make another one that says if noun.equalsIgnore "jerk". 1:35 But what would happen then is that you'd be writing these same lines inside of 1:39 these, this code block here, you'd be writing that same code twice. 1:42 And that's never a good idea. 1:45 All right so let's give this a go. 1:47 I'm gonna save it and then I'm gonna compile and run. 1:48 I'll say 20. 1:55 And we will do big and we're gonna put in jerk here, great, we caught it. 1:58 So let's walk that really quick. 2:04 So what we've done here is we have this first line that's gonna come in here. 2:06 It's gonna say noun, and we put in, jerk. 2:09 So noun is gonna be jerk. 2:11 And it says, if noun equals dork, and that's a false. 2:12 So that's going to say false. 2:15 So, false or true. 2:16 Right, because noun equals jerk and it does so we're going to say false or 2:20 true and if you imagine that anytime there's a true in there. 2:23 It's gonna be true. 2:27 Okay, so let's make sure that the dork still works. 2:28 Awesome it does after using this a bit, 2:37 I think we need to do something different about that exit. 2:39 It would be good to give the user a second chance to enter a nicer word. 2:42 Let's see if we can't fix that in the next lesson. 2:46 And one more thing I wanted to show if you wanted to do 2:48 another one of these you can always chain them with another one. 2:51 So right? So if we wanted to say. 2:53 Another guy here, say nerd. 2:57 All right perfect and that is how Ors work. 3:02 Great we just learned one way that we could check multiple conditions in one 3:08 IF statement. 3:12 By not making two identical code blocks we avoided repeating ourselves. 3:14 A goal of programming is to make sure that you don't repeat yourself or 3:18 often referred to as DRY. 3:22 As you advanced through your learnings, you'll hear this phrase often and I'm sure 3:24 it will be hammered into your thinking as you begin coding bigger applications. 3:28 What were you thinking about logic? 3:32 I'd like to also introduce you to ORs counterpart which is AND. 3:34 Well OR returns true if any statement is true and 3:38 will only return true if all expressions are true. 3:42 Logical and use the double ampersand simple 3:46 as this logic is such an important concept and used all over the place. 3:50 Let's go back and review our dating site analogy. 3:54 So an example of an OR statement is must love cats or dogs. 3:57 A cat lover would be tested and she'd say, do you love cats? 4:01 And she'd say gee, yes, yes, yes, yes, yes. 4:04 No need to ask about dogs as this person definitely met the requirement. 4:07 Now let's look at another example. 4:11 The request is younger than 40 and has no children and loves art. 4:13 This elderly gentleman has prompted how old he is. 4:18 And there is no need to go on asking any further questions as he doesn't meet 4:21 all the conditions. 4:25 Okay, so 4:26 let's take a really quick exercise around the statements before we move on. 4:27
You need to sign up for Treehouse in order to download course files.Sign up