< Return to Video

Flowchart for the Factorial Program

  • 0:01 - 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:07
    either in computer science, so really when people
  • 0:07 - 0:10
    talk about any type of algorithm or process.
  • 0:10 - 0:11
    So what I thought I would do
  • 0:11 - 0:13
    is for every program I write, is 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 one of the basic ones,
  • 0:16 - 0:19
    just so we understand that a flowchart isn't anything fancy.
  • 0:19 - 0:23
    So in this factorial program that we had been looking at in the past couple videos,
  • 0:23 - 0:27
    any flowchart you're just going to be at a starting point
  • 0:27 - 0:32
    I guess, depicted with a circle or oval of some kind.
  • 0:32 - 0:37
    And so we start at this line above this assignment
  • 0:37 - 0:40
    of the number where we actually ask for input from the user.
  • 0:40 - 0:45
    And then after that, we ask for input from
  • 0:45 - 0:46
    the user. 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
    And that is input from the user.
  • 0:57 - 1:00
    And then we'll just say number
  • 1:00 - 1:03
    equals user input.
  • 1:03 - 1:06
    And you can be a little bit loosey-goosey with the terminology
  • 1:06 - 1:09
    here, you're really just trying to tell us what you're doing in this step.
  • 1:09 - 1:12
    The parallellogram tells us that we are somehow interfacing with the user.
  • 1:12 - 1:17
    We're either taking something from the user or we're outputting something to the user.
  • 1:17 - 1:19
    So here, we're saying user input is equal to number.
  • 1:19 - 1:22
    We're assigning number to 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
  • 1:30 - 1:33
    And there we just do that in a rectangle.
  • 1:33 - 1:36
    Product equals 1.
  • 1:36 - 1:41
    Product is equal to one.
  • 1:41 - 1:43
    And then, something interesting is going to happen.
  • 1:43 - 1:47
    We enter into our for loop.
  • 1:47 - 1:49
    And we start with, and what I'm going to do here
  • 1:49 - 1:52
    is I'm going to write it a little bit different than we wrote it over here.
  • 1:52 - 1:56
    So we do is we start a
  • 1:56 - 1:57
    What we are doing is
  • 1:57 - 1:59
    We are going to start our for-loop
  • 1:59 - 2:01
    essential with an assignment
  • 2:01 - 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:12
    or it tests whether it can assign "i" to the first number in the sequence
  • 2:12 - 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:21
    i will do kind off a test case
  • 2:21 - 2:23
    right over here
  • 2:23 - 2:26
    So items left in sequece
  • 2:26 - 2:29
    so item left
  • 2:29 - 2:32
    in sequence
  • 2:32 - 2:34
    and when I'm referring to the sequence I'm talking about
  • 2:34 - 2:35
    this sequence over here
  • 2:35 - 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:42
    i know it's hard to read
  • 2:42 - 2:44
    right over here
  • 2:44 - 2:45
    If there are, so lets say that there are
  • 2:45 - 2:47
    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 lets 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 wanted to do these in blue
  • 3:15 - 3:16
    just our regual
  • 3:16 - 3:18
    or regular
  • 3:18 - 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:26
    then "i" is equal to the next item
  • 3:26 - 3:29
    is equal to the next item
  • 3:29 - 3:30
    thats 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" plus 1
  • 3:47 - 3:50
    and at this point
  • 3:50 - 3:53
    we essentially loop back
  • 3:53 - 3:54
    to this test over here
  • 3:54 - 3:56
    so at this point
  • 3:56 - 3:58
    we loop back
  • 3:58 - 3:59
    i'll try to write it neatly
  • 3:59 - 4:00
    we loop back to
  • 4:00 - 4:06
    we loop back to this point in the program
  • 4:06 - 4:07
    that's why it is called a loop!
  • 4:07 - 4:11
    cause after you preform this operation
  • 4:11 - 4:12
    this is in bedded 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:17
    so you go
  • 4:17 - 4:20
    back to the beginning before the loop and say
  • 4:20 - 4:22
    "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
    these 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 and "product" is
  • 4:32 - 4:34
    equal to what "product" was times "i" plus one
  • 4:34 - 4:35
    we go to the next
  • 4:35 - 4:37
    "are there items left in the sequence?"
  • 4:37 - 4:39
    and at some point there won't be any items
  • 4:39 - 4:41
    left in the sequence
  • 4:41 - 4:43
    so there won't be, at some point there won't be
  • 4:43 - 4:44
    items left in the sequence
  • 4:44 - 4:51
    and we can go to the right or i'll just break out of it down here
  • 4:51 - 4:53
    and now we have broken out of this for-loop
  • 4:53 - 4:55
    we have boken out of this for-loop
  • 4:55 - 4:57
    and then the next thing we do
  • 4:57 - 4:58
    once we are done with our for-loop
  • 4:58 - 5:00
    is we just print the value of "product"
  • 5:00 - 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
  • 5:27 - 5:29
    that one little understanding of the program
  • 5:29 - 5:31
    itself if the last two videos didn't help too much
  • 5:31 - 5:33
    and also gives you a simple understanding
  • 5:33 -
    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