Welcome back!
We've got one more tutorial here
in this hour of learning to program with Processing.
So, one of the things you might have noticed
is that our programs, while they animate now,
they kind of just do the same thing always.
But what if, for example, you wanted your program
to sometimes draw circles on the screen
and other times draw squares on the screen.
Computer programs can take a different path.
They can sometimes do one thing
and they can sometimes do other things.
And the way that they do this is by answering a question.
If the answer to this question is "yes"
draw a square.
If the answer to this question is "no"
draw a circle.
A yes or no question is often sometimes
referred to as a true or false question. Right?
We can think of like taking a true or false test.
And if the answer is true, draw the square.
If the answer is false, draw the circle.
So we need to figure out how to make our programs
take different paths.
What might be a question that we would ask,
for example? We could say, "Hey! Is the mouse pressed?"
If the mouse is pressed, do draw that square.
If the mouse is not pressed, draw that circle.
And this can get more and more advanced
in more advanced programs.
And you can think of a game --
If these two objects run into each other,
make a nice little pretty heart appear.
I don't know. That might be a nice thing.
Whatever.
The point is, we can have,
based on certain things that happen,
based on questions that have the answer
yes or no,
other things can happen.
And our program can take a different path.
So let's look at how we do this.
By the way, this is known as
a conditional statement.
This is one of the fundamental
building blocks of programming.
Writing conditional statements.
And the syntax of what we're actually going to write,
it looks like this.
If, parentheses, some empty space,
another curly bracket,
and another closed curly bracket.
If, we could ask a question.
Is it raining outside?
If the answer is yes, execute some code
that goes here.
What is the code that might go here?
Carry your umbrella.
If the answer to that question is no,
we're not going to do this code.
We're going to go and find out
whatever code is there --
drawing rectangles, ellipses.
So, this is how we can have a program
answer a yes or no question.
A true or false question.
And what, one of the things we're going to look at,
as our example of this
is saying, if mousePressed.
mousePressed is a word that Processing knows.
It's like asking the question
"Is the mouse pressed?"
This word is true when the mouse is pressed
and it is false when the mouse is not pressed.
So, if I were to write
rectangle here
then only when the mouse is pressed
would I draw a rectangle.
Okay. Let's take a look at that below.
So I'm going to make a basic example below
where there is a circle on the screen.
It's not doing much but there's a
circle on the screen.
Now, I'm going to add to this -- the code below
this "if" statement, this conditional statement
with a rectangle drawn when
the mouse is pressed.
Now I'm going to run that code.
Go and click in the window over here.
What happens when you click?
Only when you hold that mouse down
the rectangle appears.
As soon as you let the mouse up,
the rectangle disappears.
This is the power of conditional statements.
You allow your program to sometimes
do something one way and
sometimes do something another way.
Okay. There's a bit more.
We're almost done but there's a bit more
to that than this.
What if we want the program to draw nothing?
But draw a rectangle when the mouse is pressed.
Otherwise draw an ellipse.
The word we use in computer programming
is not otherwise, it's else.
Else, and another curly bracket,
and another curly bracket
and we can put ellipse here.
So when the answer to the question
mousePressed is yes,
or true,
we're going to execute this code.
When the answer is no,
or false,
we're going to execute this code.
This allows us to have multiple permutations
of different scenarios.
And we can add as many of these
if statements to our program.
We can have a series of three or four of them.
Let's revise that example now and add an else.
So the ellipse is going to be drawn --
or the rectangle is going to be drawn
when the mouse is pressed, else,
otherwise an ellipse is going to be drawn.
So now try clicking in the window over there.
If you're clicking I see a rectangle, right?
If you let go I see an ellipse.
It's one or the other.
I've already forgotten.
But I think you get the idea.
So I'm going to load our painting program
that we finished with at the end of our
third video tutorial, below.
Now, if you paint in the window,
you can paint as long as you want.
Make all sorts of wonderful patterns
and draw your name,
and smiley faces and rainbows
whatever you want. It's never going to erase.
What if we said,
If the mouse is pressed, draw the background.
So only when you're holding down the mouse,
the background is drawn,
effectively erasing everything that you've painted.
We now have a program
that as you move the mouse in the window,
it draws.
When you click the mouse, it erases.
We could also do the opposite.
What if you only draw when you're
moving the mouse?
So you have to hold down the mouse,
draw, you could let go, move over here,
draw some more.
That might be something you choose
to try to implement as an exercise.
Okay. So this wraps up this last video tutorial
about learning to program with Processing.
We've done actually quite a bit of stuff.
We've learned about coordinate systems.
shapes. We've learned about color, gray-scale,
and RGB. We've learned about the flow of a program.
How it has a set-up and a draw
and loops over time and things can animate
and move with the mouse.
And now we've learned about how a program
can create different paths and do something
different based on whether the mouse
is clicked or not.
So this is a time where you can spend
a little moment to explore this idea of
the conditional statement.
If you go back to your painting program
you could try adding something where
it's painting one way when you're
holding down the mouse,
and painting a different way when you're
not holding down the mouse.
You could try clearing the background
in the conditional statement,
all sorts of possible things you could try.
So work with conditional statements
in this exercise.
I'm going to leave you with a simple
painting program that uses
a conditional, below.
But you can always revert back to your code
from the last time,
just as you always have.
So, take a little time to do this exercise,
when you're done, click next
and it will be one last wrap up video.
We'll kind of say goodbye, and I'll also
show you how you can share your code
and save it so that you can come back
and look at it again later,
as well as look for resources,
for how you might, hopefully
if you're excited and interested in this,
there's lots more to learn. Right?
This is just the beginning.
We've just scratched the surface.
And there's more resources online,
through books, and other websites
that will help you continue this process.
Okay? So, see you after you
complete this exercise.