-
One thing you'll hear or see referred to a lot
-
is something called a flowchart
-
either in computer science,
-
so 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 I'll do a simple flowchart.
-
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 flowchart really isn't anything fancy.
-
So in this factorial program
-
that we had been looking at
-
in the last couple of videos,
-
any flowchart you're just going to be at a kind of starting point
-
which is really started, I guess,
-
depicted with a circle or oval of some kind.
-
So we start, you can kind of view as
-
we starting at this line
-
above this assignment of the number
-
where we actually ask for 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 in this step.
-
The parallellogram tells us that
-
we are somehow interfacing with the user.
-
We're 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 one.
-
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 we do is we start a
-
What we are doing is
-
We are 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 "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 off 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 i 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 lets say that there are
-
items left in the sequence.
-
So if there are items left in the sequence.
-
So we'll just say
-
"i" is equal to next item
-
"i" is equal to next
-
"i" is euqal 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 lets say that there are.
-
"i" is equal to the next item.
-
And then we also define
-
we define "product"
-
to be
-
we define "product"
-
actually i wanted to do these in blue
-
just our regular.
-
Se let me write this
-
is "YES".
-
If there are items in the sequence,
-
then "i" is equal to the next item
-
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 the "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!
-
cause after you preform this operation
-
this is embedded in the loop
-
you go back to the beginning
-
to see if there is 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
-
are usually specified 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 one
-
So 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
-
items left in the sequence.
-
And we could go to the right
-
or I'll just break out of it down here.
-
And now we've broken out of this for loop
-
we've boken out of this for loop.
-
And then 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
-
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 flowchart for this simple program.
-
And hopefully it helps you appreciate that
-
one little understanding of the program itself
-
if the last two videos didn't help too much,
-
and also gives you a simple understanding
-
of how to write a flowchart.