For Next Counter

Community Forums/General Help/For Next Counter

Yue(Posted 2015) [#1]
Send me Somebody help, can not understand when to use the meter after a For Next loop counter .

What it does?


Who was John Galt?(Posted 2015) [#2]
Don't do it.


Yue(Posted 2015) [#3]
??

What I do ?

I wonder at what point the counter is used to end a loop Next

counter is optional.


videz(Posted 2015) [#4]
@Yue,

you end your counter to iterate all items in your array or how many times you like to reiterate until the last item or number of items is processed. depends up to you.

Using STEP get things more complicated but try with the basics first.

You can use while wend and add a counter inside..

counter = 0
while counter <= 10
 ...
 counter = counter + 1
wend



Yue(Posted 2015) [#5]
Is That I'm Trying to endeder the loop to the next .

But optionally Following the end , I can Restarting the variable. And no logo understand when is that I do this and under what conditions.



For K = 1 to 10

   ....

Next K



videz(Posted 2015) [#6]
I can't really understand what you would like to know or what you're trying to achieve Yue. If you can give some example it would be more clear.


Yue(Posted 2015) [#7]
I just want understand this.

http://support.microsoft.com/kb/36809/en-us


Yasha(Posted 2015) [#8]
That article has nothing to do with Blitz. It is about a different programming language. There is no useful information about Blitz in Microsoft's documentation.

Information about one Basic "dialect" cannot be applied to others. They are completely different languages with no connection to each other.

Just don't worry about it and keep doing whatever you were doing before.


Matty(Posted 2015) [#9]
Ahhh....that article basically means this:


Say we have a loop:


for i = 0 to 10
;do stuff in here
next

- that is all fine and good.

If we put a loop inside another loop we could do it like this:
for i = 0 to 10
for j = 0 to 10
;do stuff in here

next
next

- that is all fine as well.


However,

Say I used the same loop counter variable "i" in both loops.....


for i = 0 to 10
for i = 0 to 10
;do stuff in here
next
next


This will run differently than you would expect.

Because blitz doesn't define the variable "i" as local to each loop what will happen in blitz is that the inner loop will run once, the variable "i" will be greater than 10 on exiting the inner loop and then will jump out of the outer loop as well.


GfK(Posted 2015) [#10]
I feel like I've wandered into the middle of Bill & Teds Bogus Journey.


Zethrax(Posted 2015) [#11]
The loop ends when the index variable has the same or greater value as the value after the 'To' keyword.

eg.
For index_variable = 1 To 10
Print index_variable
Next

will count from 1 to 10 in units of 1.

If you want to count in different units then add the Step keyword after the 'To' value, followed by the Step value you want to use (note that the Step value must be a constant or literal value. It can't be a variable value.).

eg.
For index_variable = 2 To 8 Step 2
Print index_variable
Next

will count from 2 To 8 in units of 2 (2, 4, 6, 8).

If you want to break out of the loop before it is finished then use the 'Exit' keyword.

eg.
For index_variable = 1 To 10
If index_variable = 5 Then Exit
Print index_variable
Next

will count from 1 To 5 before exiting the loop.

If you have multiple nested loops then you'll need to use a variable as a flag to force exiting them.

eg.
do_exit = False
For y = 1 To 10
For x = 1 To 10
If x = 5 Then do_exit = True : Exit
Print x + " | " + y
Next
If do_exit Then Exit
Next