1
00:00:00,620 --> 00:00:06,510
IT guys in this video we're going to take a look at if statements if else statements and comparison

2
00:00:06,510 --> 00:00:13,230
operators so if statements are used to evaluate some kind of condition and then do something based on

3
00:00:13,230 --> 00:00:15,060
that condition or something else.

4
00:00:15,210 --> 00:00:17,250
If that condition isn't met.

5
00:00:17,250 --> 00:00:22,320
All right so the syntax is very simple and much like just about every other programming language at

6
00:00:22,320 --> 00:00:24,900
least every C syntax language.

7
00:00:24,960 --> 00:00:25,860
Basically we have.

8
00:00:25,860 --> 00:00:37,460
If something is true then we're going to do something else then we're going to do something else.

9
00:00:37,470 --> 00:00:37,810
All right.

10
00:00:37,830 --> 00:00:40,940
So that's the basic simple syntax right.

11
00:00:40,950 --> 00:00:44,040
So it's comment that because it's not actual code.

12
00:00:44,070 --> 00:00:49,260
So what I'm going to do is create a variable called ID and I'm going to set it to 100.

13
00:00:49,440 --> 00:00:56,410
So we're going to first look at the equal comparison operators so let's say cool too.

14
00:00:56,970 --> 00:01:02,490
So what we'll do is we'll put an if statement here and we're going to say if Id meaning that variable

15
00:01:02,490 --> 00:01:06,740
we just created is equal to 100.

16
00:01:06,750 --> 00:01:07,140
All right.

17
00:01:07,140 --> 00:01:12,090
Now this double equal sign means that we're comparing this to this.

18
00:01:12,090 --> 00:01:16,430
It's not an assignment operator like the single equal sign.

19
00:01:16,440 --> 00:01:16,770
All right.

20
00:01:16,770 --> 00:01:20,520
So make sure that you don't get these to confuse this is assignment.

21
00:01:20,550 --> 00:01:23,730
OK we're assigning 100 to ID.

22
00:01:23,730 --> 00:01:27,240
Here we're comparing 100 to ID.

23
00:01:27,450 --> 00:01:38,210
So if this is true then let's just go ahead and console Lague and we'll just log correct.

24
00:01:38,220 --> 00:01:38,740
All right.

25
00:01:38,970 --> 00:01:42,210
So if we go ahead and save this and run it down here you'll see.

26
00:01:42,210 --> 00:01:43,220
Correct.

27
00:01:43,220 --> 00:01:43,900
OK.

28
00:01:43,980 --> 00:01:50,790
If I go ahead and change this to let's say if ID is equal to 1 a 1 and save then nothing happens.

29
00:01:50,790 --> 00:01:56,480
And that's that's what should happen because we didn't define any else.

30
00:01:56,520 --> 00:02:04,530
So if we wanted to define an else we would just go like this and we would say console Daut log and let's

31
00:02:04,530 --> 00:02:12,840
just say incorrect and save and now we get incorrect because ID does not equal 101 it equals 100 if

32
00:02:12,840 --> 00:02:16,800
I were to change just two on a one and save then we'll get correct.

33
00:02:16,830 --> 00:02:17,160
All right.

34
00:02:17,190 --> 00:02:20,120
I'm just going to undo that.

35
00:02:20,430 --> 00:02:24,000
Now at the same time we can also do not equal two.

36
00:02:24,000 --> 00:02:28,850
So we go down here let's say not equal to.

37
00:02:28,860 --> 00:02:32,720
And I'm actually just going to copy this and paste to then.

38
00:02:32,910 --> 00:02:36,900
Now not equal is going to be an exclamation and then an equal sign.

39
00:02:36,900 --> 00:02:45,570
So this is basically saying if ID is not equal to 101 an ID is actually set to 100 so it is not equal

40
00:02:45,600 --> 00:02:46,280
to 1 0 1.

41
00:02:46,280 --> 00:02:47,820
So we should get correct here.

