Locked lesson.
About this lesson
We introduce If/Else Statements and look at how they run inside a program.
Exercise files
Download this lesson’s related exercise files.
31 - Logic If else Statements.docx61.3 KB 31 - Logic If else Statements SOLUTION.docx
58.7 KB
Quick reference
Logic: If/Else Statements
If/Else Statements allow us to make decisions in our code.
When to use
Use them whenever you need to make a decision in your code.
Instructions
int a,b
a = 20;
b = 19;
if (a > b)
{
Console.WriteLine($"{a} is Greater Than {b}");
}
else
{
Console.WriteLine($"{a} is Not Greater Than {b}");
}
Hints & tips
- If/Else Statements allow you to make decisions and take action based on those decisions.
- 00:04 Okay, in this video we want to start looking at logic statements,
- 00:07 if else statements.
- 00:08 And if else statements are incredibly important with all programming languages.
- 00:11 They're really the thing that you're going to use the most often because
- 00:14 programming really comes down to just making decisions, right?
- 00:17 If this equals this, do this, otherwise do something else, right?
- 00:21 That's pretty much all programming.
- 00:22 It's Looking at things and making decisions.
- 00:25 And to do that, you need to be able to assess logical statements and use that and
- 00:28 you do that with if else statements.
- 00:30 If statements have this sort of layout, we start out with if and
- 00:33 then we use our parentheses.
- 00:35 And inside here we're going to run all of our testing.
- 00:37 And then outside of here, we use brackets and then we do stuff in the brackets.
- 00:42 So else, you do something else.
- 00:45 So this is the main layout of an if else statement.
- 00:48 And you don't have to have an else, you can just have an if, but you're generally
- 00:51 going to use an else with an if because you're going to test for something.
- 00:55 If it's not true, you want to do something else.
- 00:58 Sometimes you don't, but mostly you do.
- 01:00 So let's come up here and create some variables.
- 01:02 Let's go int a and b.
- 01:05 And let's say a = 20 and b = 19, like that.
- 01:10 So let's create an if statement.
- 01:11 Let's say if a is greater b, what do we want to do?
- 01:16 Well, let's come down here and grab this guy right here, and
- 01:19 let's pop him in there.
- 01:21 And let's create a string here and let's do this.
- 01:25 Let's say a is greater than b.
- 01:32 So if we copy this, we can also paste it in our else block here.
- 01:35 And we could say a is not greater than b, right?
- 01:40 Let's go ahead and save this and run it.
- 01:41 See how this looks.
- 01:45 And we see sure enough 20 is greater than 19, right?
- 01:50 Pretty cool.
- 01:50 So let's change this around.
- 01:52 Let's change 20 and let's change a to 18.
- 01:55 Now what's going to happen?
- 01:58 18 is not greater than 19, it's just that easy.
- 02:03 A couple of things that are weird.
- 02:06 You'll notice that at the end of almost every line of code we do have a semicolon,
- 02:10 but right here there's no semicolon.
- 02:12 That's just the format of an if statement.
- 02:16 We have if, we have our conditional, and
- 02:19 then we have whatever we want to do inside of here.
- 02:22 Semicolons continue like normal, but outside we don't have to worry about them.
- 02:26 So these are if else statements.
- 02:28 Like I said, we don't have to have the else statement, we could take this off,
- 02:35 And here 18 is not greater than 19, so if we run this what will happen?
- 02:41 Well, nothing, because it ran this if statement and said,
- 02:45 hey 18 is not greater than 19, so this doesn't get executed and
- 02:50 the program just sort of ends, right?
- 02:52 We could change this back to 20 and now that block will run and
- 02:57 it says 20 is greater than 19.
- 02:59 So very cool, super easy and super useful.
- 03:03 You're going to use if else statements for just about everything,
- 03:07 just about always and pretty cool.
- 03:08 So in the next video, we'll look at multiple comparisons.
Lesson notes are only available for subscribers.