Locked lesson.
About this lesson
In this lesson, we learn how to use "And" operators within an If/Else statement.
Exercise files
Download this lesson’s related exercise files.
32 - Logic If else And Operators.docx61.4 KB 32 - Logic If else And Operators SOLUTION.docx
58.6 KB
Quick reference
Logic: If/else And Operators
The if/else and operator && allows you to make more than one comparison. All must be true to evaluate true.
When to use
Use this whenever you need to compare more than one thing and all of them need to be true.
Instructions
int a,b;
a = 20;
b = 19;
if (a > b && a < 100)
{
Console.WriteLine($"{a} is Greater than {b} and less than 100");
}
Hints & tips
- && // and operator
- With an && operator, ALL conditions must be true for the if statement to evaluate to true
- 00:01 Okay, in this video, I want to start to look at multiple comparisons,
- 00:06 multiple conditionals.
- 00:08 And in the last video, we looked at a basic if else statement,
- 00:11 where we're just testing for one thing, is a greater than b?
- 00:14 If it is, do this, if it's not, do something else.
- 00:18 But oftentimes, you need to test against more than one thing at once, right?
- 00:22 And you can do that in a couple of different ways using the and
- 00:26 operator or the or operator.
- 00:27 In this video, we're going to look at the and operator.
- 00:29 In the next video, we're going to look at the or operator.
- 00:31 They're very similar but subtly different.
- 00:33 So let's come through here and
- 00:34 let's just get rid of this else statement just to make this a little less confusing.
- 00:37 So here we see if a is greater than b,
- 00:40 then we just want to output this to the screen.
- 00:42 And if we run this, we know what's going to happen,
- 00:45 it's going to say 20 is greater than 19, all right?
- 00:48 So what if we want to test more than one thing?
- 00:51 Well inside of here, we can use the and operator, which is the double and sign.
- 00:56 And now, after that we can run another comparison.
- 01:00 So we could say && a = b, right?
- 01:05 So if a is greater than b and it equals b, then print this out.
- 01:10 Now, obviously this is not going to evaluate to true
- 01:13 because a can't be greater than n and equal to b.
- 01:16 So that's kind of weird, but if we run this, we can see sure enough, no, right?
- 01:22 So maybe this is a bad example.
- 01:25 So let's say if a is greater than b and
- 01:29 a is greater than 100, right?
- 01:33 Okay, that's still going to evaluate to false.
- 01:36 We run this, nothing happens, right?
- 01:39 So let's go ahead and change this.
- 01:40 Let's change this to 200.
- 01:42 Now a is greater than b, 200 is greater than 19 and a is greater than 100.
- 01:50 So here if we run this and notice it says 200 is greater than 19, right?
- 01:56 So in order for this double and operator to evaluate to true,
- 02:02 both of these conditions must evaluate to true.
- 02:06 And that's the difference between and and or,
- 02:08 we'll talk about that a little bit in the next video.
- 02:11 So that's and.
- 02:12 You can do it all on one line like we've done here or you can break this apart.
- 02:17 A lot of times you see something like this because it's just sort of easier
- 02:22 to read maybe.
- 02:23 I don't know, if we save this and run it,
- 02:26 really we're going to get the exact same output 200 is greater than 19.
- 02:31 It's just a matter of style really.
- 02:33 If it's going to be not very complicated, I might do it all on one line.
- 02:40 If you're going to have multiple and statements, it might make sense
- 02:44 to break them apart like this, just so it's a little easier to read.
- 02:48 Again, that's just a matter of personal style.
- 02:51 And as a coder you'll develop into your own style what you like to do.
- 02:56 But here we can have as many of these as we want.
- 03:00 So you can say and a is less than 1000, right?
- 03:03 It doesn't matter how many we do, they still all have to evaluate to true
- 03:08 in order for this whole thing to evaluate to true when we're using and.
- 03:13 So if we save this and run it, again we're going to get the same thing,
- 03:19 200 is greater than 19.
- 03:21 Because 200 is greater than 19, 200 is greater than 100, and
- 03:26 200 is less than 1000.
- 03:28 So all three of these things are true, so it evaluates to true.
- 03:31 So that's how you can use multiple comparisons,
- 03:34 multiple conditionals in your if statements using and.
- 03:37 In the next video we'll look at or.
Lesson notes are only available for subscribers.