# loops

• ### Question

• I was messing around with a piece of code previously post to do with moving a gris. I wanted to create a moving pattern and had a counter at the the side of the grid to let me know how far into the loop I was. What I want to know is why does the code below always have one more than the number of the loop i.e if the loop is 1 to 10 then it coubts to 11.
```rows = 8
columns = 8
size = 40
offsetX = (GraphicsWindow.Width-rows*size)/2
offsetY = (GraphicsWindow.Height-columns*size)/2
For cube = 1 To 1000
GraphicsWindow.BrushColor="black"
GraphicsWindow.DrawboundText(100,100,50,cube)
For r = 1 To rows
For c = 1 To columns
GraphicsWindow.BrushColor = GraphicsWindow.GetRandomColor()
Shapes.Move(boxes[r][c], offsetX + (c-1)*size, offsetY + (r-1)*size )
EndFor
EndFor
'Program.Delay(1000)
GraphicsWindow.BrushColor="white"
GraphicsWindow.DrawboundText(100,100,50,cube)
EndFor
GraphicsWindow.BrushColor="black"
GraphicsWindow.DrawboundText(100,100,50,cube) ```
Sunday, January 10, 2010 5:45 PM

• dhester,

This is due the way that loop counters work (this is also true of other languages - not just SB):

Say we have

For i = 1 To 10
...
EndFor

When the loop starts, i is set to 1, the loop contents is executed till we get to EndFor, then it returns to the start of the loop.

At this point 2 things happen

1] the counter i is incremented, in this case to 2
2] it is tested with the maximum count in the loop (10)

If i is <= 10 then the loop proceeds.

Now, when the final loop is completed with i = 10, we again return to the start of the loop, increment i (this time to 11).  Now i is not <= 10, and the code skips to the end of the loop.

So, whenever a loop has completed, the counter variable (say i, cube in your case) will be equal to the first value that doesn't do the loop (usually the final value +1)

You wouldn't normally see this, except in your case you output this value after the loop has completed .

Also, after a while this code slows considerably due to the large number of shapes that have been added.  Try removing an old shape before adding a new one.

GraphicsWindow.BrushColor = GraphicsWindow.GetRandomColor()
Shapes.Remove(boxes[r][c])
Shapes.Move(boxes[r][c], offsetX + (c-1)*size, offsetY + (r-1)*size )

• Marked as answer by Monday, January 11, 2010 5:23 AM
Sunday, January 10, 2010 5:58 PM

### All replies

• dhester,

This is due the way that loop counters work (this is also true of other languages - not just SB):

Say we have

For i = 1 To 10
...
EndFor

When the loop starts, i is set to 1, the loop contents is executed till we get to EndFor, then it returns to the start of the loop.

At this point 2 things happen

1] the counter i is incremented, in this case to 2
2] it is tested with the maximum count in the loop (10)

If i is <= 10 then the loop proceeds.

Now, when the final loop is completed with i = 10, we again return to the start of the loop, increment i (this time to 11).  Now i is not <= 10, and the code skips to the end of the loop.

So, whenever a loop has completed, the counter variable (say i, cube in your case) will be equal to the first value that doesn't do the loop (usually the final value +1)

You wouldn't normally see this, except in your case you output this value after the loop has completed .

Also, after a while this code slows considerably due to the large number of shapes that have been added.  Try removing an old shape before adding a new one.

GraphicsWindow.BrushColor = GraphicsWindow.GetRandomColor()
Shapes.Remove(boxes[r][c])
Shapes.Move(boxes[r][c], offsetX + (c-1)*size, offsetY + (r-1)*size )

• Marked as answer by Monday, January 11, 2010 5:23 AM
Sunday, January 10, 2010 5:58 PM
• litdev

Thanks for that, i seam to recall learning that many years ago.

Perhaps there is something you might be able to answer in relation to my code is why does each loop take longer than the last. i.e the time to complete the loop from 1 to 10 is 2 seconds but from 50 to 60 is 6 seconds, and so on.

Is this a problem with smallbasic?
Sunday, January 10, 2010 6:07 PM
• See my previous post where I added an edit comment on this before you posted.
Sunday, January 10, 2010 6:10 PM
• litdev

Removing the shape does speed things up.

Thanks for the help.
Sunday, January 10, 2010 6:12 PM