OK.
Hello again, everybody.
So in this lecture, we are going to go over something called logic.
You may have heard of this before depends on kind of what context you might have heard it in.
But it definitely is something that applies to programming, but definitely more than just programming.
Logic isn't necessarily just a programming thing.
It can apply to philosophy and all kinds of stuff.
You know, sometimes just general discussions between people can involve or not involve logic.
So it's more than just programming, but very important for programming.
So that's why we're going over it.
So like I said, you most likely have heard of logic.
Some of you might have referred to logic is something more akin to just using common sense.
An example here is kind of a simple one, but you know, if all dogs have a keen sense of smell.
And my pet is a dog, then my pet has a keen sense of smell.
You can kind of just deduce that from these previous lines here, since all the dogs have a keen sense
of smell.
You know you have your pet as a dog, then you go back and you say, Well, this is a dog.
I just now found out that they have a keen sense of smell, so that means that this one dog, that's
my pet, must also have a keen sense of smell.
Something that doesn't probably doesn't need explanation, you're probably like, why?
Why do we care about this?
This is obvious.
Sometimes the logic can get a little more complex.
This is an incredibly complex, but let's look at another example.
If Bob doesn't not have a watermelon, he will not get all bananas.
If both oranges and red or yellow apples are available, so this is a little more confusing to immediately
be able to understand what is going on in this statement and something to relate this to programming.
You could kind of replace the fruit in this sentence here with data, and it would be closely related
to something that you might see in your code.
So when you're having to write some programming problems, you might see something like this where instead
of fruit, we're dealing with some type of input data or something like that, and we need to filter
it out by using logic and turning that logic into code.
So thankfully, there's ways that we can represent this and some rules that we can follow to make things
a little simpler for us.
So let's go ahead and try to analyze this statement.
This is going to be slightly similar to when you decipher a word problems in math.
So when you do that, you sometimes look for the important things in the word problem.
And then you kind of go from there, you break it down and put it in simpler terms, more mathematical
terms.
We're going to do that.
But it doesn't involve a ton of math, really.
We're just going to kind of look at it and pick out words that have meaning for us as programmers.
So they are colored here and have color coded the ones that are related, so we have an if so, that
is a condition, right?
So we're saying if Bob something also here, we're saying if both something.
So we need to consider that, and then we have this doesn't and not.
These are essentially the same things as kind of like not not right.
If you were to simplify this and be like, well, doesn't is basically does not.
So it's basically like another not.
And we also have another not here.
Also interesting is the and you know, so you got to think about when someone says this thing and that
thing rather than just one thing.
And then we also have an order here, you know, or implies this or implies either or.
So red or yellow.
So these are the important things that we need to look at their code here.
So let's break it down a little bit further.
Let's look at the words that we didn't color.
So and kind of put them in some arbitrary terms, like some variables that we can actually use to simplify
this and put it in a more kind of a mathematical definition.
But, you know, don't freak out like it's going to be some kind of crazy equation or something.
We're just going to try and represent all these words with variables so we can shorten this and not
have to like think about all these different words, cause it didn't really matter whether it's watermelon
or bananas or like, you know.
You know, yeah, apples, whether they're available, it's just like, we can simplify this into a
much more simple yes and no things.
So that's what we're going to do.
We're actually going to look at turning these into something that can be either be true or false and
represent it with a variable.
So this first one right here, Bob, have a watermelon.
So I'm just going to turn and something that barely makes more sense, which is Bob has watermelon,
should be a bob, has a watermelon, but I'm just putting Bob has watermelon, so that's something that
can be true or false, right?
Either Bob has a watermelon or Bob does not have a watermelon.
We're just going to say there's this statement that can be said about it.
Bob has a watermelon that is either true or it's false, and we're going to represent that with A..
Let's move on to the next one.
So.
He will get all bananas, so that's Bob, right?
So Bob gets all bananas, we're going to represent that with B.
And let's think about this whole sentence now.
So this is kind of the thing that we're interested in, right?
If you look at this sentence.
So if Bob.
Blah blah blah.
And over here, if blah blah blah, then Bob gets all bananas basically, right?
So these other parts of this sentence are kind of the conditions, and this is the result that's contingent
upon those conditions, right?
So Bob will not get all the bananas if this and if this part right here.
So this is kind of the result, we're still going to represent it with a variable and we'll see how
it plays into it.
So that's going to be be Bob getting all bananas.
Then we have this next part, so.
Orange is right, if it says if both oranges and love are available, right, so the oranges are available,
that's another statement that we can take out of this.
All of these things right here, these fruits are kind of having to do with the fact that they are available,
right?
So we're going to put a see for oranges are available.
Same thing with the apples, right?
And this is specifically we have red or yellow apples, so I'm going to say D is red apples are available
just like the oranges.
And then also for the yellow yellow apples are available.
So you see, we're just continuing to write these statements here that can be seen as true or false.
A kind of contingent on things, you know?
OK, so now let's break it down a little more and kind of turn it into an expression that is a little
bit more kind of mathematical or really what it's like is a.
Kind of simplified version of code.
So this is something that you can call pseudocode not quite exact C++ code, but something that we can
think of in terms of code.
So we're trying to break a normal spoken sentence, written sentence into more of an exact kind of scientific
definition, if you will.
So what do we start out with?
It's basically if Bob does not have a watermelon, right, we can simplify that with the variables and
kind of, you know, we simplify this doesn't not as well.
So we have this if here, right?
We say, if not, not a so doesn't not is not not.
And then we have this issue here in the beginning and a is Bob has a watermelon, right?
So this is if Bob does not have watermelon is going to be, if not, not a.
So let's look at the next part.
So we basically have C, which is oranges are available, that's the next part, and, you know, I've
kind of ignore I kind of left this purple.
Thing highlighted, so don't associate what we're talking about right now, like each piece of this
to be with the purple I kind of just by accident left that there.
So if C C is oranges are available, right, so we have this f right here.
So basically, we have the first air freight, so if Bob does not have a watermelon.
And then we said he will not get all bananas if, but we also we basically have this condition right
here.
Or they already had it kind of be fulfilled if I didn't have a watermelon, he's definitely not going
to get it.
Bob doesn't not have a watermelon, he definitely is not going to.
He is definitely not not going to get all the bananas.
You know, he's going to get the bananas.
So if this is fulfilled, then we can kind of look to see if this next thing is all fulfilled.
The second, if everything that comes after the second half is fulfilled and if those odds are fulfilled,
then this will be true.
This whole thing right here will be true.
So.
That's kind of what we're doing.
Breaking it down, so we basically look at this first thing right here, so we need to check first whether
Bob doesn't not have a watermelon, right?
If that gets fulfilled, if this turns out to be true, then we can look at this second condition right
here.
It starts with this if it is the rest of this sentence.
And if that turns out to be true, then we can say that this whole thing I'm circling right here would
be true, right?
That's kind of the contingencies for Bob to not get all bananas, right?
So.
We're now going to continue with this, we have the AFC for it, that's if we had to if both oranges,
but remember, we represent see with oranges are available.
So we're saying if the oranges are available and because we have this ad right here and then we're saying
de.
Or E!
Which is red apples are available.
Or yellow apples are available, right?
So that's why we're in this whole second thing, starting at this, if this whole second section of
the sentence is if.
Oranges are available and.
Red apples are available or yellow apples are available, and this is kind of like a little subsection
of the sentence, right?
So we're saying if both oranges and and then we got to look at this whole thing, red or yellow apples
are available.
So that is saying red apples are available or yellow apples are available.
So we simplify that and the D or E.
So we have F C and D Ori.
So if all of this gets fulfilled, this thing turns out to be true.
And then this all turns out to be true, then we can say B, which is Bob gets all bananas will be false,
right?
Because we said he will not get all bananas if this and if this.
So this turned out to be true, this turns out to be true, then we say this thing I'm circling is true.
But we're saying that B is false because we called B Bob gets all bananas, but this thing that I'm
circling is true, which has a knot in it.
He says he will not get all the bananas.
It is true that Bob will not get all the bananas.
So that's why since B was Bob gets all bananas were saying B is going to be false because it's the opposite,
right?
We said it's true that Bob will not get all the bananas.
So maybe a little confusing, but try and break it down for you here.
This is kind of what we do when we encounter a word problem, a logic kind of problem and we need to
turn it into code.
We got to go through this process of turning it into code, and this is kind of the initial stage is
turning it into something like this.
After that, then you would move on to transitioning from this into actual C++ code or whatever coding
language you're using.
We're going to break this down further, though, because we're not quite done yet.
I kind of just told you how to arrange this, but we need to talk about these individual components,
like if we were to say.
That a was true.
Or C.
Was false, and D was true, but E was false or something like that, you know, like rub out right?
Apples aren't available, but yellow apples are available, but oranges aren't available and Bob does
have a watermelon, you know, things like that.
We need to, like, put some information into these true false kind of questions and then figure out
what all the different answers could be.
If we said yes, a is true.
ABC is not true.
D is true.
E is not true, you know, stuff like that.
So let's take a look at that.
This is actually having a lot to do with Boolean values, which I know is already mentioned to you in
the data types section.
So if you remember the short slide on that, it did talk about how Boolean values are true and false
values, right?
There can be variables in your code that are true or false values, which are the same things as one
in zero, right?
So.
When we rearrange these normal statements that we had like that sentence into a collection of true and
false statements, then we're able to move to creating some code that can filter an output based on
those if conditions when the data comes in and we say, Hey.
A is true or B is true or, you know, C is false stuff like that.
When we build these things like this right here, then we're able to kind of filter out a total result,
right?
With these different combinations of Boolean values, they come in.
So we made variables right.
We have a is a variable b c.
Each one of those is a Boolean variable.
It's a Boolean value stored inside that variable, and it's either true or false.
And then a big, important thing to connect to it is the fact that in computer science, when we're
talking about true, that is synonymous with one, we talk about false that is synonymous with zero.
Same kind of thing in binary.
Right on, off on off.
True.
False.
One zero.
Connect all those things.
They're all the same.
They mean the same thing.