hi, so it is time for another super, big moment
in our life of learning to program the computer
and in fact this may very well be
the biggest moment of all the moments
and i'm sure I'll just say in the next video
it's the biggest moment
but right now it really is
because we're going to start talking
about conditional statements
and it might not sound like it's the
most super, biggest moment ever
but it really is!
let's understand why this is gonna be
such a huge leap forward that we're gonna take
maybe, perhaps you don't remember where
we last left off. where we last left off
we had this nice Processing sketch,
we were learning about variables,
we declared a variable at the top,
we initialized its value to zero,
and then we manipulated the value of that variable
by incrementing it every time through draw()
which looped over and over again
and when we run the sketch
we have our circle, and it's going ...
there it is ...
it's going, going, going, it's very, very slowly
so this is like awkward now
but eventually it's gonna get to the end
and it's gonna leave,
and it's not gonna come back.
and that's sad. that's a sad moment
for the circle, for us, the program, Processing, everything.
but we will now get over this sad moment
and we're going to add something new to our
bag of tricks, or bag of tools, we're going
to be able to say:
if the ball, if the circle reaches the end of that window
do something else, change colour,
turn around, reset your position back to
somewhere else.
this is what we're going to do in this video take
so we're just gonna get started with this
so let's move over here
and let's think about what are the nuts and bolts
of what we're doing
we are writing a new kind of block of code
it is called a 'conditional statement'
I'll write this down: conditional statement
and it is going to look like this
IF ( ... this is the squeaky pen ...
I don't know where the other one is,
can I pause for a second? pause
ok, got a new pen
IF, open parentheses, close parentheses,
open curly bracket, close curly bracket,
some code ... I guess all of these pens squeak to some extent
but ... hopefully it's not bothering you like it's bothering me
ok, enough about the pen!
what do we have here?
IF parentheses, parentheses, something that's
going in the middle here
open curly bracket, close curly bracket
right, this looks familiar in some ways
this is a block of code. just like we had
setup() and draw() , setup() has a
beginning and an end with the curly brackets
draw() has a beginning and an end with curly brackets
but we are now having a block of code
what this block of code means is
IF whatever is in here is TRUE
THEN execute this code
IF NOT then don't execute this code.
so this is the moment where we now
have the ability to add some logic
to our program that let's the program take
a path. it could chose to execute some code
it could draw some stuff on the screen
or it could not draw some stuff on the screen
it could change the values of some variables
or it could not change the values of some variables
so if it is always doing this through draw()
every time through draw()
sometimes it will execute things one way,
sometimes it will execute thing a different way.
this allows our program to path different behaviors
at different moments in time.
so this simple, simple structure
is incredibly powerful and important!
so what I wanna do in this video is really just talk
about, obviously there's gonna be a whole set
of videos about conditional statements, we're
gonna look at lots of scenarios,
but what I wanna focus on in this particular video
is really just this question mark.
what goes in there?
and what goes in there is known as
a 'boolean expression'
now a boolean expression is an expression
that evaluates to only TRUE or FALSE.
for example I could say 'my name is Francis' and that would be false,
or I could say 'I am an extremely nervous person'
and that would be true!
but the thing about it is, thats a little bit ...
I should point out that's a little bit subjective, you know.
we could, in the english language, we might say
things like 'I'm feeling a little melancholy today'
and that could be sort of true, mostly true but not exactly true because I'm only a little bit
I don't know what I'm saying but the point is
the types of boolean expressions we're going to write
for our computer program are going to be
distinctly true or false. only one or the other.
no gray area whatsoever.
now there are many ways that we're going
to see that we can create a boolean expression
but the simplest way, what's going to be
most useful for us right now to start with is
using something called a 'relational operator'.
so let's make a list
of relational operators. what are relational operators?
here's one: > (greater than)
another one is: < (less than)
>= (greater than or equal)
<= (less than or equal)
and I'm gonna list a couple here more
at the bottom that are a little bit goofy
== (two equal signs)
or != (not equals) , exclamation, equal
so one of the things about programming
is sometimes syntax can be a little bit cryptic
right, if I wanna ask if something is not,
if I wanna test if a value is not equal to another value
I need to know that these symbols for that are exclamation point, equals.
so we'll see examples of this as we go
I think for right now let's just focus on
these two simple relational operators.
this will get us going.
so let's write some boolean expressions
here is a boolean expression for you:
6 > 5 (six is greater than 5)
this is an expression that evaluates to 'true' or 'false'
is this a true statement or is is a false statement?
that is definitely, most definitively true
here is another one:
717 < -10 , is that true or is that false?
that's quite obviously false, right?
there is no way for this expression to be
something other than true or false,
even if I say 7 is greater than 7
is that true or false?
that is false!
so we can see: these are the types
of boolean expressions that we can write
with these relational operators.
is one value greater than another value?
is one value less than another value?
etc
so, these boolean expressions,
these relational operators,
that must mean they go in between
the parentheses right here because this
is where you put your boolean expressions.
if 6 is greater than 5 then execute that code
if this expression evaluates to 'true' then execute this code
if this expression evaluates to 'false' then skip and keep going,
don't evaluate that code!
so now let's look at this and think to ourselves:
does it make any sense at all in any universe
to write that code?
think about that for a second.
I argue, I'm gonna make the argument
right now that this, while it's syntactically correct,
it has no logical value, right? because 6 > 5 can
never ever be anything but true, so there is no
point in having this conditional statement
cause we're always gonna execute that code
so point of writing these boolean expressions
is not to use hardcoded numbers but to use
variables, variables which have variable values
sometimes the value of that value might be
greater than 5, sometimes it might not be
greater than 5. for example, what if I were to say
'if mouseX < 5' ? so if the value of the mouseX,
if the value of the mouse's x-position is greater
than 5 then execute that code.
so only when I have my mouse over here
execute that code, if I don't have my mouse
over there then execute some other code.
think about it like if you need to program a rollover
from scratch, right? you have this rectangle
on the screen and whenever the mouse is over
that rectangle you want it to light up and change
color, that's how you're gonna do it.
if the mouseX is greater than here,
and if it's less than here
and mouseY is greater than here,
and if it's less than here,
if it's within all these bounds
then draw a different color
this is where we're going with this type of stuff
ok, so, I think this kinda gives us the pieces,
the beginning pieces, of what the form of a
conditional statement looks like,
what a boolean expression is, what's a
basic boolean expressions are using relational operators
let's just look at one, tiny little example
in code and then we'll move onto the next video.
so let's put aside our bouncing ball, the program
we're hoping to eventually turn into a
bouncing ball sketch, for a moment and
let's do something quite simple:
so here we have a Processing sketch that does nothing
but draw a gray background.
size(), and background() in draw()
let's add some new code in here!
let's add this conditional statement:
if mouseX is greater than, pick a value, 200,
then I'm gonna say: background, pick a color,
any color, green, uh, red, a bit yellowish, whatever
here's another color
we're gonna run this sketch
look, gray background, look there's a line of code
in there background some other color,
but we're not seeing that!
it is not happening.
why? because mouseX is currently not greater
than 200. what is the value of mouseX?
I don't know, maybe it's about 20, 30, ...
it's an exciting moment, it's about to happen
50,100, 150, .. ah! there it is! it's orange.
that's a nice color, I like orange.
so you can see: wooh, it's not, wooh
so we can see pixel 200 is right about there
and we could be a little bit better about this
we could do something like, say,
stroke(255); draw a line at pixel 200, 0, ...
so that I could at least see that hey, whenever
the mouse is on this side it's grey, whenever
the mouse is on this side it's orange.
so, you know, this is like quite possibly
the most simple, a very, very, very simple
beginning of conditionals but I would venture to say
for a moment that one of the amazing things about programming
is all you really need is data, variables
and conditionals to make different choices
we're gonna look at lots of stuff
loops, and functions, and objects and all
sorts of other fundamentals of computation
but I would suggest that even with just the ...
I've just shown you so far, that you can have variables
and you can have conditional statement,
there's nothing you could not program right now.
nothing! everything else is just gonna be
about helping making your code shorter,
more organized, more efficient, obviously you
really wanna have a loop, otherwise you will have
to have thousands of lines of code, but
really this is sort of an important moment
to realize that you now have all the tools
to make anything you can imagine!
so you should really start giving this a try!
so if you're looking for a little exercise
to try before you get to the next video,
what I would suggest now is think about
this program: could you write a conditional
statement that executes this task:
if the circle reaches the right-hand edge of the
window, reset its location back to the left-hand edge.
now I stated that in a very english language kind
of way, I could state it more like code, more like
pseudo-code, more like code, and eventually
write the code for it. but think about that,
where would you put that conditional statement?
can you get that to work?
a very simple exercise, and we'll move on
in the next video and do more with this stuff.
ok, ... I wanted to say something profound
at the end but there is nothing profound other than
I am staring into the abyss of an empty camera
and I bid you adieu!