Locked lesson.
About this lesson
In this lesson, we learn how to use "else if" within an If/Else statement.
Exercise files
Download this lesson’s related exercise files.
34 - Logic If else if.docx61 KB 34 - Logic If else if SOLUTION.docx
58.8 KB
Quick reference
Logic: If/else if
If else if allows you to test for more than one condition in your if statement.
When to use
Use this when you need to test for more than one condition in your if statement.
Instructions
int a,b;
a = 20;
b = 19;
if (a>b)
{
Console.WriteLine($"{a} is Greater than {b}");
}
else if (a == b)
{
Console.WriteLine($"{a} is Equal To {b}");
}
else
{
Console.WriteLine($"{a} is Not Greater than {b}");
}
Hints & tips
- Allows you to test more than one condition in your if statement.
- 00:04 Okay so we've looked at if else statements there's also an if else if statement.
- 00:08 So it gives you even more control over your logic.
- 00:12 It allows you to make separate comparisons slightly differently than the and
- 00:17 and the or.
- 00:18 So in and and the or we were able to compare more than one thing.
- 00:21 In an else if statement, we can make an entire other argument, question, right?
- 00:27 And so let's just run this and see.
- 00:28 Let's redo our else statement here like we did earlier.
- 00:32 And let me just copy this guy.
- 00:36 And here we want is not greater.
- 00:38 So, here we can say if a is greater than b, then we want to do all this stuff.
- 00:43 So, if we save this and run it, we see 20 is greater than 19.
- 00:49 But what if we wanted to ask another question so to speak?
- 00:52 Well, we could do that.
- 00:54 Right here, we can go else if and then we can do an entire other statement.
- 00:59 So we could say else if a = b right,
- 01:02 then let's just copy this guy paste it in.
- 01:07 And let's say is = b.
- 01:12 So if we save this and run it,
- 01:13 nothing's really going to change here because 20 is greater than 19.
- 01:18 So what happens is anytime you're using any sort of if else or if else if blocks,
- 01:23 your program is going to start at the top and it's going to work its way down.
- 01:28 Anytime it evaluates to true,
- 01:31 it will execute the next block of code and then stop, right?
- 01:36 So if we get to this and we say, this is true, it will spit this out and then stop.
- 01:41 It will ignore all of the rest of these things.
- 01:45 But if it comes down here,
- 01:46 evaluates this as false, it'll drop down to the next thing, this one.
- 01:51 If this evaluates to false, it'll drop down to this one.
- 01:54 And then finally spit this out.
- 01:56 If if there's no true things by the time we get to the end, the else,
- 02:00 then the else gets executed, right?
- 02:03 But if at any time we hit true, it stops.
- 02:06 So let's change this around.
- 02:07 Let's go 20, so here we start out is 20 greater than 20?
- 02:14 No, it's not this is false.
- 02:15 So it's going to skip this and it's going to go to this.
- 02:18 And it's going to we have a whole new question to ask here, does a equal b?
- 02:23 Well, 20 does equal 20, so it will execute this line and then stop.
- 02:29 If this was false, then it would finally spit out this.
- 02:31 But this one is true, so we should see this on the screen.
- 02:36 So let's go ahead and save this and run it.
- 02:38 All that build up, 20 is equal to 20, all right, cool.
- 02:43 If we change this again back to 19 or 10, or anything else,
- 02:47 we could walk through this again is 19 greater than 20?
- 02:51 No, it's not, so hey, skip this.
- 02:54 And we come to this hey, is 19 equal to 20?
- 02:57 No it's not, so skip this.
- 03:00 Then execute finally whatever this is.
- 03:03 So if we save this and run it we get, as we expect, 19 is not greater than 20.
- 03:10 So that's how you use an else if statement.
- 03:12 Now, you can have as many of these as you want.
- 03:14 You can have 50 else if statements,
- 03:17 you can just keep under each one keep going, and just keep doing things.
- 03:21 And you can have as many of these as you want.
- 03:24 It will just bang, bang, bang, bang, start at the top keep going down until it finds
- 03:28 something that's true and then it will execute right after the true thing,
- 03:32 whatever is in these brackets, and then stop.
- 03:35 So that's if, that's if else, if else if, that's comparison operators, that's logic.
- 03:40 That's really all you need to know in order to do some pretty powerful coding.
- 03:44 And as you can see, it's pretty simple.
- 03:46 And that's all there is to it.
- 03:47 So that's it for this video.
- 03:49 In the next video we'll look at switch.
Lesson notes are only available for subscribers.