We're going to delve deeper into the second type of repetition structure

commonly found in programs, and that is the count-controlled loop and in VBA,

that's For...Next.

The flowchart for a count-controlled For...Next loop is shown here, and

you notice here that we have this counting index, i=1.

So we come into this with the start value, in this case i = 1, and

while i is not greater than a limit value, for example 10,

then we do the loop code and we increase i by 1.

And if you notice here, then we're not using i in any of the loop code but

we're doing this loop exactly 10 times.

At the very end I becomes 10, 10 is not greater than 10, so

we go through the loop once more.

i becomes 11, and since i exceeds 10 then we will exit the loop.

If we want to do something exactly 10 times, we could implement this Loop Until.

Now the common flow chart element these days for the For...Next looks like this.

We come into this, it's got this rectangle, we have an index = start value,

and there is kind of some implicit arrows,

you go from this upper-right box into the left box.

So we come in the index = start value,

typically this is 1 then we go into this left box, does the index pass some limit?

For example 10, if you want to do this loop 10 times,

if that true then you're done and you just move along.

While we are not exceeding that limit, we perform the loop code and

we go back and the index then is incremented by 1.

This is used frequently with vectors and arrays.

So the syntax for this in VBA is we have For index = start To limit Step increment,

then you have your loop code, and you always have Next index.

If the step increment is left out, a lot of times the step increment is just 1,

but if you wanted to step by anything other than 1, you can put that after step.

You can also count down, so you could start at 20 and count down by negative 2.

An example that would sum the first n integers is shown here.

We start out with a summing variable equal to 0,

For i = 1 to n, the new sum is equal to the old sum plus i.

So if i is equal to 1, then at the end of the first loop, the sum will be 1,

during the second loop, i will be 2.

So we're adding 2 to the previous sum, we'll have 3 and then when i is 3,

we're going to add that to the previous sum, so we get 6.

And we keep going, and that's how you can sum the first n integers.

So I just wanted to reiterate that the flowchart element on the right is exactly

equivalent to what we typically use these days and what I'm going to use in this

course, which is the flow chart element for the For...Next loop shown on the left.

Let's go through an example.

Create a VBA function, that will count the number of the first

n integers that are divisible by either 3 or 5.

So let's make a flowchart for this, we start then we get n,

n is the argument of the function, so we can set up a For loop.

We're going to have to Dim some things, we're going to have to Dim n,

that's going to be an integer, i is going to be an integer.

If i does not exceed n then we're going to go into a if then statement,

is i divisible by 3 or 5?

If that's true then I'm going to increment my counting variable, which we also

have to Dim by 1, so that's the number of items that are divisible by 3 or 5.

If that's false then we don't do anything, we loop back to increment i by 1.

And then once we have exceeded that index n, the limit, then we output C and we END.

The way that we're going to determine if i is divisible by 3 or

5 is using the mod function.

So let's go ahead and put this into VBA code.

So I've created my module and my function, well I'm going to call this divisible.