Why this basic LOOP does not work!

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have the following simple loop where I want "i' to go from 1 to 3.

Sub test()
For i = 1 To 3
Debug.Print "Inside of the loop"; i
Next
Debug.Print "Outside of the loop"; i
End Sub


==Out put is ==
Inside of the loop 1
Inside of the loop 2
Inside of the loop 3
Outside of the loop 4


It does what I expect inside the loop. But, as soon as loop is completed its
value increases by one to 4 although I see no reason for this. Why? Anybody
has any answer for this?

Thanks.
 
The loop control variable is incremented in the For statement.
Thus For is called 4 times, not 3, and the value 4 fails the
test, and control is moved to the line of code following the Next
statement.


message
news:[email protected]...
 
Hi,
It Does work since it does exactly what the definition and documentation of
the For...Next says. Check the online help:
....
"After all statements in the loop have executed, step is added to counter.
At this point, either the statements in the loop execute again (based on the
same test that caused the loop to execute initially), or the loop is exited
and execution continues with the statement following the Next statement."
....
 
Chip,

What confuses me is that loop says "i" would get the maximum value of 3 and
the loop would end with this value. In fact, as soon as it reaches 3 it is
"out" of the loop and continues with the next step. But at what step it
increments again?

I know you are right, but it is not making much of a sense to me.

Thanks.
 
What confuses me is that loop says "i" would get the maximum value of 3the loop would end with this value.

Actually what happens is the loop continues until i exceeds the max. At the
start of the fourth For i is incremented and compared to the max of three.

--
Jim
| Chip,
|
| What confuses me is that loop says "i" would get the maximum value of 3
and
| the loop would end with this value. In fact, as soon as it reaches 3 it is
| "out" of the loop and continues with the next step. But at what step it
| increments again?
|
| I know you are right, but it is not making much of a sense to me.
|
| Thanks.
|
| "Chip Pearson" wrote:
|
| > The loop control variable is incremented in the For statement.
| > Thus For is called 4 times, not 3, and the value 4 fails the
| > test, and control is moved to the line of code following the Next
| > statement.
| >
| >
| > message
| > | > > Hi,
| > >
| > > I have the following simple loop where I want "i' to go from 1
| > > to 3.
| > >
| > > Sub test()
| > > For i = 1 To 3
| > > Debug.Print "Inside of the loop"; i
| > > Next
| > > Debug.Print "Outside of the loop"; i
| > > End Sub
| > >
| > >
| > > ==Out put is ==
| > > Inside of the loop 1
| > > Inside of the loop 2
| > > Inside of the loop 3
| > > Outside of the loop 4
| > >
| > >
| > > It does what I expect inside the loop. But, as soon as loop is
| > > completed its
| > > value increases by one to 4 although I see no reason for this.
| > > Why? Anybody
| > > has any answer for this?
| > >
| > > Thanks.
| >
| >
| >
 
GreenInIowa said:
Chip,

What confuses me is that loop says "i" would get the maximum value of 3 and
the loop would end with this value.

No, it says that the loop will execute through whilst the counter is not
graeter than 3. Normally this ould mean 3 iterations with the counter
exiting with a value of 4, but the code within the loop could alter i and
force it to loop more or fewer times, and even exit with a greater number.
In fact, as soon as it reaches 3 it is
"out" of the loop and continues with the next step.

No, again, it is when it reaches 4 that the loop control knows that it has
finished. But it would know if it were any number above 3, as I said the
code within the loop can increment i, so it could set it to 5 million, and
the loop would finish after that iteration.
But at what step it increments again?

At the Next statement
 
Dear GreenInIowa

The rule for FOR NEXT in VB and also QuickBasic is that the for loop is
executed abd every time the next statement is executed the counter is
incremented by the step size, usually 1, and if the step size is outside the
limts them the loop stops.

This is quite sensible because you could write:

for i = 2 to 7 step 2
debug.print i
next i
debug.print i

This again stops when i is larger than the limit (7).

The rule here is that you should not rely on the limit value. But as you
know the limit there is no problem.

A way to get round the problem is to use a last_value variable if you really
need to as in:

for i = 2 to 7 step 2
last_value = i
debug.print last_value
next i
debug.print last_value

This works as you require and the last_value is 6 and produces:

2
4
6
6

HTH.

Martin.
 
You hit the head of the nail, Martin! Actually, my confusion started with
EXACT example you provided, involving with "for-step-next loop". In my macro,
I was relying on the value of the same variable coming out of the loop, but I
did not realize that it was incrementing outside the loop.

Now, I got it, I think!

Thank you all..
 
Actually I think if you check the official language specs the value of the counter
(i in the example below) is not offically defined once you leave the loop. It has
been that way for loop counters since the early days of Fortran (45+ years ago).
There is a tendency for the value to be the next value in the increment,
but there may be conditions where this is not true.

Also changing the value of the counter during loop execution is frowned
upon and may lead to unexpected results or at least programs that are
hard to follow and debug.

Pieter Vandenberg

: You hit the head of the nail, Martin! Actually, my confusion started with
: EXACT example you provided, involving with "for-step-next loop". In my macro,
: I was relying on the value of the same variable coming out of the loop, but I
: did not realize that it was incrementing outside the loop.

: Now, I got it, I think!

: Thank you all..

: "Martin Fishlock" wrote:

:> Dear GreenInIowa
:>
:> The rule for FOR NEXT in VB and also QuickBasic is that the for loop is
:> executed abd every time the next statement is executed the counter is
:> incremented by the step size, usually 1, and if the step size is outside the
:> limts them the loop stops.
:>
:> This is quite sensible because you could write:
:>
:> for i = 2 to 7 step 2
:> debug.print i
:> next i
:> debug.print i
:>
:> This again stops when i is larger than the limit (7).
:>
:> The rule here is that you should not rely on the limit value. But as you
:> know the limit there is no problem.
:>
:> A way to get round the problem is to use a last_value variable if you really
:> need to as in:
:>
:> for i = 2 to 7 step 2
:> last_value = i
:> debug.print last_value
:> next i
:> debug.print last_value
:>
:> This works as you require and the last_value is 6 and produces:
:>
:> 2
:> 4
:> 6
:> 6
:>
:> HTH.
:>
:> Martin.
:>
:>
:>
 
Also changing the value of the counter during loop execution is frowned
upon and may lead to unexpected results or at least programs that are
hard to follow and debug.

I didn't say you should, I said you could, and that is irrefutable!
 
A good (and idealistic) way of thinking of loop control variables is
that its contents are *unreliable* outside of the loop. It's also a
good way of thinking of a For i=...loop that you *cannot* change the
loop control variable inside of the loop.

Some languages support these capabilities. For example, imagine -- and
this can be done in many languages including vb.net -- you *declare* a
loop control variable in the For statement. Then, its scope is
automatically limited to the For loop. Something along the lines of the
vb.net code:

For i as integer =0 to 10 step 2
...
next i

Now, i doesn't exist outside of the scope of the For loop!

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Multi-disciplinary business expertise
+ Technology skills
= Optimal solution to your business problem
Recipient Microsoft MVP award 2000-2005
 
Here's an example where the loop counter is set outside the range. The
"For" loop code still needs to run its test.
5.5 is incremented by 1 to 6.5. The test now fails, and the code
continues...

Sub Demo()
Dim j
For j = 1 To 3
Debug.Print j
j = 5.5
Debug.Print j
Next j
Debug.Print j
End Sub

1
5.5
6.5
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top