# Thread: determining number of iterations in a for loop

1. ## determining number of iterations in a for loop

for i = 2 to n -1
<do stuff>
where 2<= i <= n-1

to calculate the number of iterations we use the formula
TOP INDEX - BOTTOM INDEX + 1
so
n-1 - 2 + 1 = n-3

now take this loop

for i = 2 to floor(n/2)
<do more stuff>

if n is even the number of iterations equals n/2
if n is odd the number of iterations equals (n-1)/2

Why is it that for the first example we add one after subtracting BOTTOM INDEX from TOP INDEX and in the second example we don't?

Edit** realized i am in wrong section and this is probably more appropriate for discrete math, my apologies in advance.

2. ## Re: determining number of iterations in a for loop

Hey mdm508.

I think you should write this out as a function of the iterations in the loop.

In the first one you have 2 to n-1 and in the second you have 2 to floor(n/2).

Use a common variable for the loop counter and then solve both of them in terms of that.

3. ## Re: determining number of iterations in a for loop

Originally Posted by mdm508
for i = 2 to n -1
<do stuff>
where 2<= i <= n-1

to calculate the number of iterations we use the formula
TOP INDEX - BOTTOM INDEX + 1
so
n-1 - 2 + 1 = n-3

now take this loop

for i = 2 to floor(n/2)
<do more stuff>

if n is even the number of iterations equals n/2
if n is odd the number of iterations equals (n-1)/2

Why is it that for the first example we add one after subtracting BOTTOM INDEX from TOP INDEX and in the second example we don't?

Edit** realized i am in wrong section and this is probably more appropriate for discrete math, my apologies in advance.
In the second case suppose n=4 then floor(n/2) is 2 and the trip count is 1 (not n/2=2)
again suppose n =5 then floor(n/2) is 2 and the trip count is 1 (not 4/2=2)

.

4. ## Re: determining number of iterations in a for loop

Originally Posted by mdm508
for i = 2 to floor(n/2)
<do more stuff>
if n is even the number of iterations equals n/2 ******
Make it n=8 : floor(8/2) = 4

for i = 2 to 4 : that's 3 iterations: 2,3,4

BUT n/2 = 8/2 = 4

******????????????????????

5. ## Re: determining number of iterations in a for loop

Originally Posted by chiro
Hey mdm508.

I think you should write this out as a function of the iterations in the loop.

In the first one you have 2 to n-1 and in the second you have 2 to floor(n/2).

Use a common variable for the loop counter and then solve both of them in terms of that.
See attachment below (PS is there anyway to do inline LaTex??)

my question is now is, why do we add one when we do TOPINDEX - BOTTOMINDEX + 1

6. ## Re: determining number of iterations in a for loop

Originally Posted by mdm508
my question is now is, why do we add one when we do TOPINDEX - BOTTOMINDEX + 1
Illustration:
use digits 2 to 7 to do "something"...

So that's using 7 - 2 = 5 + 1 = 6 digits ; get my drift?

7. ## Re: determining number of iterations in a for loop

yes i think so, now suppose it was 2<=i < 7
in this case 7-2 and boom we are done. so is it the greater or equal than sign that accounts for why we add 1?

8. ## Re: determining number of iterations in a for loop

Your last question is: given integers $a,\,b$ with $a\leq b$, how many integers $i$ are there with $a\leq i\leq b$?

Take a specific example; $a=1,\,b=4$. The integers $i$ are $\{1,2,3,4\}\text{ or } 4\text{ possibilities}$, that is $b-a+1$.

A related loop:

Code:
i=a;
while (i<b) {
// stuff
i=i+1;
}
The above loop executes $b-a$ times! The difference is usage of $<$ instead of $\leq$. If you really want to do it, this can be proved by inducting on $n=a-b$. I think this is overkill. You should be able to believe it by examining small examples.