Hi

The following code in a Visual Basic Net book

Dim I As Integer , X As Integer
For I = 1 to 3
For X = 1 to 3
Debug.Write("I = " & I & vbCrLf)
Debug.Write("X = " & X & vbCrLf)

produces

I = 1
X = 1
I = 1
X = 2
I = 1
X = 3
I = 2
X = 1
I = 2
X = 2
I = 2
X = 3
I = 3
X = 1
I = 3
X = 2
I = 3
X = 3

I get the gist of this but I am surprised by line 8( X + 1 [again])

There is no code that has actually stipulates that X should return to zero.
I would have been less surprised if X had proceeded to 4 and the code had
refused to execute after line 8

Can sm give us a quick explanation please?


Thanks in advance

Re: Loops that zero themselves? by Jezebel

Jezebel
Sun Aug 01 06:38:23 CDT 2004

You've got two loops here, on inside the other. For each value of I, you run
through the loop for X.


"Dave Neve" <NoAdressForSpammers@Nofs.fr> wrote in message
news:uLn8T25dEHA.592@TK2MSFTNGP11.phx.gbl...
> Hi
>
> The following code in a Visual Basic Net book
>
> Dim I As Integer , X As Integer
> For I = 1 to 3
> For X = 1 to 3
> Debug.Write("I = " & I & vbCrLf)
> Debug.Write("X = " & X & vbCrLf)
>
> produces
>
> I = 1
> X = 1
> I = 1
> X = 2
> I = 1
> X = 3
> I = 2
> X = 1
> I = 2
> X = 2
> I = 2
> X = 3
> I = 3
> X = 1
> I = 3
> X = 2
> I = 3
> X = 3
>
> I get the gist of this but I am surprised by line 8( X + 1 [again])
>
> There is no code that has actually stipulates that X should return to
zero.
> I would have been less surprised if X had proceeded to 4 and the code had
> refused to execute after line 8
>
> Can sm give us a quick explanation please?
>
>
> Thanks in advance
>
>



Re: Loops that zero themselves? by Dave

Dave
Sun Aug 01 12:31:05 CDT 2004

Hi

Yes, this bit I've got.

But are you saying that running through a loop takes it back to its initial
value.

If this was so, then loops like the ones below would run forever, going back
to zero and running again.

This is what I don't get about the code snippet.

What put X back to 1????

Thanks in advance
"Jezebel" <dwarves@heaven.com.kr> a écrit dans le message de news:
uzTsPw7dEHA.3476@tk2msftngp13.phx.gbl...
> You've got two loops here, on inside the other. For each value of I, you
run
> through the loop for X.
>
>
> "Dave Neve" <NoAdressForSpammers@Nofs.fr> wrote in message
> news:uLn8T25dEHA.592@TK2MSFTNGP11.phx.gbl...
> > Hi
> >
> > The following code in a Visual Basic Net book
> >
> > Dim I As Integer , X As Integer
> > For I = 1 to 3
> > For X = 1 to 3
> > Debug.Write("I = " & I & vbCrLf)
> > Debug.Write("X = " & X & vbCrLf)
> >
> > produces
> >
> > I = 1
> > X = 1
> > I = 1
> > X = 2
> > I = 1
> > X = 3
> > I = 2
> > X = 1
> > I = 2
> > X = 2
> > I = 2
> > X = 3
> > I = 3
> > X = 1
> > I = 3
> > X = 2
> > I = 3
> > X = 3
> >
> > I get the gist of this but I am surprised by line 8( X + 1 [again])
> >
> > There is no code that has actually stipulates that X should return to
> zero.
> > I would have been less surprised if X had proceeded to 4 and the code
had
> > refused to execute after line 8
> >
> > Can sm give us a quick explanation please?
> >
> >
> > Thanks in advance
> >
> >
>
>



Re: Loops that zero themselves? by Jezebel

Jezebel
Sun Aug 01 17:54:55 CDT 2004

X is set to 1 each time the inner loop starts, which happens three times in
your code, once for each iteration of the outer loop.


