for loop

Blitz3D Forums/Blitz3D Beginners Area/for loop

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

r_x = 10 
r_y = 10 
For x = 1 To 20 

Rect r_x+(20*n),r_y+(20*n),50,50,0 

Next


Ok. You appear to be a newbie to the extreme. So lets look at this code step by step.

r_x = 10 
r_y = 10 


Those two lines I think you know. They set the variables r_x and r_y to an initial value of 10 each.

For x = 1 To 20 


This is the beginning of your loop. It initializes and defines how you want the loop to act. It initializes variable x to start at a value of 1 and through each iteration of the loop, increment x by one through value 20.

Next


That determines the end of your loop and sends the code back to the For statement to increment the x variable.

For x = 1 to 20 
  Print x 
Next


would print

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20


Since x is being incremented each time through the loop, you can use it's value for your rectangles to increment their x and y locations.

Rect r_x+(20*x),r_y+(20*x),50,50,0 


This line is your meat and potatoes of the loop. It draws your rectangles.

r_x+(20*x) is your x location and
r_y+(20*x) is your y location

r_x+(20*x) and r_y+(20*x) on the first iteration would equal 30 (r_x = 10 : 20 * 1 = 20... 20 + 10 = 30)

So on the first iteration it would draw a rectangle 50 pixels wide and 50 pixels high at location 30,30.

r_x+(20*x) and r_y+(20*x) on the second iteration would equal 50 (r_x = 10 : 20 * 2 = 40... 10 + 40 = 50)

So on the second iteration in would draw at 50,50 and so on through iteration 20 where it would draw the rectangle at 400,400 (10 + (20 * 20)).

That should, I hope clear up the whole loop + rectangle mystery. :)

Kanati

[EDIT] And I agree totally with CS_GUY... don't use _ in variable names. It makes them rather unreadable.