42
00:02:47,820 --> 00:02:52,190
So if I save you can see the second one here we get correct.

43
00:02:52,230 --> 00:02:53,560
All right.

44
00:02:53,700 --> 00:02:59,190
Now this way is basically just testing the value.

45
00:02:59,280 --> 00:03:01,530
OK it's not testing the type.

46
00:03:01,590 --> 00:03:09,300
If I were to let's actually change this back to 100 if I were to change this ID right here to a string

47
00:03:09,300 --> 00:03:17,390
of 100 and then keep this ID equals 100 and save which we get correct.

48
00:03:17,400 --> 00:03:17,750
All right.

49
00:03:17,760 --> 00:03:23,040
Because it doesn't matter in this case it doesn't matter if this is a string or a number.

50
00:03:23,040 --> 00:03:24,810
It's still 100.

51
00:03:24,810 --> 00:03:27,060
Now this isn't really the way that you want to do it.

52
00:03:27,060 --> 00:03:32,370
Most times most times you want to test the value and the type is it.

53
00:03:32,610 --> 00:03:37,190
It stops a lot of different errors in issues from arising.

54
00:03:37,200 --> 00:03:42,420
So that's what I would suggest and what most programmers would suggest and the way that you do that

55
00:03:42,420 --> 00:03:44,960
is to use three equal signs.

56
00:03:44,970 --> 00:03:56,110
All right so what we'll do is we'll copy this let's say equal to the value and type and then what we'll

57
00:03:56,110 --> 00:03:59,380
do is just add an extra equal sign here.

58
00:03:59,440 --> 00:04:04,430
Now we have a string of 100 and then we're testing this.

59
00:04:04,630 --> 00:04:08,290
Three equals two the number 100.

60
00:04:08,320 --> 00:04:14,470
So if we save this and we check out the last one here it's incorrect because this is a number.

61
00:04:14,530 --> 00:04:15,770
This is a string.

62
00:04:16,180 --> 00:04:21,280
So if we change this back to a number by removing the quotes and then we save.

63
00:04:21,280 --> 00:04:23,020
Now we get correct.

64
00:04:23,020 --> 00:04:23,670
All right.

65
00:04:23,740 --> 00:04:29,500
So to avoid issues I would suggest using this most of the time.

66
00:04:29,890 --> 00:04:30,290
All right.

67
00:04:30,310 --> 00:04:32,220
At least when in doubt.

68
00:04:32,650 --> 00:04:36,490
Now we can also do not equal to the value and the type.

69
00:04:36,490 --> 00:04:44,020
So if we copy this and what we can do is just change the first one to an exclamation and then keep the

70
00:04:44,020 --> 00:04:46,570
double equals.

71
00:04:46,570 --> 00:04:48,720
So if we save that we get incorrect.

72
00:04:48,730 --> 00:04:55,810
And the reason for that is because ID is in fact not the right value and the right type.

73
00:04:55,900 --> 00:04:56,220
All right.

74
00:04:56,230 --> 00:05:03,170
If I were to change this back to a string that's now not going to be equal to the 100 number.

75
00:05:03,370 --> 00:05:06,570
So if I save now we get correct.

76
00:05:06,570 --> 00:05:07,990
All right.

77
00:05:09,430 --> 00:05:11,200
So just keep that in mind you want to.

78
00:05:11,200 --> 00:05:14,840
You want to by default used to triple equals OK.

79
00:05:14,850 --> 00:05:16,380
And this here.

80
00:05:16,570 --> 00:05:22,130
So what do we need to do now is just common all this out because I don't want all these logs over here.

81
00:05:22,600 --> 00:05:27,970
And then I want to test to see if this is actually define this ID.

82
00:05:28,090 --> 00:05:32,230
So you may think that we would do something like this.

83
00:05:32,350 --> 00:05:47,320
So if Id then consul dot log will just say no ID or I'm sorry we'll log the ID.

