Help With For Loop

Status
Not open for further replies.

Arbiter

In Runtime
Messages
147
Here is the loop in Small Basic...

For i = 1 To 100 Step 5
GraphicsWindow.DrawRectangle(100 - i, 100 - i, i *2, i * 2)
EndFor

I'm having trouble understanding why 'i' is being used in the parenthesis.
 
It looks like i is simply being used to help determine the location and size of the rectangle being drawn on the window. In the code posted, I would guess that 20 rectangles of decreasing size are drawn at increasing distances from the top left of the window.
 
So inside the parenthesis... i which equals 1 to 100 step 5, is being subtracted from 100 in the x and y co-ordinate and then i is being multiplyed by 2 for the width and height I believe, is this correct?
 
Here is the loop in Small Basic...

For i = 1 To 100 Step 5
GraphicsWindow.DrawRectangle(100 - i, 100 - i, i *2, i * 2)
EndFor

I'm having trouble understanding why 'i' is being used in the parenthesis.
The "parenthesis" are used because it indicates (in this case) that the prior word is a Function called "DrawRectangle" within the "GraphicsWindow" (which was either previously defined or is using the default values (unlikely as it probably needs to be opened (permitted, defined) )).

The "i" is used so the value can be incremented (by 5) each time through the Loop (which repeats (100-1)/5 times).

Note: 100-0 would be 'better' (usually, but not always) since then you would count 0,5,10,15,etc. instead of 1,6,11,16,etc. . IF you actually do want to count 1,6,11,16,etc. then is is fine as it is, otherwise you want " For i = 0 To xx Step 5" (where xx is either 95 or 100).


When "GraphicsWindow.DrawRectangle" is called the values are:
GraphicsWindow.DrawRectangle(99, 99, 2, 2)
GraphicsWindow.DrawRectangle(94, 94, 12, 12)
GraphicsWindow.DrawRectangle(88, 88, 22, 22)
...

Loops often start with zero and not one so the increment would count by 5's (in this case). You would be better served by reading examples for Beginners written by a more skillful Teacher. Try a different Book.
 
The "parenthesis" are used because it indicates (in this case) that the prior word is a Function called "DrawRectangle" within the "GraphicsWindow" (which was either previously defined or is using the default values (unlikely as it probably needs to be opened (permitted, defined) )).

The "i" is used so the value can be incremented (by 5) each time through the Loop (which repeats (100-1)/5 times).

Note: 100-0 would be 'better' (usually, but not always) since then you would count 0,5,10,15,etc. instead of 1,6,11,16,etc. . IF you actually do want to count 1,6,11,16,etc. then is is fine as it is, otherwise you want " For i = 0 To xx Step 5" (where xx is either 95 or 100).


When "GraphicsWindow.DrawRectangle" is called the values are:
GraphicsWindow.DrawRectangle(99, 99, 2, 2)
GraphicsWindow.DrawRectangle(94, 94, 12, 12)
GraphicsWindow.DrawRectangle(88, 88, 22, 22)
...

Loops often start with zero and not one so the increment would count by 5's (in this case). You would be better served by reading examples for Beginners written by a more skillful Teacher. Try a different Book.

Wow there. Stop.
1) Just because the loop starts at 1 doesn't mean it's wrong, especially in this case since if it started at 0 the first rectangle would be 0 size (and therefore probably not visible) anyway.
2) The exact size and location of the rectangle is not relevant in the context of an example loop.
3) You don't need to be Brian Kernighan to teach entry level BASIC.
4) You are ill-equipped to judge the skill of the teacher (or author of the example).
5) I don't know that he is even using a book, but if he is, I doubt that he needs to rush out to get another.

Also, the first two arguments in the third loop would be 89, not 88.
 
Status
Not open for further replies.
Back
Top Bottom