Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 38
Default Why this basic LOOP does not work!

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.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Why this basic LOOP does not work!

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.


"GreenInIowa" wrote in
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.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 694
Default Why this basic LOOP does not work!

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."
....

--
Regards,
Sébastien
<http://www.ondemandanalysis.com


"GreenInIowa" wrote:

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.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 38
Default Why this basic LOOP does not work!

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.


"GreenInIowa" wrote in
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.




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,718
Default Why this basic LOOP does not work!

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

the 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
"GreenInIowa" wrote in message
...
| 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.
|
|
| "GreenInIowa" wrote in
| 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.
|
|
|




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Why this basic LOOP does not work!


"GreenInIowa" wrote in message
...
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


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Why this basic LOOP does not work!

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.



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 38
Default Why this basic LOOP does not work!

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.



  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Why this basic LOOP does not work!

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

GreenInIowa wrote:
: 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.
:
:
:
  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Why this basic LOOP does not work!



"vandenberg p" wrote in message
...

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!




  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 126
Default Why this basic LOOP does not work!

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

In article ,
says...
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.


"GreenInIowa" wrote in
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.


  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 690
Default Why this basic LOOP does not work!

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

--
Dana DeLouis
Win XP & Office 2003


"GreenInIowa" wrote in 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.



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
For Each - loop doesn't work. Intellihome[_31_] Excel Programming 3 June 29th 05 11:10 PM
Excel Visual Basic - need help to create a loop of filenames dgates12 Excel Programming 3 May 12th 05 01:22 PM
Excel formula similar to a loop in Basic? Cashtime Excel Worksheet Functions 2 February 6th 05 07:53 PM
Would a loop work? sixfivebeastman[_7_] Excel Programming 1 August 31st 04 03:12 PM
Why doesn't my loop work? Insp Gadget Excel Programming 5 December 22nd 03 10:56 AM


All times are GMT +1. The time now is 12:14 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"