hi, so by now if you're here
you've probably watched some videos about variables
and some videos about conditional statements
in this video we are going to somehow combine those two in an interesting new way
and we're going to look at
what it means to have a variable of type boolean, right?
what is a variable? a variable is a type like an integer,
it is a name, like we might make up the name 'x',
and we might give it an initial value
so this is a totally reasonable variable:
the integer x, that you can't exactly see
cause [inintelligible]
x equals 100
let's, this is crazy, I'm totally out of sync here
but let's make a variable now that has
a type boolean, 'going' equals false
this is the type of the variable
it is not a number, it is not a string,
it is not other types of variables,
that you may or may not have learned about,
it is a boolean. that means it only has one
possible, two possible values: true or false
you could use an integer in many ways
and have the integer either be 0, for false, or 1, for true
but there are many other numbers
that piece of data could be
in this case the piece of data can only be false or true
and if you remember, a conditional statement
is an expression : if, some expression, evaluates to true, execute some code
guess what? a boolean variable evaluates
to true or false, right?
this boolean 'going' is false, so 'if going', meaning it's not, we're not going to
execute this code because 'going' is false.
but if I change its value to true
then this code will become executed.
so, let's look at a way that we might use this in an example.
here is a very simple example,
it has a circle moving across the screen
size(), background(), an ellipse drawing at x,
x = x+2. what if I don't want this circle to start
until I click the mouse?
well, you might think I would do something like
ah, if mousePressed and then run this
and now, ah, the circle is not moving
if I click the mouse it is moving
if I let go of the mouse it stopped
but what if I just want the event
of clicking the mouse to trigger the circle
to move from now on and forever going forward?
so, to do that, what if I made up my own variable
like 'going'?
and we say: boolean going equals false
and run this code
it's not moving!
now if I change the value of 'going' to true
it's moving, because 'going' is true,
so this code is being executed.
so now all you need to do is change the value of 'going'
from false to true the moment that I want the circle start moving
when I click the mouse.
so in this case right here
let's start with 'going' as false, and then when in mousePressed, this is an event
that's triggered as soon as the user clicks the mouse
let's set: going = true, and it bothers me that you can't see all of the code here
so I'm just gonna make this fit here.
now we can see: ok, going is false
so when we run it, it's not moving
it's not moving because going is false
but now when I click the mouse
going becomes true and it moves.
ah, I'm gonna stop it! click the mouse!
ah, no matter what, when I click the mouse now going is true
what if want going to become false?
here's a neat little trick:
we have time for this,
you have time for this, I have somewhere I need to be
but I'm making this video anyway
so, in mousePressed we are saying: going equals true
but what if I want going to only become true
if it wasn't true, and if going was already true I want going now to be false
in other words I want to say:
if going ... then going equals false, right?
if going is true then set going equal to false,
otherwise set going equal to true
so: if going is true, set going to false
otherwise, if going is false, set it equal to true.
let's go implement that!
ok, so I'm down here, and I'm going to implement this.
if going, and by the way, you could have written it like this: if going == true
but that's really long-winded, going is just true or false,
and a boolean expression evaluates to true or false
so I can really just say: if (going == true) I can just say if (going)
by the way, if (going == false) is the same as saying: if (!going)
that exclamation point is NOT, is not going, my ... a little bigger here
hopefully you can read this.
ok, so remember: if it's going then going should be false
otherwise let's say going is true
let's run this now, right?
so going is false right now, so when I click the mouse
[inintelligible]
now if I click the mouse again, going is true, ah, going is set to false
oh, what a magical thing just happened!
ready? start ... stop ... start ... stop
oh, it's gonna leave the screen.
ok, but, maybe you thought this was all
there's one more piece of this,
this is fine and good and perfectly reasonable
it is rather long-winded
we as programmers, sometimes our whole life is just trying to make things shorter,
although that's not really any point of that,
you might also just make things longer,
it's good for things to be clear, readable, but anyway,
if we want to make this shorter,
let's examine this statement
going = ! going ;
this seems like it must be some sort of illegal code
that you could not possibly write
and it's self-referential and circular in some way
it's not gonna work, but in fact
this is exactly what we want in this particular case
let's think about this again
let's say x = y + z
if we have a statement like this,
remember we evaluate this side of the equation
and store the result over here
so we're going to do the same thing here
we evaluate this side of the equation
and store the result here
it's kind of like how, when you say
x = x + 1 , this seems a little strange
it equals itself plus 1?
but it's not big deal, if it were 5 then 5+1=6
and now x equals to 6, this is an operation
that increments x over and over again
5 becomes 6 becomes 7, I'm talking way too fast in this video,
but here, let's say going is true
what is not true? not true is false
let's say going is false, what is not false? true!
so going equals not itself just takes the value of going,
if it's true it turns it into false, if it's false it turns it into true!
that might give you a little bit of a headache
but I think that as you start to think about it
and write it down and think about it again
it will start to make more and more sense
so let's go back over here and let's look at what he can write here now
going equals not going, ok?
going is currently false, so as soon as I click the mouse
going is going to equal not false, which is true
and when I click the mouse again, going is going to equal not true
which is false.
so this is just a little bit of an inkling
of what it means to use a variable
with a type of boolean
it's just a little bit of a helper, it's kind of like we can store
the value of a switch in our program
we can always know is that switch on or is that switch off?
and at any point we could act upon that
if the switch is on, do this code, if it's off do this other code.
and we can also change the value of that switch
by turning it on, setting it to true, or turning it off, setting it to false.
usually at the end of these videos I give some sort of exercise
but I can't think of one right now
but what you might do is find some program that you wrote
and see where you might store a value of true or false
and act upon that. that could be something that you could do.
I'll think of something better later!
maybe put it in the notes
ok, I'm gonna stop this video
and you're going to enjoy the rest of your day
I'm over here and I'm going now.
my going is true!
ok, bye