84
00:05:47,410 --> 00:05:55,450
So I'll put this inside of a template string so I'll use back ticks and will say the ID is and then

85
00:05:55,450 --> 00:05:57,390
we can put a variable like this.

86
00:05:57,460 --> 00:06:02,450
We already went over the template strings or template literals else.

87
00:06:03,310 --> 00:06:08,300
Then let's cancel dot log.

88
00:06:08,410 --> 00:06:10,080
No ID.

89
00:06:10,090 --> 00:06:10,380
All right.

90
00:06:10,390 --> 00:06:15,210
So if I go out and save this it's going to say the idea is 100 which is fine.

91
00:06:15,220 --> 00:06:22,630
But if we were to take this out of here so that there is no ID define and save we're going to get an

92
00:06:22,630 --> 00:06:28,000
error that says ID is not defined even though all we're doing is testing it here.

93
00:06:28,090 --> 00:06:30,300
It's still going to give us this error.

94
00:06:30,340 --> 00:06:37,240
So the correct way to test this would be is to use the type of operator and test to see if it's undefined.

95
00:06:37,230 --> 00:06:51,080
So remember the type of type of if type of ID is not equal to undefined.

96
00:06:51,080 --> 00:06:57,230
Then we want to log the ID else then we'll say no ID so let's save that.

97
00:06:57,410 --> 00:07:01,400
When I do this should be an issue actually should be in quotes.

98
00:07:01,400 --> 00:07:03,560
All right so now we get no ID.

99
00:07:03,740 --> 00:07:08,930
So if you want to test to see if something is undefined to see if it's not there at all then you want

100
00:07:08,930 --> 00:07:09,820
to use this.

101
00:07:09,840 --> 00:07:15,230
OK you don't want to just say if ID because you're going to get an error you want to test the type and

102
00:07:15,230 --> 00:07:18,610
see if it's equal to or not equal to undefined.

103
00:07:18,860 --> 00:07:23,990
You could do it either way you could say equal to undefined and then have this here and this here or

104
00:07:23,990 --> 00:07:25,180
you could do it this way.

105
00:07:26,280 --> 00:07:28,400
All right let's put a comment here.

106
00:07:28,490 --> 00:07:34,690
We'll just say test if undefined.

107
00:07:34,740 --> 00:07:35,030
All right.

108
00:07:35,040 --> 00:07:40,490
So I'm going to go ahead and just uncomment the idea up here and we'll go ahead and comment.

109
00:07:40,510 --> 00:07:49,000
So all right so so far we've gone over equal to not equal to in both.

110
00:07:49,380 --> 00:07:55,320
Also using the value in the type also testing to see if something's undefined.

111
00:07:55,500 --> 00:07:59,820
So now let's test if something is greater than or less than right.

112
00:07:59,910 --> 00:08:07,950
So we'll see a greater or less than I'm sure a lot of you guys already know this stuff is pretty easy.

113
00:08:08,460 --> 00:08:21,050
So what I'll do is copy this I will say if I d Let's do it Friday is greater than 200

114
00:08:24,090 --> 00:08:25,150
words.

115
00:08:25,760 --> 00:08:28,110
Well just keep correct and incorrect.

116
00:08:28,350 --> 00:08:28,650
All right.

117
00:08:28,650 --> 00:08:34,530
So if I go ahead and save that we get incorrect because obviously ideas set to 100 which is not greater

118
00:08:34,530 --> 00:08:35,730
than 200.

119
00:08:35,820 --> 00:08:41,140
If I were to change id to 300 and save we get correct.

120
00:08:41,310 --> 00:08:43,220
So very very easy.

121
00:08:43,320 --> 00:08:51,410
We can also put an equal sign here because right now even if I say one hundred and save we get incorrect.

122
00:08:51,660 --> 00:08:58,920
If we want to test to see if it's 100 or greater we can do greater than or equal to and then we save

123
00:08:58,950 --> 00:09:00,450
and we get correct.

