+ Reply to Thread
Results 1 to 8 of 8

Why this basic LOOP does not work!

  1. #1
    GreenInIowa
    Guest

    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. #2
    Chip Pearson
    Guest

    Re: 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" <GreenInIowa@discussions.microsoft.com> wrote in
    message
    news:B5D0E0AF-CB14-4824-AA76-F9E0C84CC532@microsoft.com...
    > 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. #3
    sebastienm
    Guest

    RE: 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. #4
    GreenInIowa
    Guest

    Re: 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" <GreenInIowa@discussions.microsoft.com> wrote in
    > message
    > news:B5D0E0AF-CB14-4824-AA76-F9E0C84CC532@microsoft.com...
    > > 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. #5
    Jim Rech
    Guest

    Re: 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" <GreenInIowa@discussions.microsoft.com> wrote in message
    news:77B9FD53-E6CB-47EC-9C9C-F86A7722AEED@microsoft.com...
    | 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" <GreenInIowa@discussions.microsoft.com> wrote in
    | > message
    | > news:B5D0E0AF-CB14-4824-AA76-F9E0C84CC532@microsoft.com...
    | > > 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. #6
    Bob Phillips
    Guest

    Re: Why this basic LOOP does not work!


    "GreenInIowa" <GreenInIowa@discussions.microsoft.com> wrote in message
    news:77B9FD53-E6CB-47EC-9C9C-F86A7722AEED@microsoft.com...
    > 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. #7
    Martin Fishlock
    Guest

    Re: 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. #8
    GreenInIowa
    Guest

    Re: 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.
    >
    >
    >


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1