# Thread: Ways to evaluate loops quickly

1. ## Ways to evaluate loops quickly

Need some help with a formula:

What is the quick formula/way for evaluating no. of times a loop will be processed without actually evaluating the loop?

I was told this:

(Final - Initial)/Step

final is final value
initial is initial value
step is the increment value

for <= condition increment final by one

Example 1

Code:
```int i= 5;
while (i <= 10)
{
cout << "hi" <<endl;
i++;
}```
using formula (11- 5)/1 = 6 times which is correct

but

with Example 2
Code:
```   int count = 1;
int num = 25;
while (count < 25)
{
num = num - 1;
count++;
}
cout <<count << num << endl```
would print 25 and 1.
Formula (25- 1)/1 =24 times which would make the formula found not work for all cases.

Do any of you the correct formula for this?

mrc

2. But it is executed 24 times... Think about it: It is executed once for count=1, once for count=2, etc... up to count=24 at which time, the loop executes, incrementing the variable of count to 25 (which is why it executes no more). The number of times count was incremented was 24 times (1+24=25). Similarly with the other variable.

3. In the second case FINAL should be 24 not 25. Your using < which means the loop will execute with a max value of 24.

4. Originally Posted by Mister C
Need some help with a formula:

What is the quick formula/way for evaluating no. of times a loop will be processed without actually evaluating the loop?

I was told this:

(Final - Initial)/Step

final is final value
initial is initial value
step is the increment value

for <= condition increment final by one

Example 1

Code:
```int i= 5;
while (i <= 10)
{
cout << "hi" <<endl;
i++;
}```
using formula (11- 5)/1 = 6 times which is correct

but

with Example 2
Code:
```   int count = 1;
int num = 25;
while (count < 25)
{
num = num - 1;
count++;
}
cout <<count << num << endl```
would print 25 and 1.
Formula (25- 1)/1 =24 times which would make the formula found not work for all cases.

Do any of you the correct formula for this?

mrc

Two things wrong:

2. You plugged the wrong values into the formula.

Let's forget C programming loops, and look at a couple of cases.

Suppose initial = 1, final = 6, step = 1

How many values are used? Count them:

1, 2, 3, 4, 5, 6

Number of values = 6

Now, let initial = 1, final = 5, step = 2

1, 3, 5

Number of values = 3

Now I propose that the formula should be

((final - initial)/step) + 1

Apply the formula to your examples

First example: initial = 5, final = 10, step = 1

Number of values = 6

Second example: initial = 1, final = 24, step = 1

Number of values = 24.

See the pattern? A few examples does not prove that the proposed formula is correct. How would you prove it? (Mathematical induction comes to mind.)

Regards,

Dave

Engineer's rule of mathematical induction: If it works for n = 1, and it works for n = 2, it works for all n.

---Anonymous, but I think that engineer was one of the casualties when his bridge collapsed at its opening ceremony

5. Dave,

I realize I can use induction proofs. I would have to go back and refresh myself with this.

I was just trying to see if anyone would know this without me doing the mathematical rigor.

I also realize this is not a hard one to solve either. (Someone else has allready figured this out)

jc

6. Originally Posted by Mister C
Dave,

I realize I can use induction proofs. I would have to go back and refresh myself with this.

I was just trying to see if anyone would know this without me doing the mathematical rigor.

I also realize this is not a hard one to solve either. (Someone else has allready figured this out)

jc
(I didn't actuallly mean that you should go through a formal proof before using the formula. I wanted to point out that your formula was incorrect. I think you can use enough sample cases to convince youself that the proposed correction is valid, then use it forever.)

Mathematical induction is rarely useful for discovering formulas, but if an instructor insists on proving a given formula, mathematical induction is a possibility. Not that I would expect any one actually to do this.

To show a formula is incorrect, you only have to give an example for which the formula gives the wrong answer.

Regards,

Dave