Flowchart for the Factorial Program


One thing you’ll hear or
you’ll see referred to a lot is something called
a flow chart, either in computer science,
or really when people talk about any type of
algorithm or process. So what I thought I would
do is for every program I write is write a
little simple flow chart. Maybe I won’t do
it for every one, but I’ll start with
some of the basic ones, just so we understand
that a flow chart really isn’t anything fancy. So in this factorial
program that we had been looking at the last
couple of videos, any flow chart, you’re just going to be
kind of at the starting point, which is usually
started, I guess, depicted with a circle
or oval of some kind. So we start– you
could kind of view it as we’re starting at this
line above this assignment of the number where we actually
ask for an input from the user. And then after that, we ask
for input from the user. This line right here,
we are asking for input from the user, so
this right over here. And we will depict that
by a parallelogram. So that is input from the user. And then we could just say
number equals user input. And you can be a little
bit loosey goosey with the terminology here. You’re really just trying to say
what you’re doing at this step. The parallelogram tells
us that we are somehow interfacing with the user. We’re just either taking
something from the user, or we’re outputting
something to the user. So here we’re saying user
input is equal to number. We’re assigning number
as the user input. Then the next thing we do is
just a straight-up operation. We just set product
to be equal to 1. So we just set– and there we
just do that in a rectangle. Product equals 1. Product is equal to 1. And then something interesting
is going to happen. We enter into our for
loop, and we start with– and what I’m going
to do here is I’m going to write it a little bit
different than we wrote it over here. So what we do is we start
a– what we’re doing is we’re going to start
our for loop essentially with an assignment, where we
assign– for loop is really a bunch of things
happening at the same time. It assigns an i to the first
number in the sequence, or it tests whether
it can assign i to the first number
in the sequence. If it can, then it proceeds. If it can’t, then it
breaks out of the for loop. So let me put it this way. I will do kind of a test
case right over here. So items left in sequence. And when I’m referring
to the sequence, I’m talking about this sequence
here, the range of our number. In the example they gave in the
last video, the number was 3, so items left in this sequence. I know it’s hard to
read right over here. If there are– so
let’s say that there are items left in sequence. So if there are items
left in sequence, so we’ll just say i is equal to
next item. i is equal to next. i is equal to the next item. If there are– well, I’ll
just hold off for the case where there are no items
left in the sequence. But let’s say that there are.
i is equal to the next item. And then we also define
product to be– actually, I wanted to do these in blue,
just our regular– so let me write this as yes. If there are items
in the sequence, then i is equal
to the next item. That’s one operation we do. And then we reset product. We reassign product
to be equal to what the former value of
product was times i plus 1. And at this point,
we essentially loop back to this
test over here. So at this point, we loop back. I’ll try to write it neatly. We loop back to– let me
do it right over here. We loop back to this
point in the program. That’s why it is called a
loop, because after you perform this operation– this is
embedded in the loop– you go back to the
beginning to see if there’s any more
of the loop to do. So you go back to the
beginning before the loop, and you say, hey, are there
any items in the sequence? And this should be a
question mark over here. These kind of
question mark decision points you specify
with this diamond. If there is another
item in the sequence, then i is equal
to the next item, and product is equal to what
product was, times i plus 1, and We go to the next. Are there items left
in the sequence? And at some point,
there won’t be any items left in the sequence. So there won’t
be– at some point, there won’t be any items
left in the sequence. And we could go to the
right, or I’ll just break out of it down here. I’ll just break that
out of it down here. And now we’ve broken
out of this for loop. We’ve broken out
of this for loop, and the next thing we do once
we’re done with our for loop is we just print the
value of product. We just output the
value of product. So that is an interaction
with the user. So we are literally
just outputting the value of product. So we output product,
and we are done. And I can write end for
the end of our program, and the program
will stop running. End. So this is just a simple flow
chart for this simple program, and hopefully, it helps you
appreciate that one– well, one, a little understanding
of the program itself if the last two
videos didn’t help too much. And it also gives you
a simple understanding of how to write a flow chart.

16 Replies to “Flowchart for the Factorial Program”

  1. i know its annoying, but its just a comment. lets not disparage. besides, its almost an achievement with all the views and comments Sal gets on his videos =)

  2. Can I say that my teacher in programming is an idiot letting us to make flowchart but he never taught us about this stuffs, he only taught us all about the basic , I'm hoping that you will be our teacher !

Leave a Reply

Your email address will not be published. Required fields are marked *