Locked lesson.
About this lesson
We learn how to use "or" operators within If/Else statements.
Exercise files
Download this lesson’s related exercise files.
33 - Logic If else Or Operators.docx61.4 KB 33 - Logic If else Or Operators SOLUTION.docx
58.7 KB
Quick reference
Logic: If/else Or Operators
The Or operator || allows you to evaluate more than one thing at a time.
When to use
Use when you need to evaluate more than one thing, and not 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} or less than 100");
}
Hints & tips
- || // or operator
- Or is used to evaluate more than one thing but not all of them need to be true
- 00:04 Okay, in the last video, we looked at multiple comparisons,
- 00:07 multiple conditionals using and.
- 00:08 In this video, we want to look at multiple conditionals, multiple comparisons, and
- 00:13 I use those two phrases sort of interchangeably using or.
- 00:16 So, in the last video and, the whole thing was all of these have to be true
- 00:21 in order for the entire thing to evaluate true, that's how and works.
- 00:26 Or is different, it's or, so one or
- 00:29 the other has to evaluate to true in order for the entire thing to be true.
- 00:34 So, let's just play around this here.
- 00:36 So, let's say,
- 00:40 if (a > b | | a < 1,000).
- 00:48 Then we want to do this.
- 00:50 And this is the or operator these two, sort of, we call them pipes in
- 00:54 programming languages, is just the straight upright bar.
- 00:58 You have to hunt around in your keyboard, all keyboards are different.
- 01:01 You might have to hunt around on your keyboard in order to find it.
- 01:04 Mine is in sort of the top right corner above the Enter key.
- 01:07 Hit Shift and the pipe thing twice.
- 01:10 I don't know exactly where it's going to be on your keyboard, but
- 01:13 it's these upright straight bars, and it's two of them, right?
- 01:16 So, if 200 is greater than 19 or
- 01:20 200 is greater than 1,000.
- 01:24 Now this clearly is not true, but this is, and
- 01:28 like I said, one or the other has to be true.
- 01:32 So, this should evaluate to true, and then print this thing out onto the screen.
- 01:36 So, go ahead and save this and run it.
- 01:40 Sure enough, yep, 200 is greater than 19.
- 01:43 Or what if both of these are false?
- 01:45 Let's say, if a is less than b, it's clearly not.
- 01:49 Or a is greater than 1,000, it's also clearly not.
- 01:53 So, neither of these are true.
- 01:55 What do we think's going to happen?
- 01:57 Well, it's probably going to evaluate to false, if neither of them are true, and
- 02:01 sure enough you get nothing here, because neither of them are true.
- 02:05 So, that's all, and just like with and, you can have this on multiple lines.
- 02:10 So, you could do something like this, if you wanted to, to make it easier to read,
- 02:15 totally up to you just a matter of style.
- 02:17 And just like with and, you can do as many of these as you want.
- 02:20 So, you can just keep stringing them along, and doing more or,
- 02:24 or, ors, and that works, too.
- 02:25 So, we can say if a == b, which clearly is not, we run this,
- 02:30 it's still going to evaluate to false.
- 02:33 Sure enough, nothing there and pretty cool.
- 02:36 So, there are times when you might and, and
- 02:39 there are times when you might need or.
- 02:41 There are plenty of times when you don't have to use either of these and you just
- 02:45 want to make one comparison, you can use just your basic FL statement for that.
- 02:48 It's really just up to you in what you need to do in your program, but
- 02:51 these tools are so important.
- 02:52 Like I said, a couple of videos ago, if FL statements logic is just so
- 02:56 important in computer programming and and, or just make it even more powerful.
- 03:00 Now some people forget these, and in some programming languages you can just use or
- 03:07 like that, if we save this and run it, we get an error.
- 03:11 You can't do that in C-Sharp.
- 03:13 Same thing with and,
- 03:14 some programming languages just allow you to use the words and.
- 03:18 So again, it's C-Sharp, we can't do that, we get errors as well.
- 03:22 So, just realize you have to use either the double ampersand or
- 03:27 the double bars for and and or.
- 03:30 And you may have to Google it every once in a while, because you can't remember,
- 03:32 wait, what was or again?
- 03:34 But that's what it is, and that's all there is to it.
- 03:37 So, that's all for and and or.
- 03:38 In the next video, we'll look at as if statements.
Lesson notes are only available for subscribers.