1
00:00:01,020 --> 00:00:07,710
So so far, we've learned how to write a basic go program consisting of one file in a package, Maine,

2
00:00:08,070 --> 00:00:09,590
and we have that main function.

3
00:00:09,960 --> 00:00:16,470
So we've also learned how to to create basic functions that we can call from Maine, how to store variables

4
00:00:16,950 --> 00:00:21,750
and how to create our own types of variables with structures and types.

5
00:00:22,290 --> 00:00:24,300
And we've learned about maps and slices.

6
00:00:25,080 --> 00:00:30,030
One of the things that you need to know, and we're going to do it right now is how to make decisions

7
00:00:30,030 --> 00:00:31,990
in a program, Engo in particular.

8
00:00:32,490 --> 00:00:38,400
So the conventional method for making a decision is the IF statement, and it's really easy.

9
00:00:38,400 --> 00:00:42,180
If you've worked in any other programming languages, you'll have no problem with it whatsoever.

10
00:00:42,570 --> 00:00:44,010
And the syntax is simple.

11
00:00:44,130 --> 00:00:51,870
If some condition, then we're going to do something, otherwise do something else or don't do anything

12
00:00:51,870 --> 00:00:52,180
at all.

13
00:00:52,530 --> 00:00:53,760
So let's just give it a try.

14
00:00:54,600 --> 00:01:00,630
Well, first of all, create a variable and we're going to have a variable, which I'll call is true.

15
00:01:01,850 --> 00:01:05,280
And I'm going to give that a shorthand syntax and I'm going to say true.

16
00:01:05,490 --> 00:01:08,840
This is a type of bool which we haven't actually seen yet.

17
00:01:09,570 --> 00:01:11,550
So let's do the other way of looking at it.

18
00:01:11,550 --> 00:01:17,820
Just so you're familiar with the type var is true is a bool, which is a boolean, which means it can

19
00:01:17,820 --> 00:01:25,140
only hold two possible values, true or false, and will say is true, is equal to trip.

20
00:01:25,680 --> 00:01:28,560
And then we'll do a simple if statement.

21
00:01:28,650 --> 00:01:36,120
If is true then in print then we start our curly brackets log print line.

22
00:01:37,770 --> 00:01:44,220
True is true is followed by the value of is true.

23
00:01:44,400 --> 00:01:45,120
Is true.

24
00:01:46,580 --> 00:01:58,980
ELT's loved print line is true, is is true, same thing, and that's all we're going to do.

25
00:01:59,480 --> 00:02:02,710
So if you think about this, it's a very, very simple program.

26
00:02:03,320 --> 00:02:08,090
I've created a variable called Is True and I've assigned it the value of true.

27
00:02:08,090 --> 00:02:15,260
And then I use go to say to to test that value if is true, which is saying exactly the same thing as

28
00:02:15,260 --> 00:02:18,320
saying if is true is equal to true.

29
00:02:18,320 --> 00:02:22,050
And what I say is the double equals sign here because that's a comparison.

30
00:02:22,700 --> 00:02:28,130
It is true is equal to true print this otherwise print that lets run it.

31
00:02:29,570 --> 00:02:31,820
Go run maeng go.

32
00:02:31,850 --> 00:02:33,090
And what do you think is going to happen.

33
00:02:33,380 --> 00:02:34,760
Pretty simple is true.

34
00:02:34,760 --> 00:02:35,420
Is true.

35
00:02:35,750 --> 00:02:37,880
Which is a really weird sentence when you think about it.

36
00:02:38,190 --> 00:02:39,200
Let's try that again.

37
00:02:39,410 --> 00:02:43,490
The variable named is true has the value of true.

38
00:02:43,880 --> 00:02:46,220
Therefore what's here on line ten.

39
00:02:46,220 --> 00:02:50,300
We'll get executed and what's here on line twelve will not get executed.

40
00:02:50,310 --> 00:02:52,490
In other words, it should print out to the terminal.

41
00:02:52,790 --> 00:02:53,570
Is true.

42
00:02:53,570 --> 00:02:54,800
Is true.

43
00:02:57,850 --> 00:03:04,970
And it does and if we come up here and change is true to false, then it should print this letter instead.

44
00:03:05,620 --> 00:03:07,440
So let's clear the screen and run it.

45
00:03:07,450 --> 00:03:10,890
And it should print is true, is false, and it does.

