Welcome to the for loop Wildblue lecture in this lecture I'm going to introduce and explain how the
for loop and while loop can be used in VHDL.
First let's talk about the for loop for loop is commonly used to generate multiple instances of the
same logic in VHDL for loops.
They go away after synthesis.
So we commonly use these to construct circuits for you know if we want to generate something over and
over a for loop is a great way to do this.
But after synthesis the four loops go away.
They're just used to help make our code a lot cleaner is a lot less lines and a little more easy to
understand.
The key words in a for loop bar you have four in two loop and end loop.
So an example of a for loop would be we have two signals who have signaled a b and c and their standard
logic vectors from four down to zero so we know that there are five bits wide.
And now we have our four loop statement where we're going to use this to generate R and gates.
So we have our four I mean zero 2 for loop.
So what that's going to do is take this value by and it's going to iterate from 0 to 4.
So we'll get five iterations of 0 1 2 3 and 4.
And inside of that loop whatever statements we replace is going to be iterated five times.
So we have the statement.
See why is equal to.
I ended with be-I.
So this is going to generate 5 different and gates where we have the input signal A and B will be added
together and create the signal C..
So if you look we have the five different and gates where our age zero is added with a zero and the
output is zero.
And this goes on for a one be one all the way down to a four before we get our output see.
And this really is a quick and easy way.
Instead of writing C I yield to AI and B or C zeroes equals to a 1 or 0 0 9 C one is equal to a 1 and
B 1 all the way down to a 4 or C 4 is equal to a 4.
And before this is a lot cleaner and quicker way of doing this.
Now here we only have five and gates.
But if you have a lot more complex circuit where you're working with say 100 and Gates or however many
you have this is a quick and easy way to do that.
And you can also do more complex things where you can implement flip flops or component instantiations
of different circuits.
But this is the general idea after synthesis this for loop example the for the for loops going to go
away.
We're just going to generate for endgames Now we'll talk about a while.
These are very similar in nature to a for loop.
However there are several differences.
And the big one being that the condition is checked before the loop is entered in a for loop.
We specifically tell the loop How many times we're going to iterate like with our example we said we're
going from zero to four.
So we knew right off the bat we're going to be iterating five times.
However in a while loop we have a condition and this condition is checked before we go into the loop.
And then every time we iterate through that loop we check that condition.
And if that condition evaluates to true then we get out of the loop.
So gives it that loop continues running until the condition evaluates to false.
So it's important to know that the condition eventually evaluates to false.
So if we look at the syntax of how the while loop would take place we have the loop name while condition
and the condition is whatever you set out to be.
While that's true we're going to be executing the statements and then we end our loop.
And so you could do the same exact thing you do in a while versus a for loop.
However you just have to make sure at some point in time that your condition will evaluate to a false
so for example we have a process of calling it P2.
We have it evaluates on the Ellen underscores the variable the one we're saying that to evaluate one
and then we begin.
Now we have our while loop for a while.
The one is equal to less than or equal to ninety nine.
We want to loop through.
So our out Z is being set to Alan underscore z z 1 plus 8.
And really the important thing to note here that is the one is constantly being incremented the values
being set to Z One plus one.
So we're going to increment through this loop 100 times or 99 times because you will start with z one
starts at 1 and we'll go through nine nine times until the while the one is less than or equal to ninety
nine.
We'll just keep looping through.
And so once we hit the one hundredth time we'll get out of the loop and the process and get out.
Now if we would take out the statement z one is equal to Z One plus one.
We would create a condition we call an infinite loop if that while loop will never go to falls.
Then you'll sit in that loop and spin forever.
And this can be a problem either your synthesizer will catch this and spit on a warning or if it doesn't
hear program or your code and be HGL will never get out of this process.
And so that can cause an issue.
So that's just something have to be careful with when you're working with a while loop.
Just be very cautious of infinite loops.
So the main difference if we can bear for a loop while loop for loop will iterate a specific amount
of times where a while loop will iterate based upon the code you tell it until the condition is false.
And when you're working with a while loop just be very very cautious of an infinite loop.
Now that you have the knowledge of for loops and while loops.
Next we're going to learn about when else statements and with select when statements.