|
For | |||
For <.counterVar.> = <.initialValue.> To <.finalValue.> [Step <.stepValue.>] ... Next [counterVar] | |||
Parameters: NONE | |||
Returns: NONE | |||
A For-Next, unlike a Repeat-Until or While-EndWhile loop, requires you to specify explicitly the number of iterations you want the loop to cycle through. At the start of the first iteration the InitialValue is assigned to the counter variable (integer type). At the end of each iteration the counter is increased by 1, or if an optional Step statement is given, by the stepValue. If the value of the counter variable is greater than the finalValue, PlayBASIC exits the loop. If the initialValue is greater than the finalValue, stepValue should be a negative number, so that the counter will be decreased. In this case PlayBasic exits the loop if the value of the counter variable is less than finalValue. FACTS: * For-Next statements must be paired. * For-Next default to counting up from the start to end by one. If you want to count backwards or by a different value than one, then use the Step statement. * The counter variable may follow the Next statement, but is not necessary. * You can exit a For-Next loop anytime with the Exit or the ExitFor statement. * The For/Each/Next variant can be used to iterate through linked lists (Example bellow) For / Next Example: Examples of For-Next Loops
This code will output:
For Each (linked list) Example: Examples of For-Next Loops
|
Related Info: | Continue | Do | Each | Exit | ExitFor | List | Loops | Next | Repeat | Step | Types | While : |
|
|||||||||||||||||||||||||||||||||||||||
(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com |