46
00:03:11,560 --> 00:03:18,850
So that's a basic if statement, if statements can become more complex than that, but not difficult

47
00:03:18,850 --> 00:03:19,250
at all.

48
00:03:19,270 --> 00:03:20,430
So let's try something else.

49
00:03:20,440 --> 00:03:29,590
Let's create a new variable called cat is assign the value of cat and let's do it if statement if cat

50
00:03:29,980 --> 00:03:35,720
is equal to cat log dot.

51
00:03:35,920 --> 00:03:37,710
I've got to get inside the brackets there.

52
00:03:40,680 --> 00:03:55,950
Logged print line cat is is Cat Elz, and again, inside those curly brackets, log gridline cat is

53
00:03:55,950 --> 00:04:01,710
not cat again very simple will run it because Cat is assigned the value of cat.

54
00:04:02,220 --> 00:04:04,260
That first one should actually get printed.

55
00:04:04,260 --> 00:04:06,360
And it's given me a warning saying cat is always true.

56
00:04:06,360 --> 00:04:11,430
But I know that because I'm just trying to demonstrate something here it was a cat is a cat and if I

57
00:04:11,430 --> 00:04:14,490
say Cat two instead of cat it should put the second one.

58
00:04:14,580 --> 00:04:15,540
Cat is not cat.

59
00:04:16,140 --> 00:04:16,980
Cat is not cat.

60
00:04:17,400 --> 00:04:18,870
OK, let's try it with numbers.

61
00:04:20,950 --> 00:04:21,840
My name

62
00:04:25,140 --> 00:04:27,790
is equal to or is assigned the value of I'll use.

63
00:04:27,810 --> 00:04:34,400
The shorthand is assign the value of one hundred and this time I'll do a second variable as well.

64
00:04:35,070 --> 00:04:38,040
Is true is assigned the value of thoughts.

65
00:04:39,360 --> 00:04:42,840
What if I want to test two conditions which is what I'm trying to demonstrate here.

66
00:04:43,200 --> 00:04:52,830
If my name is Tomago greater than ninety nine and I'm going to use a double ampersand to say I want

67
00:04:52,830 --> 00:04:57,120
to have this condition and another condition is true.

68
00:04:59,630 --> 00:05:02,460
Then log, print line.

69
00:05:04,400 --> 00:05:13,830
My name is greater than 99 and is true, is set to true.

70
00:05:14,240 --> 00:05:18,780
Now what's going to happen if I run this program first?

71
00:05:18,810 --> 00:05:25,400
I've been passing in two conditions and both of these have to evaluate to true in order for this to

72
00:05:25,400 --> 00:05:26,140
take place.

73
00:05:26,540 --> 00:05:32,420
So the first condition is the number of the variable stored in myname has to be greater than 99.

74
00:05:32,870 --> 00:05:36,170
And the variable is true, has to be set to true.

75
00:05:36,800 --> 00:05:38,660
Is are both of those conditions true?

76
00:05:38,660 --> 00:05:40,570
Well, is 100 greater than 99.

77
00:05:40,580 --> 00:05:41,000
Yes.

78
00:05:41,210 --> 00:05:42,440
Is is true.

79
00:05:42,830 --> 00:05:43,250
True.

80
00:05:43,370 --> 00:05:47,050
No, it is set to false because one of these conditions is false.

81
00:05:47,540 --> 00:05:48,780
This will never execute.

82
00:05:49,010 --> 00:05:51,050
So if I ran this program, nothing should happen.

83
00:05:51,600 --> 00:05:55,610
Let's clear the screen, run the program and nothing happens.

84
00:05:55,640 --> 00:05:56,330
Exactly.

85
00:05:56,630 --> 00:06:02,390
Now, if I change this, if I say and is true, is equal to false, I could do it like this is true

86
00:06:02,390 --> 00:06:03,440
is equal to false.

87
00:06:04,220 --> 00:06:09,740
And this time of year in the program, it will run and it will print it out, which is exactly what

88
00:06:09,740 --> 00:06:10,130
I wanted.

89
00:06:10,370 --> 00:06:11,290
But there's a shorthand.

90
00:06:12,650 --> 00:06:19,700
So instead of saying is true, is equal to true, I can put an exclamation mark in front of it which

91
00:06:19,700 --> 00:06:28,820
says not so if my name is greater than 99 and is true, is not true or not, is true, in other words,

92
00:06:28,820 --> 00:06:29,720
is to is false.

