< Return to Video

Flowchart for the Factorial Program

  • 0:00 - 0:03
    One thing you'll hear or see referred to a lot
  • 0:03 - 0:05
    is something called a flowchart
  • 0:05 - 0:06
    either in computer science,
  • 0:06 - 0:07
    so really when people talk about
  • 0:07 - 0:09
    any type of algorithm or process.
  • 0:09 - 0:11
    So what I thought I would do is
  • 0:11 - 0:12
    for every program I write—
  • 0:12 - 0:13
    is I'll do a simple flowchart.
  • 0:13 - 0:14
    Maybe I won't do it for every one,
  • 0:14 - 0:16
    but I'll start with some of the basic ones.
  • 0:16 - 0:17
    Just so we understand that
  • 0:17 - 0:19
    a flowchart really isn't anything fancy.
  • 0:19 - 0:21
    So in this factorial program
  • 0:21 - 0:22
    that we had been looking at
  • 0:22 - 0:24
    in the last couple of videos—
  • 0:24 - 0:27
    any flowchart you're just going to be a kind of starting point
  • 0:27 - 0:30
    which is really started, I guess,
  • 0:30 - 0:32
    depicted with a circle or an oval of some kind.
  • 0:32 - 0:34
    So we start, you can kind of view as
  • 0:34 - 0:35
    we starting at this line
  • 0:35 - 0:37
    above this assignment of the number
  • 0:37 - 0:40
    where we actually ask for input from the user.
  • 0:40 - 0:43
    And then, after that,
  • 0:43 - 0:45
    we ask for input from the user.
  • 0:45 - 0:46
    This line right here.
  • 0:46 - 0:49
    We are asking for input from the user.
  • 0:49 - 0:51
    So this right over here.
  • 0:51 - 0:55
    And we will depict that by a parallelogram.
  • 0:55 - 0:57
    So that is input from the user,
  • 0:57 - 0:58
    and then we could just say
  • 0:58 - 1:04
    "number equals user input".
  • 1:04 - 1:05
    And you can be a little bit loosey-goosey
  • 1:05 - 1:06
    with the terminology here,
  • 1:06 - 1:07
    you're really just trying to say
  • 1:07 - 1:08
    what you're doing in this step.
  • 1:08 - 1:10
    The parallellogram tells us that
  • 1:10 - 1:12
    we are somehow interfacing with the user.
  • 1:12 - 1:14
    We're either taking something from the user
  • 1:14 - 1:16
    or we're outputting something to the user.
  • 1:16 - 1:19
    So here, we're saying user input is equal to number.
  • 1:19 - 1:22
    We're assigning number as the user input.
  • 1:22 - 1:26
    Then, the next thing we do is just a straight up operation.
  • 1:26 - 1:30
    We just set product to be equal to 1, so we just set.
  • 1:30 - 1:33
    And there we just do that in a rectangle,
  • 1:33 - 1:36
    "product equals 1",
  • 1:36 - 1:40
    product is equal to 1.
  • 1:40 - 1:43
    And then, something interesting is going to happen.
  • 1:43 - 1:47
    We enter into our for loop.
  • 1:47 - 1:48
    And we start with—
  • 1:48 - 1:49
    and what I'm going to do here is
  • 1:49 - 1:50
    I'm going to write it a little bit different
  • 1:50 - 1:52
    than we wrote it over here—
  • 1:52 - 1:55
    so we do is we start a—
  • 1:55 - 1:56
    what we are doing is
  • 1:56 - 1:58
    we are going to start our for loop
  • 1:58 - 2:00
    essentially with an assignment
  • 2:00 - 2:01
    where we assign—
  • 2:01 - 2:03
    for loop is really a bunch of things
  • 2:03 - 2:04
    happening at the same time.
  • 2:04 - 2:08
    It assigns i to the first number in the sequence
  • 2:08 - 2:10
    or it tests whether it can assign i
  • 2:10 - 2:11
    to the first number in the sequence.
  • 2:11 - 2:12
    If it can,
  • 2:12 - 2:13
    then it proceeds.
  • 2:13 - 2:14
    If it can't,
  • 2:14 - 2:16
    then it breaks out of the for loop.
  • 2:16 - 2:19
    So let me put it this way.
  • 2:19 - 2:23
    I will do kind of a test case right over here.
  • 2:23 - 2:32
    So "items left in sequence".
  • 2:32 - 2:33
    And when I'm referring to the sequence
  • 2:33 - 2:34
    I'm talking about this sequence here—
  • 2:34 - 2:36
    the range of our number.
  • 2:36 - 2:38
    In the example i gave in the last video
  • 2:38 - 2:39
    the number was 3.
  • 2:39 - 2:41
    So items left in this sequence.
  • 2:41 - 2:44
    I know it's hard to read right over here.
  • 2:44 - 2:45
    If there are—so lets say that
  • 2:45 - 2:47
    there are items left in the sequence.
  • 2:47 - 2:50
    So if there are items left in the sequence.
  • 2:50 - 2:52
    So we'll just say
  • 2:52 - 2:53
    "i is equal to next item"
  • 2:53 - 2:54
    i is equal to next...
  • 2:54 - 2:58
    i is euqal to the next item.
  • 2:58 - 2:59
    If there are—
  • 2:59 - 3:01
    well, i'll just hold off for the case
  • 3:01 - 3:03
    where there are no items left in the sequence—
  • 3:03 - 3:05
    but let's say that there are.
  • 3:05 - 3:06
    "i is equal to the next item"
  • 3:06 - 3:08
    And then we also define—
  • 3:08 - 3:10
    we define product
  • 3:10 - 3:11
    to be
  • 3:11 - 3:13
    we define product
  • 3:13 - 3:15
    actually I want to do these in blue
  • 3:15 - 3:17
    just our regular...
  • 3:17 - 3:19
    Se let me write this
  • 3:19 - 3:20
    is "YES".
  • 3:20 - 3:22
    If there are items in the sequence,
  • 3:22 - 3:28
    then "i is equal to the next item"
  • 3:28 - 3:30
    that's one operation we do.
  • 3:30 - 3:32
    And then we reset product—
  • 3:32 - 3:36
    we reassign product to be equal to
  • 3:36 - 3:38
    what the former value of the product was
  • 3:38 - 3:47
    times i + 1.
  • 3:47 - 3:52
    And at this point, we essentially—
  • 3:52 - 3:54
    we loop back to this test over here.
  • 3:54 - 3:57
    So at this point, we loop back—
  • 3:57 - 3:59
    I'll try to write it neatly—
  • 3:59 - 4:00
    we loop back to—
  • 4:00 - 4:02
    let me do it right over here—
  • 4:02 - 4:05
    we loop back to this point in the program.
  • 4:05 - 4:07
    That's why it is called a loop.
  • 4:07 - 4:11
    Because after you preform this operation—
  • 4:11 - 4:12
    this is embedded in the loop—
  • 4:12 - 4:14
    you go back to the beginning
  • 4:14 - 4:16
    to see if there is any more of the loop to do.
  • 4:16 - 4:19
    So you go back to the beginning before the loop
  • 4:19 - 4:22
    and you say "Hey, are there any items in the sequence?"
  • 4:22 - 4:24
    and this should be a question mark over here.
  • 4:24 - 4:26
    This kind of question mark decision points
  • 4:26 - 4:28
    are usually specified with this diamond.
  • 4:28 - 4:30
    If there is another item in the sequence
  • 4:30 - 4:32
    then i is equal to the next item
  • 4:32 - 4:33
    and product is equal to
  • 4:33 - 4:34
    what product was times i + 1.
  • 4:34 - 4:35
    So we go to the next
  • 4:35 - 4:37
    "Are there items left in the sequence?"
  • 4:37 - 4:38
    And at some point
  • 4:38 - 4:40
    there won't be any items left in the sequence.
  • 4:40 - 4:41
    So there won't be—
  • 4:41 - 4:42
    at some point there won't be
  • 4:42 - 4:44
    any items left in the sequence.
  • 4:44 - 4:45
    And we could go to the right
  • 4:45 - 4:50
    or I'll just break out of it down here.
  • 4:50 - 4:53
    And now we've broken out of this for loop.
  • 4:53 - 4:55
    We've boken out of this for loop.
  • 4:55 - 4:57
    And then, the next thing we do
  • 4:57 - 4:58
    once we're done with our for loop
  • 4:58 - 5:01
    is we just print the value of product.
  • 5:01 - 5:03
    We just output the value of product.
  • 5:03 - 5:05
    So that is an interaction with the user.
  • 5:05 - 5:08
    So we are literally just outputting the value of product.
  • 5:08 - 5:10
    So we output—
  • 5:10 - 5:13
    "output product"
  • 5:13 - 5:16
    and we are done!
  • 5:16 - 5:17
    And I can write "end"
  • 5:17 - 5:20
    for the end of our program,
  • 5:20 - 5:22
    and the program will stop running.
  • 5:22 - 5:23
    "end".
  • 5:23 - 5:25
    So this is just a simple flowchart for this simple program.
  • 5:25 - 5:27
    And hopefully it helps you appreciate that
  • 5:27 - 5:29
    one little understanding of the program itself
  • 5:29 - 5:31
    if the last two videos didn't help too much,
  • 5:31 - 5:33
    and also gives you a simple understanding
  • 5:33 - 5:36
    of how to write a flowchart.
Title:
Flowchart for the Factorial Program
Description:

Flowchart for the factorial program.

more » « less
Video Language:
English
Duration:
05:36

English subtitles

Revisions Compare revisions