
-----------------------------------
Cervantes
Thu Feb 19, 2004 8:57 pm

[Tutorial] [Blitz] For loops
-----------------------------------
For Loops

what they are

For loops are used to execute a code between the 'for' and the 'next' lines.  The code is executed a set number of times, decided by the parameters you enter at the 'for' line.  For loops also keep track of how many times the code has been repeated in a variable (that doesn't need to be declared beforehand)

Syntax

the first line

For qwerty = 1 To 10


qwerty can be anything you want, as long as it is not a command or already used variable.  It is the counter.  1 To 10 sets the number that qwerty begins at, and 10 sets the number that qwerty ends at.  This for loops will execute code 10 times.

The last line

Next


Simple enough :P

you can put whatever code you want between there.  It's totally up to you!

Here's an example of code that counts to 5!!!!!!!!!!!!!!!!!!!!!


For number = 1 To 5
Print number
Next


You can also count by different intervals, using Step.


For number = 1 To 5 Step 2
Print number
Next



"Cervantes!  How do I do 'decreasing' like in good ol' turing?!"
You said it!  How do you do decreasing?!


For number = 5 to 1 Step -2
Print number
Next


tricky huh?

-----------------------------------
jonos
Thu Feb 19, 2004 9:23 pm


-----------------------------------
wow, nice tutorial, really makes me understand the syntax. 

is there a need for a 'decreasing' word like in turing for a decreasing for loop, or can it just be the same just with a higher first number and lower second number.

-----------------------------------
shorthair
Thu Feb 19, 2004 9:30 pm


-----------------------------------
You are doing such an awsome job guys , thanks so much for hte support in here ,these tutorials rock , they are alot easier to understand , unlike mine THIS  is awsome you have my seal of approval ( if it means anything to you ) :D

-----------------------------------
shorthair
Thu Feb 19, 2004 9:31 pm


-----------------------------------
Psssst , just rename it with " ( Blitz ) " at hte end , to seperate it from dark in case anyone ever does some work with it

-----------------------------------
Cervantes
Fri Feb 20, 2004 4:48 pm


-----------------------------------
Sweet!  I have the Shorthair seal of approval!  Now all I need is a pic of it in my inventory :lol:

Jonos: try the following


For i = 1 To 5
  Print i
Next

For k = 5 To 1
  Print k
Next


It doesn't give you an error, it just doesn't do it.


as for why we need decreasing for loops?

You wrote that lil program that made a boarder, then I made a better one :P  Then I made an EVEN BETTER one!!  (I know, its crazy!)


Graphics 400, 400 
SetBuffer BackBuffer()
x1 = 350
y1 = 50
x2 = 50
y2 = 350
For k = 0 To 350 Step 3
  x1 = x1 - 3
  y1 = y1 + 3
  x2 = x2 + 3
  y2 = y2 - 3
  Line 50, y1, 50, y1 + 3
  Line x1, 50, x1 - 3, 50
  Line 350, y2, 350, y2 - 3
  Line x2, 350, x2 + 3, 350
  Flip
Next

x1 = 0
y1 = 400
x2 = 400
y2 = 0

For k = 0 To 350 Step 3
x1 = x1 + 3
y1 = y1 - 3
x2 = x2 - 3
y2 = y2 + 3

For i = 0 To 50 Step 3
  Plot x1, i
  Plot i, y1
  Plot x2, i + 350
  Plot i + 350, y2
Next
Flip
Next
;;;;;;;;;
While Not KeyHit (1)
Wend
End


That would have been easier with decreasing for loops.  esp the first part.  instead of using x1 and y1 and x2 and y2, I could have just used the for variable.

-----------------------------------
jonos
Fri Feb 20, 2004 4:57 pm


-----------------------------------
that is some nice stuff there!!! i will give up for now, but just wait...

-----------------------------------
jonos
Wed Feb 25, 2004 5:40 pm


-----------------------------------
hey... i found how to do decreasing, i was looking throught the blitz tutorials and i found this, straight from the help, i didn't right it:

You can also step backwards by placing a minus (-) sign after the command with the number of steps backwards you would like to take:

For A = 10 To 0 step -2
Print A
Next

This would print: 10 8 6 4 2 0

-----------------------------------
shorthair
Wed Feb 25, 2004 8:26 pm


-----------------------------------
Thats one way to do it , there is a more complex way and it allowy you to add some more cool options , its used when decreasing in hte z axis , but your very right on that decreasing loop , MAKE IT A TUTORIAL
