< Return to Video

5. QUESTIONS

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

more » « less
Video Language:
English
buch.10 edited English subtitles for 5. QUESTIONS

English subtitles

Revisions