124
00:09:00,450 --> 00:09:02,520
All right so greater than or equal to.

125
00:09:02,640 --> 00:09:09,480
And then you can also do of course less than or equal to I'm not going to retype it because this is

126
00:09:09,480 --> 00:09:14,250
very easy stuff and I don't want to take up too much time here.

127
00:09:14,250 --> 00:09:18,720
All right so it's going comment that out now we're going to take a look at if.

128
00:09:18,720 --> 00:09:20,560
Else.

129
00:09:20,700 --> 00:09:23,610
OK.

130
00:09:24,360 --> 00:09:30,570
So basically what I'm going to do here is I'm going to set a variable let's call it color and we'll

131
00:09:30,570 --> 00:09:36,920
set it to a string of yellow.

132
00:09:37,110 --> 00:09:37,460
All right.

133
00:09:37,480 --> 00:09:47,990
And then we're going to do is put in F here and I'm going to say if color is equal to red I should set

134
00:09:48,010 --> 00:09:57,730
it to read first so save it's equal to red then let's console log.

135
00:09:57,820 --> 00:10:00,160
Color is red.

136
00:10:00,460 --> 00:10:00,840
All right.

137
00:10:00,850 --> 00:10:08,440
And then instead of doing just else like we did above we can test other conditions by saying else if

138
00:10:08,560 --> 00:10:11,500
and then another condition.

139
00:10:11,560 --> 00:10:18,070
All right so what we'll do here is we'll say if color is equal to blue

140
00:10:20,950 --> 00:10:23,110
then we'll Konsole dot log

141
00:10:25,660 --> 00:10:27,140
color is blue.

142
00:10:27,280 --> 00:10:28,010
OK.

143
00:10:30,200 --> 00:10:33,900
Now if I save this we're going to get color is red.

144
00:10:34,080 --> 00:10:39,020
OK if I change it to blue will get color is blue.

145
00:10:39,020 --> 00:10:44,000
Now if I change it to yellow we get nothing.

146
00:10:44,000 --> 00:10:47,210
So this is where we can put in an else.

147
00:10:47,270 --> 00:10:47,600
All right.

148
00:10:47,600 --> 00:10:52,610
And we can keep doing other else if we could say if you know color is orange or whatever.

149
00:10:52,700 --> 00:11:01,460
But when you want to actually end it all just to an else and to go ahead we'll do console log and we'll

150
00:11:01,460 --> 00:11:08,190
say color is not red or blue K needs to be something that makes sense.

151
00:11:08,210 --> 00:11:15,130
So we have yellow in here we run through the tests the owls see the Yousif and it's not it doesn't match.

152
00:11:15,140 --> 00:11:18,290
So we get color is not red or blue.

153
00:11:18,620 --> 00:11:23,830
All right so now I want to do is take a look at logical operators.

154
00:11:26,850 --> 00:11:36,540
All right so if we want to test to see if we want to test more than one thing we can use these operators

155
00:11:36,540 --> 00:11:43,080
so let's create a name.

156
00:11:43,590 --> 00:11:46,290
Let's say Steve and let's create an age

157
00:11:50,100 --> 00:11:54,240
we'll say age equals 20.

158
00:11:54,910 --> 00:11:55,630
OK.

159
00:11:55,940 --> 00:12:03,830
And then what we'll do is we'll do an if statement and we're going to say if the age is greater than

160
00:12:03,830 --> 00:12:14,510
zero and a double ampersand which represents and age is less than 12.

161
00:12:14,510 --> 00:12:22,320
All right so if that's true then we want to console Daut logs.

162
00:12:22,790 --> 00:12:28,380
We want to cancel dot log and we're going to say the name.

163
00:12:28,400 --> 00:12:33,710
OK so the name variable actually will put a template string in here make it easier.

164
00:12:34,040 --> 00:12:44,350
So put a variable like this name is a name is a child.

165
00:12:44,850 --> 00:12:45,770
OK.

