Computer Science Canada

[Tutorial] [Blitz] For loops

Author:  Cervantes [ Thu Feb 19, 2004 8:57 pm ]
Post subject:  [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
code:

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
code:

Next


Simple enough Razz

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!!!!!!!!!!!!!!!!!!!!!

code:

For number = 1 To 5
Print number
Next


You can also count by different intervals, using Step.

code:

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?!

code:

For number = 5 to 1 Step -2
Print number
Next


tricky huh?

Author:  jonos [ Thu Feb 19, 2004 9:23 pm ]
Post subject: 

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.

Author:  shorthair [ Thu Feb 19, 2004 9:30 pm ]
Post subject: 

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 ) Very Happy

Author:  shorthair [ Thu Feb 19, 2004 9:31 pm ]
Post subject: 

Psssst , just rename it with " ( Blitz ) " at hte end , to seperate it from dark in case anyone ever does some work with it

Author:  Cervantes [ Fri Feb 20, 2004 4:48 pm ]
Post subject: 

Sweet! I have the Shorthair seal of approval! Now all I need is a pic of it in my inventory Laughing

Jonos: try the following

code:

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 Razz Then I made an EVEN BETTER one!! (I know, its crazy!)

code:

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.

Author:  jonos [ Fri Feb 20, 2004 4:57 pm ]
Post subject: 

that is some nice stuff there!!! i will give up for now, but just wait...

Author:  jonos [ Wed Feb 25, 2004 5:40 pm ]
Post subject: 

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

Author:  shorthair [ Wed Feb 25, 2004 8:26 pm ]
Post subject: 

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


: