1
00:00:00,980 --> 00:00:01,580
OK.

2
00:00:01,610 --> 00:00:02,750
Hello again, everybody.

3
00:00:02,780 --> 00:00:07,310
So in this lecture, we are going to go over something called logic.

4
00:00:08,000 --> 00:00:15,680
You may have heard of this before depends on kind of what context you might have heard it in.

5
00:00:15,920 --> 00:00:22,370
But it definitely is something that applies to programming, but definitely more than just programming.

6
00:00:22,460 --> 00:00:25,010
Logic isn't necessarily just a programming thing.

7
00:00:25,460 --> 00:00:30,170
It can apply to philosophy and all kinds of stuff.

8
00:00:30,320 --> 00:00:36,320
You know, sometimes just general discussions between people can involve or not involve logic.

9
00:00:37,610 --> 00:00:40,750
So it's more than just programming, but very important for programming.

10
00:00:40,760 --> 00:00:41,990
So that's why we're going over it.

11
00:00:43,650 --> 00:00:46,110
So like I said, you most likely have heard of logic.

12
00:00:46,380 --> 00:00:51,600
Some of you might have referred to logic is something more akin to just using common sense.

13
00:00:52,200 --> 00:00:59,040
An example here is kind of a simple one, but you know, if all dogs have a keen sense of smell.

14
00:00:59,370 --> 00:01:04,490
And my pet is a dog, then my pet has a keen sense of smell.

15
00:01:04,500 --> 00:01:10,980
You can kind of just deduce that from these previous lines here, since all the dogs have a keen sense

16
00:01:10,980 --> 00:01:11,610
of smell.

17
00:01:12,130 --> 00:01:16,800
You know you have your pet as a dog, then you go back and you say, Well, this is a dog.

18
00:01:16,980 --> 00:01:22,290
I just now found out that they have a keen sense of smell, so that means that this one dog, that's

19
00:01:22,290 --> 00:01:25,080
my pet, must also have a keen sense of smell.

20
00:01:26,600 --> 00:01:30,270
Something that doesn't probably doesn't need explanation, you're probably like, why?

21
00:01:30,500 --> 00:01:31,700
Why do we care about this?

22
00:01:31,700 --> 00:01:32,590
This is obvious.

23
00:01:33,740 --> 00:01:36,710
Sometimes the logic can get a little more complex.

24
00:01:37,790 --> 00:01:40,850
This is an incredibly complex, but let's look at another example.

25
00:01:41,920 --> 00:01:47,080
If Bob doesn't not have a watermelon, he will not get all bananas.

26
00:01:47,110 --> 00:01:55,990
If both oranges and red or yellow apples are available, so this is a little more confusing to immediately

27
00:01:55,990 --> 00:02:03,070
be able to understand what is going on in this statement and something to relate this to programming.

28
00:02:03,070 --> 00:02:10,510
You could kind of replace the fruit in this sentence here with data, and it would be closely related

29
00:02:10,510 --> 00:02:13,150
to something that you might see in your code.

30
00:02:13,150 --> 00:02:19,150
So when you're having to write some programming problems, you might see something like this where instead

31
00:02:19,150 --> 00:02:26,890
of fruit, we're dealing with some type of input data or something like that, and we need to filter

32
00:02:26,890 --> 00:02:31,750
it out by using logic and turning that logic into code.

33
00:02:32,380 --> 00:02:39,190
So thankfully, there's ways that we can represent this and some rules that we can follow to make things

34
00:02:39,190 --> 00:02:40,210
a little simpler for us.

35
00:02:41,930 --> 00:02:44,600
So let's go ahead and try to analyze this statement.

36
00:02:44,630 --> 00:02:49,160
This is going to be slightly similar to when you decipher a word problems in math.

37
00:02:49,850 --> 00:02:55,490
So when you do that, you sometimes look for the important things in the word problem.

38
00:02:55,880 --> 00:03:00,650
And then you kind of go from there, you break it down and put it in simpler terms, more mathematical

39
00:03:00,650 --> 00:03:01,250
terms.

40
00:03:01,790 --> 00:03:02,720
We're going to do that.

41
00:03:02,720 --> 00:03:05,000
But it doesn't involve a ton of math, really.

42
00:03:05,450 --> 00:03:10,100
We're just going to kind of look at it and pick out words that have meaning for us as programmers.

43
00:03:11,690 --> 00:03:19,700
So they are colored here and have color coded the ones that are related, so we have an if so, that

44
00:03:19,700 --> 00:03:21,680
is a condition, right?

45
00:03:22,100 --> 00:03:30,230
So we're saying if Bob something also here, we're saying if both something.

46
00:03:31,490 --> 00:03:38,360
So we need to consider that, and then we have this doesn't and not.

47
00:03:38,510 --> 00:03:42,500
These are essentially the same things as kind of like not not right.

48
00:03:43,160 --> 00:03:47,420
If you were to simplify this and be like, well, doesn't is basically does not.

49
00:03:47,480 --> 00:03:49,340
So it's basically like another not.

50
00:03:49,880 --> 00:03:51,530
And we also have another not here.

51
00:03:52,790 --> 00:03:59,840
Also interesting is the and you know, so you got to think about when someone says this thing and that

52
00:03:59,840 --> 00:04:01,970
thing rather than just one thing.

53
00:04:02,990 --> 00:04:07,610
And then we also have an order here, you know, or implies this or implies either or.

54
00:04:07,880 --> 00:04:09,380
So red or yellow.

55
00:04:09,590 --> 00:04:12,740
So these are the important things that we need to look at their code here.

56
00:04:14,390 --> 00:04:16,210
So let's break it down a little bit further.

57
00:04:16,220 --> 00:04:18,990
Let's look at the words that we didn't color.

58
00:04:19,970 --> 00:04:30,770
So and kind of put them in some arbitrary terms, like some variables that we can actually use to simplify

59
00:04:30,770 --> 00:04:35,570
this and put it in a more kind of a mathematical definition.

60
00:04:35,570 --> 00:04:41,420
But, you know, don't freak out like it's going to be some kind of crazy equation or something.

61
00:04:41,420 --> 00:04:46,430
We're just going to try and represent all these words with variables so we can shorten this and not

62
00:04:46,430 --> 00:04:51,530
have to like think about all these different words, cause it didn't really matter whether it's watermelon

63
00:04:51,530 --> 00:04:53,540
or bananas or like, you know.

64
00:04:55,910 --> 00:05:01,810
You know, yeah, apples, whether they're available, it's just like, we can simplify this into a

65
00:05:01,820 --> 00:05:03,890
much more simple yes and no things.

66
00:05:05,020 --> 00:05:06,580
So that's what we're going to do.

67
00:05:06,850 --> 00:05:13,150
We're actually going to look at turning these into something that can be either be true or false and

68
00:05:13,150 --> 00:05:14,370
represent it with a variable.

69
00:05:14,380 --> 00:05:17,900
So this first one right here, Bob, have a watermelon.

70
00:05:18,700 --> 00:05:24,490
So I'm just going to turn and something that barely makes more sense, which is Bob has watermelon,

71
00:05:24,820 --> 00:05:29,020
should be a bob, has a watermelon, but I'm just putting Bob has watermelon, so that's something that

72
00:05:29,020 --> 00:05:30,280
can be true or false, right?

73
00:05:30,800 --> 00:05:34,670
Either Bob has a watermelon or Bob does not have a watermelon.

74
00:05:34,960 --> 00:05:38,620
We're just going to say there's this statement that can be said about it.

75
00:05:38,980 --> 00:05:43,780
Bob has a watermelon that is either true or it's false, and we're going to represent that with A..

76
00:05:44,320 --> 00:05:45,490
Let's move on to the next one.

77
00:05:45,500 --> 00:05:45,880
So.

78
00:05:47,040 --> 00:05:49,680
He will get all bananas, so that's Bob, right?

79
00:05:50,310 --> 00:05:54,420
So Bob gets all bananas, we're going to represent that with B.

80
00:05:55,710 --> 00:05:57,660
And let's think about this whole sentence now.

81
00:05:57,660 --> 00:06:02,210
So this is kind of the thing that we're interested in, right?

82
00:06:02,220 --> 00:06:03,300
If you look at this sentence.

83
00:06:03,300 --> 00:06:04,320
So if Bob.

84
00:06:05,510 --> 00:06:06,320
Blah blah blah.

85
00:06:06,650 --> 00:06:14,660
And over here, if blah blah blah, then Bob gets all bananas basically, right?

86
00:06:15,200 --> 00:06:22,640
So these other parts of this sentence are kind of the conditions, and this is the result that's contingent

87
00:06:22,640 --> 00:06:24,290
upon those conditions, right?

