hello, ok, so here's the thing
if you watched the previous video
you will have noticed it was a complete disaster!
so what I'm gonna try to do with this one
is make this really short
just talk about one simple topic
it's gonna be like, I'm gonna turn the clock on
clock is now going, I can see it
I'm gonna try to keep this to 3 or 4 minutes
simple topic, we're gonna discuss it
we're gonna make one quick example
the video will be over
we're not gonna lose the train of thought
we're gonna keep the energy going
and you're gonna really, it's going to be great, ok?
ok, here we go!
today's topic for this video is logical operators
so what have we just be doing?
in the previous video I actually made this example
but I talked for so long the cam just recorded for twelve minutes
and then cut off
but I got to this point where we made this ... example where what we're saying is:
aha, if ... now x is greater than 500 draw a red background
400 yellow background
let's see where we're going here
ah, wooh, pink, that's beautiful
pink background
if 300 blue, 200 ... and in all other cases: green.
so we learned about how we can have an if statement:
when the condition is met, execute that code.
when can join in a statement with 'else if'
saying if this is true, otherwise if this is true
and 'else' tells us in all the other cases to do this.
and I probably don't need to recap the previous video
because in most cases you'll just have watched it
but I'm gonna figure that out eventually
it will make sense and I'll just redo all of these.
ok, but there's one more piece.
so this is actually everything you need to know
to design the logic of your program
is right here in this example
IF, ELSE IF and ELSE
you basically, with variables and conditionals
you can program absolutely anything!
you know, we're gonna introduce
a lot of things to make this better
- and it's already a minute and a half, and I say three minutes -
so I'm gonna get back to this.
logical operators, what we're talking about
when we talk about logical operators
are AND and OR, because sometimes we wanna say things like
IF I'm hungry AND it's evening, have dinner.
IF I'm hungry AND it's morning, have breakfast.
right? so we want to be able to join
two boolean expressions. if they're both true, do this!
if only one of them is true, do this!
if only one of them is true that's OR.
by the way, there's something called 'exclusive or'
- but I don't know why I bring this up right now -
but it is kind of interesting
'exclusive or' means 'they can't both be true',
just one of them true. maybe you need that for something but let's
just not worry about that right now.
this has nothing to do with anything - unintelligble .
ok, it's like talking about 'double buffering' (?) or something like that.
here we go!
AND and OR.
how do we ... that?
so what if I have a boolean expression
and I wanna know if this boolean expression
is true and this boolean expression is true
then execute the code that's in here.
well, you might be thinking: oh, you could just write the word AND there.
that might be nice, that would sort of make sense
it would look right and it seems to match
the logic that we've been talking about.
the thing about Processing is
it's built on this language called Java,
which in many ways ... similar syntax
than the programming language C
and the logical AND is written with two ampersands (&&)
- I am very good at drawing ampersands -
which is actually not true at all, as you can see with this horrible ampersand
this is an AND
so IF this expression is true AND this expression is true,
the ampersands joins those,
sometimes because I like to be a little bit anal about stuff
you can put parentheses around the whole thing too
because truly this whole thing is one boolean expression
the whole thing evaluates to either true or false
it only happens to evaluate to true
when both of these sub-boolean expressions
also evaluate to true.
so what if I wrote something like this:
if mouseX is greater than 100
AND mouseX is less than 50
execute this code
what's wrong with this?
it's completely illogical!
I mean it'll work but it is impossible.
try, I dare you, try to come up with a number
that is both greater than 200 and smaller than 50!
take your time. as long as you need.
get back to me whenever, I will do some sort of exciting thing
like eat a cupcake, dance on top of a small ... stand while wearing
a pretty outfit or something, I can't come up with anything right now.
if you can come up with that number, but it's not possible.
so just because you have these options
doesn't mean that you automatically
gonna write all this great code
that's gonna do this stuff.
it's still up to you to come up with that logic
to implement what you want.
let's look at a scenario where this AND ampersand can make sense
so, we wanna say:
if - we're gonna get rid of all this stuff -
if mouseX is greater than 100 AND if mouseX is less than 200
and let's put those, I think it's useful, I shouldn't have gotten rid of it,
to draw lines on the screen in this case
just so we can see them
and know where the mouse is in relation to those numbers
ok, so what's the problem?
look at me, it's good that this happened
'unexpected token ampersand'
you can't really see that
there's a way to zoom
oh, how do I zoom?
terrible pause, look up something on Google!
Ok, I gotta figure that out
I know how to do it on my laptop
but this is like a regular keyboard
so down here, you can't read it, at the bottom it says:
'unexpected token &'
so something must be wrong with the syntax back here
but ... we wrote it here, we put these parentheses around here
so it's a little bit goofy
I mean, I'm just gonna show you
if I took these parentheses out
it's gonna work
so this is gonna be fine. remember you need to have parentheses
that are going around the entire boolean expression
the way I had it before, like this, there were no parentheses
going around the entire boolean expression
this will also work because we can have parentheses around the other
boolean expressions. this, I would say, is probably a good way to write it
just to be most clear right now.
so if mouseX is greater than 100 and mouseX is less than 200
tada tada taaaa
ok, over here we got a black background,
over here we got a black background,
and inbetween we've got red, inbetween, because here
is where the value is both greater than 100
and less than 200. Six minutes and counting ... ok.
Ok, we've got to talk about OR.
I mean, how can I not make a video about AND and OR and have it just ...
it could be done in three minutes, I don't know what going on
gonna work on it. Ok: OR. The thing about this stuff
is what's nice about these boolean expressions and this logical stuff
it really just follows your intuition
I mean, you don't have to explain what AND means
just try to say it in English: if mouseX is greater than 100
and less than 200, then execute this code.
it works out. what if, ... in this case OR is written in an even more
cryptic way: OR is written with two pipes.
that is OR. Okay?
so I could say: if the mouseX is greater than 100
OR it is less than 200 then what's gonna happen?
no matter what I do it's always red! why?
try to come up with a number that's neither greater than 100 nor less than 200!
every number, 300 is greater than 100, 50 happens to be less than 200
150 is greater than 100 and less than 200
so this is always true!
both together with an OR , at least one of them is going to be true
but now we have to case, what if we say: if it's greater than 200
OR less than 100? what's gonna happen now?
it's greater than 200, we have red
it's less than 100, it's red
but now if it's here, neither of those expressions evaluates to true
it's neither greater than 200 nor less than 100, and we've got
a black background now in the middle.
this brings up an interesting point. there is another logical operator:
it is called NOT. Right?
if x is greater than 100, do something.
what if I was saying: if x is NOT greater than 100, do something?
this is very, very useful!
oftentimes you want to sort of do the inverse.
so I could say right here: if this entire boolean expression
is not true, and that's not the right syntax ...
.... probably put another set of parentheses around
you know, I don't know what's wrong with me today
I just completely lost my mind but, uhm, this is the complete opposite
so now if, we can certainly the crazy, by the way, we've just gone off
the deep end here, this is a nice little demonstration but the point of this
was to kind of understand OR, AND and NOT.
so you can use these in all kinds of combinations
you can say: if this and this or this but not this
you can create these very elaborate boolean expressions.
for the most part it is very hard to do that in a vacuum, we're coming up with
these arbitrary scenarios, what you wanna do now
is figure out where is this going to be useful to you
and I think our next video, where we will finally return to that moving ball
is really going to demonstrate that, and you're ready for this now.
how could you say, for example, with these structures:
we're gonna do this in the next video
if you have a ball moving over here, if it reaches this edge,
or if it reaches the other edge, how to turn around?
that's one place where OR would make sense.
that's, it's really, you know, it'd be much easier
if you were just standing here and said: oh, I have this idea, what kind of
logical expression do I need? well, then we would kind of
work that through. but with the absence of an idea we're just kind of
spinning in the wind. so, maybe if I make some of these videos in the future
I've got more specific examples. this is practically 10 minutes!!