"Dave Neve" <NoAdressForSpammers@Nofs.fr> wrote in message
news:uyYbJ1%23dEHA.3988@tk2msftngp13.phx.gbl...
> Hi
>
> Yes, this bit I've got.
>
> But are you saying that running through a loop takes it back to its
initial
> value.
>
> If this was so, then loops like the ones below would run forever, going
back
> to zero and running again.
>
> This is what I don't get about the code snippet.
>
> What put X back to 1????
>
> Thanks in advance
> "Jezebel" <dwarves@heaven.com.kr> a écrit dans le message de news:
> uzTsPw7dEHA.3476@tk2msftngp13.phx.gbl...
> > You've got two loops here, on inside the other. For each value of I, you
> run
> > through the loop for X.
> >
> >
> > "Dave Neve" <NoAdressForSpammers@Nofs.fr> wrote in message
> > news:uLn8T25dEHA.592@TK2MSFTNGP11.phx.gbl...
> > > Hi
> > >
> > > The following code in a Visual Basic Net book
> > >
> > > Dim I As Integer , X As Integer
> > > For I = 1 to 3
> > > For X = 1 to 3
> > > Debug.Write("I = " & I & vbCrLf)
> > > Debug.Write("X = " & X & vbCrLf)
> > >
> > > produces
> > >
> > > I = 1
> > > X = 1
> > > I = 1
> > > X = 2
> > > I = 1
> > > X = 3
> > > I = 2
> > > X = 1
> > > I = 2
> > > X = 2
> > > I = 2
> > > X = 3
> > > I = 3
> > > X = 1
> > > I = 3
> > > X = 2
> > > I = 3
> > > X = 3
> > >
> > > I get the gist of this but I am surprised by line 8( X + 1 [again])
> > >
> > > There is no code that has actually stipulates that X should return to
> > zero.
> > > I would have been less surprised if X had proceeded to 4 and the code
> had
> > > refused to execute after line 8
> > >
> > > Can sm give us a quick explanation please?
> > >
> > >
> > > Thanks in advance
> > >
> > >
> >
> >
>
>



Re: Loops that zero themselves? by Barry

Barry
Sun Aug 01 23:46:52 CDT 2004

On Sun, 1 Aug 2004 19:31:05 +0200, "Dave Neve"
<NoAdressForSpammers@Nofs.fr> wrote:

>Hi
>
>Yes, this bit I've got.
>
>But are you saying that running through a loop takes it back to its initial
>value.

Iterating through a loop does not reset the loop index. In fact the
loop index is normally incremented or decremented. Re-entering a loop
does reinitialize the index.

Your code has two loops, one for I and one for X. They are nested.
Thus the I loop cannot iterate until the X loop has iterated three
times. The first time through I, you enter X and iterate three times.
Then you iterate on I and enter X anew, thus iterating three times.
Then you iterate on I and do it all again. Finally, the I loop
terminates.

>
>If this was so, then loops like the ones below would run forever, going back
>to zero and running again.
>
>This is what I don't get about the code snippet.
>
>What put X back to 1????

As an example, assume you have a two dimensional array and you want to
step through each element. After you finish the first row, you want
to start at the beginning of the second row.

>
>Thanks in advance
>"Jezebel" <dwarves@heaven.com.kr> a écrit dans le message de news:
>uzTsPw7dEHA.3476@tk2msftngp13.phx.gbl...
>> You've got two loops here, on inside the other. For each value of I, you
>run
>> through the loop for X.
>>
>>
>> "Dave Neve" <NoAdressForSpammers@Nofs.fr> wrote in message
>> news:uLn8T25dEHA.592@TK2MSFTNGP11.phx.gbl...
>> > Hi
>> >
>> > The following code in a Visual Basic Net book
>> >
>> > Dim I As Integer , X As Integer
>> > For I = 1 to 3
>> > For X = 1 to 3
>> > Debug.Write("I = " & I & vbCrLf)
>> > Debug.Write("X = " & X & vbCrLf)
>> >
>> > produces
>> >
>> > I = 1
>> > X = 1
>> > I = 1
>> > X = 2
>> > I = 1
>> > X = 3
>> > I = 2
>> > X = 1
>> > I = 2
snip


<<Remove the del for email>>