Locked lesson.
About this lesson
Sometimes you need to compare more than two things, here's how.
Exercise files
Download this lesson’s related exercise files.
Multiple Conditional Comparison Operators.docx60.1 KB Multiple Conditional Comparison Operators - Solution.docx
58.9 KB
Quick reference
Multiple Conditional Comparison Operators
We can test against multiple conditions in our IF/ELSE statements.
When to use
Use them whenever you need to compare more than one thing at a time.
Instructions
We can compare more than one thing at a time using && (and) and || (or) in our IF/ELSE statements.
To use &&:
if num > 10 && num < 20
puts "Your number is between 10 and 20"
end
To use ||:
if num > 10 || num < 20
puts "Your number is between 10 and 20"
end
Hints & tips
- && means and
- || means or
- 00:04 In the last video we talked about conditional statements just so
- 00:08 important programming and very easy to use.
- 00:10 In this video,
- 00:11 I want to talk about multiple comparison operators in your conditional statement.
- 00:15 So, remember the format let's get rid of all this stuff here.
- 00:21 And so, we have if, and our comparison operator,
- 00:24 our conditional statement goes right here.
- 00:27 Well, we can have multiple comparison statement.
- 00:30 We can do as many as we want.
- 00:31 And we'd string them together with a bit of logic.
- 00:34 And we do and/or, so and is just the and sign double,
- 00:39 or is just double pipes, up right pipes.
- 00:43 So let's go, and,
- 00:51 Or, so what's the difference between these two?
- 00:53 Well, let's just write something.
- 00:56 Let's say, lets change this to name and name equals Bill.
- 01:02 Hello John and Bill.
- 01:05 What do we have going on here?
- 01:06 So we have this conditional statement and this conditional statement.
- 01:09 And in order for this whole thing to evaluate into true,
- 01:13 both of these have to be true, because and means both conditions have to be met.
- 01:18 So if we save this and run it and I type in John, it says, I don't know you because
- 01:23 in order for this to be true, the name has to be John and Bill and that's impossible.
- 01:29 You can't have two names that are equal, right?
- 01:31 Well, in this case, we can use the or.
- 01:34 And with or, only one of these has to be true.
- 01:38 Either this has to be true, or this has to be true, in order for
- 01:42 this to get executed.
- 01:44 Otherwise, this gets executed.
- 01:46 So, save our program, run it again into your name, John.
- 01:50 Hello, John and Bill, run it again type in Bill.
- 01:54 Hello, John and Bill, run it and again, type in Mary.
- 01:57 I don't know you, because Mary is not John or Bill.
- 02:00 So this is very useful, you're going to use this a lot and you can keep going,
- 02:05 you can keep stringing these together, name equals Tim,
- 02:08 doesn't really matter, have as many as you want.
- 02:11 And you sort of have to decide whether and or or is more appropriate,
- 02:15 just based on whatever you're trying to do with your program.
- 02:19 So you might be thinking, well, what would we ever use and for?
- 02:23 Well, let's come up here to change this to number, change this to number.
- 02:31 And if we change this to, if number equals 41 and
- 02:35 number greater than 45, right, so one last thing we need to do.
- 02:41 Remember get stands for get string and we're dealing with integers now.
- 02:45 So we need to convert this to an integer and we already know how to do this.
- 02:48 We learned this a couple of videos ago, we use our to_i or 2 integer method.
- 02:54 So we save this, come back and run it.
- 02:56 And if we type in 41, it says I don't know you because the number is 41 and
- 03:01 that obviously equals 41, but it doesn't also end up being greater than 45.
- 03:05 And with and both of these things have to be true.
- 03:08 Now we can change this around a little bit, we can say greater than 41 and
- 03:12 greater than 45.
- 03:13 Save it and run it again, and type in say 99, 99 is greater than 41,
- 03:18 it's greater than 45, so it says Hello John and Bill.
- 03:21 Both of these evaluated to true.
- 03:23 We could come back and try it again and go say 43.
- 03:27 I don't know you because 43 is greater than 41, but
- 03:30 it's not also greater than 45.
- 03:32 So there are definitely reasons why you would want to use double and or double or.
- 03:37 Double and is often used with numbers and things like that.
- 03:39 But anyway, those are multiple conditionals,
- 03:41 multiple comparison operators, very useful.
- 03:44 In the next video will look at string manipulation.
Lesson notes are only available for subscribers.