88
00:06:25,730 --> 00:06:34,280
So Bob will not get all the bananas if this and if this part right here.

89
00:06:35,270 --> 00:06:40,040
So this is kind of the result, we're still going to represent it with a variable and we'll see how

90
00:06:40,040 --> 00:06:41,030
it plays into it.

91
00:06:42,320 --> 00:06:44,630
So that's going to be be Bob getting all bananas.

92
00:06:46,200 --> 00:06:48,090
Then we have this next part, so.

93
00:06:49,450 --> 00:06:58,230
Orange is right, if it says if both oranges and love are available, right, so the oranges are available,

94
00:06:58,240 --> 00:07:00,850
that's another statement that we can take out of this.

95
00:07:01,660 --> 00:07:08,740
All of these things right here, these fruits are kind of having to do with the fact that they are available,

96
00:07:09,010 --> 00:07:09,340
right?

97
00:07:10,570 --> 00:07:14,770
So we're going to put a see for oranges are available.

98
00:07:17,350 --> 00:07:19,230
Same thing with the apples, right?

99
00:07:19,240 --> 00:07:27,400
And this is specifically we have red or yellow apples, so I'm going to say D is red apples are available

100
00:07:28,150 --> 00:07:29,260
just like the oranges.

101
00:07:29,710 --> 00:07:32,830
And then also for the yellow yellow apples are available.

102
00:07:32,830 --> 00:07:37,720
So you see, we're just continuing to write these statements here that can be seen as true or false.

103
00:07:39,270 --> 00:07:41,250
A kind of contingent on things, you know?

104
00:07:42,970 --> 00:07:51,970
OK, so now let's break it down a little more and kind of turn it into an expression that is a little

105
00:07:51,970 --> 00:07:57,820
bit more kind of mathematical or really what it's like is a.

106
00:07:58,930 --> 00:08:01,240
Kind of simplified version of code.

107
00:08:01,690 --> 00:08:10,450
So this is something that you can call pseudocode not quite exact C++ code, but something that we can

108
00:08:10,450 --> 00:08:13,530
think of in terms of code.

109
00:08:13,540 --> 00:08:23,560
So we're trying to break a normal spoken sentence, written sentence into more of an exact kind of scientific

110
00:08:23,560 --> 00:08:24,750
definition, if you will.

111
00:08:25,880 --> 00:08:27,260
So what do we start out with?

112
00:08:28,610 --> 00:08:35,030
It's basically if Bob does not have a watermelon, right, we can simplify that with the variables and

113
00:08:35,030 --> 00:08:39,410
kind of, you know, we simplify this doesn't not as well.

114
00:08:39,410 --> 00:08:41,660
So we have this if here, right?

115
00:08:41,660 --> 00:08:47,050
We say, if not, not a so doesn't not is not not.

116
00:08:47,060 --> 00:08:51,260
And then we have this issue here in the beginning and a is Bob has a watermelon, right?

117
00:08:52,710 --> 00:08:57,600
So this is if Bob does not have watermelon is going to be, if not, not a.

118
00:08:58,650 --> 00:08:59,940
So let's look at the next part.

119
00:09:01,490 --> 00:09:06,710
So we basically have C, which is oranges are available, that's the next part, and, you know, I've

120
00:09:06,710 --> 00:09:09,920
kind of ignore I kind of left this purple.

121
00:09:11,140 --> 00:09:16,420
Thing highlighted, so don't associate what we're talking about right now, like each piece of this

122
00:09:16,420 --> 00:09:19,210
to be with the purple I kind of just by accident left that there.

123
00:09:20,140 --> 00:09:25,600
So if C C is oranges are available, right, so we have this f right here.

124
00:09:28,210 --> 00:09:33,910
So basically, we have the first air freight, so if Bob does not have a watermelon.

125
00:09:35,070 --> 00:09:42,030
And then we said he will not get all bananas if, but we also we basically have this condition right

126
00:09:42,030 --> 00:09:42,540
here.

127
00:09:44,240 --> 00:09:48,590
Or they already had it kind of be fulfilled if I didn't have a watermelon, he's definitely not going

128
00:09:48,590 --> 00:09:49,630
to get it.

129
00:09:49,640 --> 00:09:52,520
Bob doesn't not have a watermelon, he definitely is not going to.

130
00:09:54,910 --> 00:09:57,800
He is definitely not not going to get all the bananas.

131
00:09:57,820 --> 00:09:59,590
You know, he's going to get the bananas.