93
00:06:30,320 --> 00:06:33,020
This should run and it does.

94
00:06:33,020 --> 00:06:35,180
And it prints out the line exactly as I expected.

95
00:06:35,510 --> 00:06:39,200
And if I wanted to, I could put at else condition here, just as I did before.

96
00:06:39,200 --> 00:06:48,470
And in fact I can go else ELT's ELT's, I put something in here.

97
00:06:49,370 --> 00:07:02,720
So else if my number is less than one hundred and not and is true, which is the same thing as saying

98
00:07:02,720 --> 00:07:03,860
and is true is true.

99
00:07:03,980 --> 00:07:08,510
OK, and here I could have a third condition if myname

100
00:07:12,350 --> 00:07:18,020
is exactly equal to 101 or is true.

101
00:07:19,840 --> 00:07:32,450
Elusive, my name is greater than 1000 and is true, is equal to faults or not is true.

102
00:07:32,720 --> 00:07:34,580
I can have all of those and I have an area.

103
00:07:34,580 --> 00:07:39,410
One is this is truly mine, a much greater thousand and is true equal to faults, evaluative, but not

104
00:07:39,410 --> 00:07:39,710
used.

105
00:07:39,710 --> 00:07:39,820
Right.

106
00:07:39,830 --> 00:07:44,150
I got to put something in there so I could just put log print lines and I'll just put really simple

107
00:07:44,150 --> 00:07:44,840
things in there.

108
00:07:46,070 --> 00:07:46,670
One

109
00:07:50,360 --> 00:07:52,840
to three.

110
00:07:55,070 --> 00:08:05,300
OK else if they're so now I can change these values and depending on what I change them to, one of

111
00:08:05,330 --> 00:08:07,390
these conditions might execute.

112
00:08:07,940 --> 00:08:13,880
So you can have as many Elsas as you want and in a statement, but honestly, you probably never want

113
00:08:13,880 --> 00:08:16,160
to have more than one or two eltis.

114
00:08:16,340 --> 00:08:19,430
I only ever go with one in the vast majority of cases.

115
00:08:19,430 --> 00:08:24,350
Usually I test a condition and if it evaluates to true, I want to do something.

116
00:08:24,350 --> 00:08:26,120
Otherwise I want to do something else.

117
00:08:26,450 --> 00:08:29,780
If I have something that's more complex than that, then than that.

118
00:08:29,780 --> 00:08:35,150
And I have more than two things, have two situations or two cases I ever want to handle, I chances

119
00:08:35,150 --> 00:08:37,010
are I'm not going to use an if statement.

120
00:08:37,100 --> 00:08:43,730
I'm going to use another decision structure which is which exists specifically for this kind of situation.

121
00:08:43,730 --> 00:08:45,140
So I'm going to delete everything out of this.

122
00:08:45,470 --> 00:08:51,140
And I clear the screen down here and we'll go to another decision structure called the switch statement.

123
00:08:52,160 --> 00:08:54,590
And the switch statement is really easy.

124
00:08:54,920 --> 00:09:03,350
So you simply say switch it on some value, which I'll call my var, which I haven't created yet, but

125
00:09:03,350 --> 00:09:10,100
I will in a minute switch on my var and then you have cases and you have a case and some value.

126
00:09:10,110 --> 00:09:11,270
So let's create my var.

127
00:09:11,270 --> 00:09:12,230
So this makes sense.

128
00:09:12,620 --> 00:09:21,830
My bar is assigned the value of cat and I'm going to switch on my var inside of those, these brackets

129
00:09:21,830 --> 00:09:23,210
I'm going to have key statements.

130
00:09:23,570 --> 00:09:30,440
So the first one is to say if the if my var has the value of cat do something.

131
00:09:30,890 --> 00:09:38,390
So I put case followed by the value I'm looking for or the condition I'm testing for, followed by a

132
00:09:38,390 --> 00:09:47,840
corn and then do something like the print line cat is set to cat and I better use lower case here.

133
00:09:47,940 --> 00:09:52,220
Not confused myself then I can have another case.

134
00:09:53,950 --> 00:10:03,070
Which has to come here, of course, another case, case dog and then do something, so I'll copy this

135
00:10:04,540 --> 00:10:05,690
and paste this in here.

136
00:10:06,250 --> 00:10:14,530
Cat is such a dog that I might have a third one, which I copy the whole thing and pass it down and