166
00:12:45,920 --> 00:12:50,130
And then we'll do LCF or CHRONICAN.

167
00:12:50,210 --> 00:12:56,340
We're kind of combining the logical operators and the else have here.

168
00:12:56,600 --> 00:13:05,280
So say Alsip age is greater than or equal to 13.

169
00:13:05,550 --> 00:13:10,600
And age is less than or equal to 19.

170
00:13:10,910 --> 00:13:16,730
OK if that's the case then we're going to say console log throw a template string in here and we'll

171
00:13:16,730 --> 00:13:17,270
say

172
00:13:19,950 --> 00:13:24,880
say name is a teenager.

173
00:13:25,780 --> 00:13:26,260
OK.

174
00:13:26,360 --> 00:13:29,630
And then let's do Pells

175
00:13:34,120 --> 00:13:35,560
console dot log

176
00:13:39,260 --> 00:13:41,410
back to X..

177
00:13:42,140 --> 00:13:47,900
I keep doing that name is an adult.

178
00:13:47,940 --> 00:13:48,190
All right.

179
00:13:48,210 --> 00:13:53,700
So let's go ahead and save that and we get Steve as an adult because we have Steve at 20 years old if

180
00:13:53,700 --> 00:14:00,420
we change him to 15 and say we get Steve as a teenager if we change him to four.

181
00:14:00,660 --> 00:14:02,910
We get Steve as a child.

182
00:14:02,910 --> 00:14:04,080
All right.

183
00:14:04,080 --> 00:14:12,420
So the ampersand the end logical operator is going to make sure that this and this are both true.

184
00:14:12,520 --> 00:14:15,330
OK both of them not just one but both.

185
00:14:15,330 --> 00:14:19,760
Now we want to make sure just one is true then we could use or.

186
00:14:19,800 --> 00:14:20,220
All right.

187
00:14:20,220 --> 00:14:22,790
So let me actually put a comment here so this is.

188
00:14:22,830 --> 00:14:27,920
And which is represented with two ampersands.

189
00:14:28,060 --> 00:14:32,700
OK we also have or which is represented with two pipe characters.

190
00:14:32,700 --> 00:14:33,060
All right.

191
00:14:33,060 --> 00:14:35,280
So what I'm going to do here is say if

192
00:14:38,400 --> 00:14:53,790
you will say if age is less then 16 or age is greater than let's say 65 then we're going to say Konsole

193
00:14:54,040 --> 00:14:55,340
doth Lague

194
00:14:58,580 --> 00:15:07,680
and we'll say name we'll say this is a race registration so cannot run in race.

195
00:15:07,750 --> 00:15:08,070
All right.

196
00:15:08,080 --> 00:15:13,750
So basically we have if we have a race and we have some kind of program to evaluate people that are

197
00:15:13,750 --> 00:15:20,830
allowed to run in it we can't have people that are under 16 or over 65 running in the race.

198
00:15:20,830 --> 00:15:31,960
All right so that would be a case you would use or so we'll say else console dog log will say the name

199
00:15:34,490 --> 00:15:37,870
is registered for the race.

200
00:15:38,380 --> 00:15:38,710
All right.

201
00:15:38,710 --> 00:15:44,170
So something like that and Steve cannot run in the race because he's four years old and four year olds

202
00:15:44,170 --> 00:15:46,340
cannot run in the race.

203
00:15:46,420 --> 00:15:50,910
Let's go ahead and put them at let's say twenty five and save.

204
00:15:50,920 --> 00:15:53,100
And now he's registered for the race.

205
00:15:53,150 --> 00:15:55,710
We put him at 70 save.

206
00:15:55,930 --> 00:16:00,830
You might have a heart attack so he can't run in the race.

207
00:16:01,000 --> 00:16:01,330
All right.

208
00:16:01,330 --> 00:16:03,580
So these are all the basics.

209
00:16:03,580 --> 00:16:11,890
Now I do want to show you a couple of shorthand ways of doing conditionals so the ternary operator is

