Loops that zero themselves?

D

Dave Neve

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
 
J

Jezebel

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

Dave Neve

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 said:
You've got two loops here, on inside the other. For each value of I, you run
through the loop for X.


Dave Neve said:
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
 
J

Jezebel

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 said:
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 said:
You've got two loops here, on inside the other. For each value of I, you run
through the loop for X.


Dave Neve said:
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
 
B

Barry Schwarz

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
snip


<<Remove the del for email>>
 

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

Top