Locked lesson.
About this lesson
Understanding conditional statements using If, If Else, and If Else If statements.
Exercise files
Download this lesson’s related exercise files.
If and If/Else and If/Else If Statements.docx58.9 KB If and If/Else and If/Else If Statements - Solution.docx
59 KB
Quick reference
If and If/Else and If/Else If Statements
If, Else, and Else If Statements are great for testing conditions.
When to use
Any time you have a condition that you want to test, these statements are what you need.
Instructions
To create an If Statement:
if (condition) {
// do something
}
To create an If/Else statement:
if (condition) {
// do something
} else {
// do something else
}
To create an If/Else If statement.
if (condition 1) {
// do something
} else if (condition 2) {
// do somthing
} else {
// do something else
}
Hints & tips
- If statements are used to test 1 thing
- If else statements are used to test two things
- Else if statements are used to test many things
- Use logic operators inside your conditional to test and/or
- 00:05 In this video, I wanna talk about if-then statements.
- 00:07 Now, I've mentioned if statements a few times already when we were
- 00:10 talking about comparison operators and logic operators.
- 00:13 If statements allow us to do logic stuff.
- 00:15 If this is true, do this, if not, do that, and
- 00:18 they're really an important part of any programming language, and
- 00:21 you can actually do quite a lot of stuff with them.
- 00:23 If statements are what we call conditional statements,
- 00:25 if a condition is met, do something, if it's not met, do something else.
- 00:29 So, we'll be looking at three things here.
- 00:32 If statements, else statements, and else-if statements.
- 00:35 An if statement, so we have if conditional or
- 00:39 if (condition) do something, right?
- 00:45 So this is the format, if condition, and we execute some code of some sort.
- 00:50 So, let's just go ahead and build one here.
- 00:53 I'll go ahead and just paste this in, make this nice looking.
- 00:58 So, we have, we've declared a variable, we called it myNum, and set it equal to 41.
- 01:02 And now, here's our if statement: if myNum is greater than 12,
- 01:06 write this to the screen.
- 01:07 So, let's save this and run it.
- 01:10 And 41 is greater than 12.
- 01:12 Well, let's go ahead and change this.
- 01:14 Let's change our number to 1.
- 01:16 So, if 1 is greater than 12, do this.
- 01:21 So if we save this, come back and hit reload, we see nothing happened because 1
- 01:25 is not greater than 12, so this doesn't get executed.
- 01:28 And that's the nature of an if statement, and
- 01:30 notice we used our comparison operator here, greater than.
- 01:33 We could also do equal to or less than, or less than or
- 01:38 equal to, all of our comparison operators work there.
- 01:41 We can also use our logic operators to test against different things.
- 01:45 We could go && myNum == 12 or
- 01:50 == 43, we can string these along,
- 01:56 greater than 3, so that's where all of this stuff goes.
- 02:00 So that's an if statement,
- 02:01 the next thing I wanna talk about is an if-else statement.
- 02:03 And that, it looks about the same,
- 02:08 if (condition) do something,
- 02:16 but then else do something else.
- 02:21 So it's the same basic format, it's just at the end,
- 02:24 we slap an else and then more curly brackets.
- 02:29 So let's change our number back to 41, and let's put in some else code.
- 02:33 So, if my number is greater than 12, output This is greater than 12.
- 02:37 Otherwise, else, output, It is not greater than 12.
- 02:41 So if we save this, come back here and hit reload, 41 is greater than 12.
- 02:44 If we change this to 1, 1 is not greater than 12, so we get this output.
- 02:49 So, it's a good way to test against two different things, if-else.
- 02:54 So we'll usually use if-else statements together,
- 02:56 you very rarely use just an if statement.
- 02:59 There's times when you can use just an if statement, but for
- 03:01 the most part you're almost always gonna use if-else statements.
- 03:04 So, finally, let's look at an else-if statement.
- 03:06 And the format for those is, I'm just gonna paste this in here.
- 03:12 So, if condition1 do something, else if condition2 do something,
- 03:17 if condition1 is false, else do something if condition1 and 2 are false.
- 03:22 So this allows us to test against two different things, condition 1 and
- 03:26 condition 2.
- 03:27 So let's go ahead and make one of these real quick.
- 03:29 And we'll just paste this in here, basically the same thing.
- 03:32 If my number equals to 100, write down my number is 100.
- 03:36 Else, if my number is greater than 12, but it's greater than 12, else,
- 03:39 it's not greater than 12.
- 03:40 So let's save this and hit reload, and 41 is greater than 12.
- 03:45 So let's change our number here at the beginning to 100.
- 03:50 If we save this, hit reload, boom.
- 03:51 100 is 100 because we say, we see right off the bat this condition is met, and so
- 03:56 that gets outputted.
- 03:57 And as soon as a condition is met, the statement stops, you don't keep on going.
- 04:01 That's sort of good to note.
- 04:04 So, if we save this, change that to 1, save and reload, 1 is not greater than 12.
- 04:08 So, that's pretty much it, those are if else and else if statements.
- 04:12 In the next video, we'll look at switch statements,
- 04:14 which is sort of the same but a little bit different.
Lesson notes are only available for subscribers.