210
00:16:11,890 --> 00:16:17,340
something that is available in most programming language languages.

211
00:16:17,470 --> 00:16:26,020
So as a tertiary operator and this is just a very shorthand way of doing conditionals.

212
00:16:26,020 --> 00:16:30,570
So what we'll do here is we'll do a console dot log.

213
00:16:30,970 --> 00:16:31,490
All right.

214
00:16:31,540 --> 00:16:37,490
And we're going to go back to using the ID that we have here which is set to 100.

215
00:16:37,600 --> 00:16:41,770
So we want to basically output our console log.

216
00:16:41,770 --> 00:16:42,760
Correct.

217
00:16:42,820 --> 00:16:45,850
If it's equal to 100 and incorrect if it's not.

218
00:16:46,090 --> 00:16:54,650
So we can do that by simply saying ID equals or is equal to 100.

219
00:16:54,820 --> 00:16:57,720
And then the turnaround operator which is a question mark.

220
00:16:57,730 --> 00:16:58,150
All right.

221
00:16:58,150 --> 00:17:03,650
So if this is true then we want to output correct.

222
00:17:03,670 --> 00:17:06,530
Now the else is represented with a colon.

223
00:17:06,550 --> 00:17:10,270
So we want to output incorrect.

224
00:17:10,300 --> 00:17:10,650
All right.

225
00:17:10,660 --> 00:17:16,390
So this is much shorter than writing out you know a full statement in logging correct and incorrect

226
00:17:16,630 --> 00:17:18,010
like we did above.

227
00:17:18,010 --> 00:17:21,250
So it's going save that we get incorrect.

228
00:17:21,670 --> 00:17:25,220
Why do we get incorrect.

229
00:17:25,370 --> 00:17:29,010
Consul log ID is equal to 100.

230
00:17:29,040 --> 00:17:31,380
That's because I have it as a string.

231
00:17:31,380 --> 00:17:34,290
We use the triple equals So let's save that.

232
00:17:34,290 --> 00:17:36,270
And now we get correct.

233
00:17:36,270 --> 00:17:36,600
All right.

234
00:17:36,600 --> 00:17:42,960
So you can see that this is much easier and I mean you know I can use this in every situation and every

235
00:17:42,960 --> 00:17:47,900
if statement but for something like this just makes it much easier.

236
00:17:47,900 --> 00:17:48,270
All right.

237
00:17:48,270 --> 00:17:53,910
Another thing that I want to mention is that in javascript when you do an if statement the brackets

238
00:17:53,910 --> 00:17:56,640
are the curly braces are actually optional.

239
00:17:56,640 --> 00:18:02,080
They are recommended I would suggest using them but it will work without them.

240
00:18:02,280 --> 00:18:07,350
So let's say without question capitalizes.

241
00:18:07,500 --> 00:18:11,010
So I will say without braces.

242
00:18:11,040 --> 00:18:19,350
So if we were to say if an ID is equal to one hundred and notice I'm not going to put the curly braces

243
00:18:19,350 --> 00:18:25,070
here I'm just going to go down to the next line and indent and do a console log.

244
00:18:29,630 --> 00:18:30,960
Correct.

245
00:18:31,220 --> 00:18:32,730
And then I'll do one else.

246
00:18:32,810 --> 00:18:38,110
So go like that and say Konsole dot log.

247
00:18:40,610 --> 00:18:42,230
And correct and save.

248
00:18:42,230 --> 00:18:43,750
And we get correct.

249
00:18:43,760 --> 00:18:47,860
All right so this is possible but again I wouldn't suggest it.

250
00:18:47,870 --> 00:18:51,170
I would suggest sticking with the curly braces.

251
00:18:51,170 --> 00:18:56,960
All right guys so that is it for if statements and if else statements and the next video we're going

252
00:18:56,960 --> 00:19:01,970
to jump into switches which is another form of a conditional and Javascript.

