Code: Select all
;counting loop with variable Step value
Procedure loopy(aStart,aStop,aStep)
Debug "Cust: " + Str(aStart) + " To " + Str(aStop) + " Step " + Str(aStep)
If (aStep > 0 And aStop < aStart) Or (aStep < 0 And aStop > aStart) Or aStep = 0
ProcedureReturn aStart
EndIf
Protected aLoop = aStart
Repeat
;loop code performed here
Debug aLoop
;end of loop routines
aLoop + aStep
If aStep < 0
If aLoop < aStop
Break
EndIf
Else
If aLoop > aStop
Break
EndIf
EndIf
ForEver
ProcedureReturn aLoop
EndProcedure
;standard For/Next with nonvariable Step
Macro loopy2(aStart,aStop,aStep)
Debug "For: " + Str(aStart) + " To " + Str(aStop) + " Step " + Str(aStep)
For aLoop = aStart To aStop Step aStep
Debug aLoop
Next
EndMacro
;counting loop that uses floats
Procedure loopy3(aStart.f,aStop.f,aStep.f)
Debug "Float: " + StrF(aStart,2) + " To " + StrF(aStop,2) + " Step " + StrF(aStep,2)
If (aStep > 0 And aStop < aStart) Or (aStep < 0 And aStop > aStart) Or aStep = 0
ProcedureReturn aStart
EndIf
Protected aLoop.f = aStart
Repeat
;loop code performed here
Debug StrF(aLoop,2)
;end of loop routines
aLoop + aStep
If aStep < 0
If aLoop < aStop
Break
EndIf
Else
If aLoop > aStop
Break
EndIf
EndIf
ForEver
ProcedureReturn aLoop
EndProcedure
;test each possible loop with a selection of values
loopy(5, 60, 9)
loopy2(5, 60, 9)
loopy(5, 60, -9)
loopy2(5, 60, -9)
loopy(60, 5, 9)
loopy2(60, 5, 9)
loopy(60, 5, -9)
loopy2(60, 5, -9)
loopy(-5, -60, 9)
loopy2(-5, -60, 9)
loopy(-5, -60, -9)
loopy2(-5, -60, -9)
loopy(-60, -5, 9)
loopy2(-60, -5, 9)
loopy(-60, -5, -9)
loopy2(-60, -5, -9)
loopy(5, 5, 9)
loopy2(5, 5, 9)
loopy(5, 5, -9)
loopy2(5, 5, -9)
loopy(-5, -5, 9)
loopy2(-5, -5, 9)
loopy(-5, -5, -9)
loopy2(-5, -5, -9)
Debug ""
loopy3(5.2, 60.7, 9.3)
loopy3(5.2, 60.7, -9.3)
loopy3(60.7, 5.2, 9.3)
loopy3(60.7, 5.2, -9.3)
loopy3(-5.2, -60.7, 9.3)
loopy3(-5.2, -60.7, -9.3)
loopy3(-60.7, -5.2, 9.3)
loopy3(-60.7, -5.2, -9.3)
loopy3(5.2, 5.2, 9.3)
loopy3(5.2, 5.2, -9.3)
loopy3(-5.2, -5.2, 9.3)
loopy3(-5.2, -5.2, -9.3)
It is noted that if some slightly sloppy programming is not contrary to your style you can just modify the For/Next loop variable incrementing/decrementing it according to your needs. Even so, this has at least two noteworthy limitations: you have to do a special check to make sure the loop's ending conditions are not met before it starts (depends on the sign of the Step), and you are still limited to using integers (no float values for the Step).