132
00:09:59,950 --> 00:10:07,750
So if this is fulfilled, then we can kind of look to see if this next thing is all fulfilled.

133
00:10:08,350 --> 00:10:13,120
The second, if everything that comes after the second half is fulfilled and if those odds are fulfilled,

134
00:10:13,420 --> 00:10:15,240
then this will be true.

135
00:10:15,250 --> 00:10:17,320
This whole thing right here will be true.

136
00:10:19,090 --> 00:10:19,570
So.

137
00:10:20,940 --> 00:10:21,930
That's kind of what we're doing.

138
00:10:21,990 --> 00:10:27,060
Breaking it down, so we basically look at this first thing right here, so we need to check first whether

139
00:10:27,060 --> 00:10:29,520
Bob doesn't not have a watermelon, right?

140
00:10:30,240 --> 00:10:36,510
If that gets fulfilled, if this turns out to be true, then we can look at this second condition right

141
00:10:36,510 --> 00:10:36,990
here.

142
00:10:37,110 --> 00:10:39,720
It starts with this if it is the rest of this sentence.

143
00:10:40,930 --> 00:10:47,140
And if that turns out to be true, then we can say that this whole thing I'm circling right here would

144
00:10:47,140 --> 00:10:48,100
be true, right?

145
00:10:49,270 --> 00:10:54,640
That's kind of the contingencies for Bob to not get all bananas, right?

146
00:10:55,630 --> 00:10:56,080
So.

147
00:10:57,340 --> 00:11:04,330
We're now going to continue with this, we have the AFC for it, that's if we had to if both oranges,

148
00:11:04,330 --> 00:11:08,260
but remember, we represent see with oranges are available.

149
00:11:08,260 --> 00:11:16,390
So we're saying if the oranges are available and because we have this ad right here and then we're saying

150
00:11:16,390 --> 00:11:17,080
de.

151
00:11:18,100 --> 00:11:19,680
Or E!

152
00:11:20,110 --> 00:11:22,420
Which is red apples are available.

153
00:11:22,820 --> 00:11:26,560
Or yellow apples are available, right?

154
00:11:28,290 --> 00:11:34,560
So that's why we're in this whole second thing, starting at this, if this whole second section of

155
00:11:34,560 --> 00:11:36,450
the sentence is if.

156
00:11:37,430 --> 00:11:40,250
Oranges are available and.

157
00:11:42,040 --> 00:11:49,900
Red apples are available or yellow apples are available, and this is kind of like a little subsection

158
00:11:49,900 --> 00:11:50,900
of the sentence, right?

159
00:11:50,920 --> 00:11:57,850
So we're saying if both oranges and and then we got to look at this whole thing, red or yellow apples

160
00:11:57,850 --> 00:11:58,420
are available.

161
00:11:58,420 --> 00:12:02,050
So that is saying red apples are available or yellow apples are available.

162
00:12:03,030 --> 00:12:05,400
So we simplify that and the D or E.

163
00:12:05,430 --> 00:12:08,690
So we have F C and D Ori.

164
00:12:10,070 --> 00:12:14,570
So if all of this gets fulfilled, this thing turns out to be true.

165
00:12:15,020 --> 00:12:27,050
And then this all turns out to be true, then we can say B, which is Bob gets all bananas will be false,

166
00:12:27,050 --> 00:12:27,260
right?

167
00:12:27,260 --> 00:12:32,360
Because we said he will not get all bananas if this and if this.

168
00:12:33,890 --> 00:12:40,670
So this turned out to be true, this turns out to be true, then we say this thing I'm circling is true.

169
00:12:42,080 --> 00:12:48,830
But we're saying that B is false because we called B Bob gets all bananas, but this thing that I'm

170
00:12:48,830 --> 00:12:50,720
circling is true, which has a knot in it.

171
00:12:50,870 --> 00:12:52,580
He says he will not get all the bananas.

172
00:12:52,610 --> 00:12:55,460
It is true that Bob will not get all the bananas.

173
00:12:55,460 --> 00:13:01,910
So that's why since B was Bob gets all bananas were saying B is going to be false because it's the opposite,

174
00:13:01,910 --> 00:13:02,090
right?

175
00:13:02,090 --> 00:13:05,120
We said it's true that Bob will not get all the bananas.

176
00:13:06,630 --> 00:13:10,260
So maybe a little confusing, but try and break it down for you here.

