WEBVTT

00:12.740 --> 00:18.830
Welcome to the for loop Wildblue lecture in this lecture I'm going to introduce and explain how the

00:18.830 --> 00:23.170
for loop and while loop can be used in VHDL.

00:23.180 --> 00:29.240
First let's talk about the for loop for loop is commonly used to generate multiple instances of the

00:29.240 --> 00:33.100
same logic in VHDL for loops.

00:33.110 --> 00:35.090
They go away after synthesis.

00:35.090 --> 00:41.150
So we commonly use these to construct circuits for you know if we want to generate something over and

00:41.150 --> 00:43.260
over a for loop is a great way to do this.

00:43.280 --> 00:45.580
But after synthesis the four loops go away.

00:45.590 --> 00:52.280
They're just used to help make our code a lot cleaner is a lot less lines and a little more easy to

00:52.280 --> 00:53.190
understand.

00:53.360 --> 01:01.480
The key words in a for loop bar you have four in two loop and end loop.

01:01.680 --> 01:10.080
So an example of a for loop would be we have two signals who have signaled a b and c and their standard

01:10.080 --> 01:15.520
logic vectors from four down to zero so we know that there are five bits wide.

01:15.900 --> 01:20.910
And now we have our four loop statement where we're going to use this to generate R and gates.

01:20.910 --> 01:26.560
So we have our four I mean zero 2 for loop.

01:26.580 --> 01:32.850
So what that's going to do is take this value by and it's going to iterate from 0 to 4.

01:32.850 --> 01:38.260
So we'll get five iterations of 0 1 2 3 and 4.

01:38.460 --> 01:43.640
And inside of that loop whatever statements we replace is going to be iterated five times.

01:43.650 --> 01:45.630
So we have the statement.

01:45.640 --> 01:48.750
See why is equal to.

01:48.930 --> 01:51.460
I ended with be-I.

01:51.600 --> 01:58.860
So this is going to generate 5 different and gates where we have the input signal A and B will be added

01:58.860 --> 02:02.000
together and create the signal C..

02:02.010 --> 02:09.270
So if you look we have the five different and gates where our age zero is added with a zero and the

02:09.270 --> 02:10.770
output is zero.

02:10.860 --> 02:17.730
And this goes on for a one be one all the way down to a four before we get our output see.

02:17.790 --> 02:20.250
And this really is a quick and easy way.

02:20.310 --> 02:32.310
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

02:32.310 --> 02:37.160
B 1 all the way down to a 4 or C 4 is equal to a 4.

02:37.170 --> 02:40.910
And before this is a lot cleaner and quicker way of doing this.

02:40.920 --> 02:43.530
Now here we only have five and gates.

02:43.710 --> 02:49.950
But if you have a lot more complex circuit where you're working with say 100 and Gates or however many

02:49.950 --> 02:52.720
you have this is a quick and easy way to do that.

02:52.800 --> 02:58.320
And you can also do more complex things where you can implement flip flops or component instantiations

02:58.320 --> 02:59.550
of different circuits.

02:59.670 --> 03:05.820
But this is the general idea after synthesis this for loop example the for the for loops going to go

03:05.820 --> 03:06.470
away.

03:06.510 --> 03:12.320
We're just going to generate for endgames Now we'll talk about a while.

03:12.410 --> 03:15.420
These are very similar in nature to a for loop.

03:15.440 --> 03:17.300
However there are several differences.

03:17.370 --> 03:22.970
And the big one being that the condition is checked before the loop is entered in a for loop.

03:22.970 --> 03:28.100
We specifically tell the loop How many times we're going to iterate like with our example we said we're

03:28.100 --> 03:29.610
going from zero to four.

03:29.690 --> 03:33.320
So we knew right off the bat we're going to be iterating five times.

03:33.320 --> 03:39.370
However in a while loop we have a condition and this condition is checked before we go into the loop.

03:39.440 --> 03:42.990
And then every time we iterate through that loop we check that condition.

03:43.100 --> 03:47.840
And if that condition evaluates to true then we get out of the loop.

03:48.020 --> 03:53.300
So gives it that loop continues running until the condition evaluates to false.

03:53.300 --> 03:57.710
So it's important to know that the condition eventually evaluates to false.

03:57.740 --> 04:04.820
So if we look at the syntax of how the while loop would take place we have the loop name while condition

04:05.180 --> 04:08.690
and the condition is whatever you set out to be.

04:08.690 --> 04:14.340
While that's true we're going to be executing the statements and then we end our loop.

04:14.480 --> 04:19.080
And so you could do the same exact thing you do in a while versus a for loop.

04:19.220 --> 04:24.230
However you just have to make sure at some point in time that your condition will evaluate to a false

04:26.220 --> 04:31.290
so for example we have a process of calling it P2.

04:31.440 --> 04:38.160
We have it evaluates on the Ellen underscores the variable the one we're saying that to evaluate one

04:38.670 --> 04:39.750
and then we begin.

04:39.840 --> 04:42.810
Now we have our while loop for a while.

04:42.810 --> 04:48.290
The one is equal to less than or equal to ninety nine.

04:48.300 --> 04:49.630
We want to loop through.

04:49.770 --> 04:57.510
So our out Z is being set to Alan underscore z z 1 plus 8.

04:57.600 --> 05:03.250
And really the important thing to note here that is the one is constantly being incremented the values

05:03.300 --> 05:06.350
being set to Z One plus one.

05:06.360 --> 05:13.800
So we're going to increment through this loop 100 times or 99 times because you will start with z one

05:13.800 --> 05:19.830
starts at 1 and we'll go through nine nine times until the while the one is less than or equal to ninety

05:19.830 --> 05:20.190
nine.

05:20.200 --> 05:21.720
We'll just keep looping through.

05:21.990 --> 05:27.470
And so once we hit the one hundredth time we'll get out of the loop and the process and get out.

05:27.540 --> 05:33.400
Now if we would take out the statement z one is equal to Z One plus one.

05:33.920 --> 05:39.680
We would create a condition we call an infinite loop if that while loop will never go to falls.

05:39.690 --> 05:41.720
Then you'll sit in that loop and spin forever.

05:41.850 --> 05:47.580
And this can be a problem either your synthesizer will catch this and spit on a warning or if it doesn't

05:47.940 --> 05:52.850
hear program or your code and be HGL will never get out of this process.

05:52.890 --> 05:54.500
And so that can cause an issue.

05:54.500 --> 05:57.310
So that's just something have to be careful with when you're working with a while loop.

05:57.330 --> 06:00.010
Just be very cautious of infinite loops.

06:01.340 --> 06:06.260
So the main difference if we can bear for a loop while loop for loop will iterate a specific amount

06:06.260 --> 06:13.100
of times where a while loop will iterate based upon the code you tell it until the condition is false.

06:13.340 --> 06:18.880
And when you're working with a while loop just be very very cautious of an infinite loop.

06:19.010 --> 06:21.780
Now that you have the knowledge of for loops and while loops.

06:21.800 --> 06:27.740
Next we're going to learn about when else statements and with select when statements.