137
00:10:14,590 --> 00:10:15,780
put it in the right spot again.

138
00:10:19,270 --> 00:10:23,020
And fish cat is set to fish.

139
00:10:23,970 --> 00:10:26,500
Now, I have an error somewhere.

140
00:10:26,530 --> 00:10:27,010
What is it?

141
00:10:31,270 --> 00:10:37,460
The error is I get an extra quote hidden down below the scroll here, extra closing bracket.

142
00:10:37,810 --> 00:10:39,570
All right, so now I have a switch statement.

143
00:10:39,880 --> 00:10:43,780
So I have a variable called cat called my Vah, which is set to cat.

144
00:10:44,110 --> 00:10:47,440
And then I switch on it and I switch on the variable, my version.

145
00:10:48,670 --> 00:10:55,180
And I simply say, if that variable happens to equal cat do this, if it happens to equal dog, do this,

146
00:10:55,300 --> 00:11:01,900
if it happens to equal fish, do this and then I can have a special case default, which is to say,

147
00:11:02,110 --> 00:11:10,120
if none of the above conditions are true, do this log print line and keep putting these in the wrong

148
00:11:10,120 --> 00:11:11,040
spot for some reason.

149
00:11:14,500 --> 00:11:15,070
Default.

150
00:11:18,760 --> 00:11:26,300
Logged print online cat is something else, and I saw what I have to do.

151
00:11:26,770 --> 00:11:28,330
So let's think about what's going to happen here.

152
00:11:29,040 --> 00:11:30,460
I've established this variable.

153
00:11:30,460 --> 00:11:34,510
I created this variable called my version of assign the value of Cat to it.

154
00:11:34,930 --> 00:11:39,830
Then I switch and I handle a bunch of cases and I can have as many cases as I want.

155
00:11:40,990 --> 00:11:45,110
Check this valuable mirror and compare it against all of these cases.

156
00:11:45,130 --> 00:11:48,580
So one is cat is cat equal to cat?

157
00:11:48,670 --> 00:11:49,450
Yes, it is.

158
00:11:49,480 --> 00:11:50,690
Therefore, this will happen.

159
00:11:51,190 --> 00:11:56,950
Now, in other programming languages that implement the switch statement, you actually will keep going.

160
00:11:56,950 --> 00:12:00,750
You keep testing to see do you meet any of these other conditions.

161
00:12:01,060 --> 00:12:10,420
So if the condition was case cat or and then case some other condition that matches it, it would keep

162
00:12:10,420 --> 00:12:10,780
going.

163
00:12:10,810 --> 00:12:14,170
And if it matched three of the cases, it would do all three of those actions.

164
00:12:14,260 --> 00:12:19,090
Go does not do that go breaks out as soon as it matches one case.

165
00:12:19,090 --> 00:12:20,600
And that's something you have to remember.

166
00:12:21,460 --> 00:12:26,290
So if I run this program, it should print out cat is set to cat.

167
00:12:26,290 --> 00:12:26,770
Let's try it.

168
00:12:27,250 --> 00:12:30,370
Go run Mingo Cat said the cat.

169
00:12:30,730 --> 00:12:37,220
If I said Cat two dog and run the program, it should print out the case that matches dog and cat,

170
00:12:37,240 --> 00:12:37,900
said the dog.

171
00:12:38,350 --> 00:12:41,800
If I said it to horse, it doesn't match any of these three cases.

172
00:12:41,920 --> 00:12:42,710
What's going to happen?

173
00:12:43,090 --> 00:12:45,130
Well, it's going to execute this default case.

174
00:12:45,130 --> 00:12:48,070
So let's run that program and see what happens.

175
00:12:48,430 --> 00:12:49,920
Cat is set to something else.

176
00:12:50,620 --> 00:12:53,140
So those are your two basic decision structures.

177
00:12:53,170 --> 00:12:59,710
The IF statement, which you use a great deal of the time, and the switch statement which you use periodically,

178
00:12:59,710 --> 00:13:02,830
particularly when you have to make a decision based upon a lot of different cases.

179
00:13:03,070 --> 00:13:05,890
There is a third one, but we're not going to get to that for a little while.

180
00:13:05,890 --> 00:13:11,140
So bear in mind that the two main ones we've covered right now, the if statement and the switch statement

181
00:13:11,140 --> 00:13:13,840
and the third one will get to a little bit later on.

182
00:13:14,860 --> 00:13:16,060
That's enough for this time around.