177
00:13:10,710 --> 00:13:18,210
This is kind of what we do when we encounter a word problem, a logic kind of problem and we need to

178
00:13:18,240 --> 00:13:19,590
turn it into code.

179
00:13:19,620 --> 00:13:25,770
We got to go through this process of turning it into code, and this is kind of the initial stage is

180
00:13:25,770 --> 00:13:27,450
turning it into something like this.

181
00:13:28,410 --> 00:13:35,010
After that, then you would move on to transitioning from this into actual C++ code or whatever coding

182
00:13:35,010 --> 00:13:35,820
language you're using.

183
00:13:36,330 --> 00:13:39,170
We're going to break this down further, though, because we're not quite done yet.

184
00:13:39,180 --> 00:13:45,750
I kind of just told you how to arrange this, but we need to talk about these individual components,

185
00:13:45,750 --> 00:13:47,640
like if we were to say.

186
00:13:48,630 --> 00:13:51,840
That a was true.

187
00:13:52,200 --> 00:13:53,550
Or C.

188
00:13:54,500 --> 00:14:00,710
Was false, and D was true, but E was false or something like that, you know, like rub out right?

189
00:14:00,710 --> 00:14:06,530
Apples aren't available, but yellow apples are available, but oranges aren't available and Bob does

190
00:14:06,530 --> 00:14:08,150
have a watermelon, you know, things like that.

191
00:14:08,630 --> 00:14:15,680
We need to, like, put some information into these true false kind of questions and then figure out

192
00:14:15,680 --> 00:14:17,770
what all the different answers could be.

193
00:14:17,780 --> 00:14:19,970
If we said yes, a is true.

194
00:14:20,440 --> 00:14:21,680
ABC is not true.

195
00:14:22,040 --> 00:14:23,560
D is true.

196
00:14:23,570 --> 00:14:25,700
E is not true, you know, stuff like that.

197
00:14:27,050 --> 00:14:28,190
So let's take a look at that.

198
00:14:28,700 --> 00:14:33,110
This is actually having a lot to do with Boolean values, which I know is already mentioned to you in

199
00:14:33,110 --> 00:14:35,000
the data types section.

200
00:14:35,000 --> 00:14:41,960
So if you remember the short slide on that, it did talk about how Boolean values are true and false

201
00:14:41,960 --> 00:14:42,710
values, right?

202
00:14:42,980 --> 00:14:48,890
There can be variables in your code that are true or false values, which are the same things as one

203
00:14:48,890 --> 00:14:50,840
in zero, right?

204
00:14:50,840 --> 00:14:51,260
So.

205
00:14:52,310 --> 00:14:57,980
When we rearrange these normal statements that we had like that sentence into a collection of true and

206
00:14:57,980 --> 00:15:05,600
false statements, then we're able to move to creating some code that can filter an output based on

207
00:15:05,600 --> 00:15:09,050
those if conditions when the data comes in and we say, Hey.

208
00:15:10,220 --> 00:15:14,510
A is true or B is true or, you know, C is false stuff like that.

209
00:15:15,680 --> 00:15:21,980
When we build these things like this right here, then we're able to kind of filter out a total result,

210
00:15:21,980 --> 00:15:22,340
right?

211
00:15:22,370 --> 00:15:25,580
With these different combinations of Boolean values, they come in.

212
00:15:25,580 --> 00:15:26,990
So we made variables right.

213
00:15:27,000 --> 00:15:28,940
We have a is a variable b c.

214
00:15:29,630 --> 00:15:32,270
Each one of those is a Boolean variable.

215
00:15:32,450 --> 00:15:36,170
It's a Boolean value stored inside that variable, and it's either true or false.

216
00:15:37,230 --> 00:15:42,210
And then a big, important thing to connect to it is the fact that in computer science, when we're

217
00:15:42,210 --> 00:15:49,560
talking about true, that is synonymous with one, we talk about false that is synonymous with zero.

218
00:15:49,950 --> 00:15:51,780
Same kind of thing in binary.

219
00:15:51,780 --> 00:15:54,390
Right on, off on off.

220
00:15:54,420 --> 00:15:55,100
True.

221
00:15:55,230 --> 00:15:55,900
False.

222
00:15:55,920 --> 00:15:57,210
One zero.

223
00:15:58,020 --> 00:15:59,250
Connect all those things.

224
00:15:59,250 --> 00:16:00,120
They're all the same.

225
00:16:00,120 --> 00:16:01,050
They mean the same thing.
