Page 1 of 1

Tip: How to do For/Next with a variable Step

Posted: Fri Mar 12, 2004 4:51 am
by PB
Code updated for 5.20+

I thought I posted this as a standalone tip before, but I can't find it now...
so here it is again. (I originally had it posted as part of this longer thread:
viewtopic.php?t=4930).

Code: Select all

; Example of 'For a=1 To 40 Step b' in PureBasic,
; because you can't use variables for 'Step' yet.

b = 2            ;The 'Step' value (normally a constant!).
For a = 1 To 40
  Debug a        ;Do whatever processing on 'a' here.
  a + b - 1      ;This MUST go directly before 'Next'!
Next

Posted: Fri Mar 12, 2004 1:36 pm
by scurrier
this is out of the help

For : Next

Syntax

For <variable> = <expression1> To <expression2> [Step <constant>]
...
Next [<variable>]

or is this just for windows and not linux?

Sean

Posted: Fri Mar 12, 2004 1:38 pm
by LarsG
I think that PB meant this as a way of having the Step value variable, instead of a constant.. :)

Posted: Fri Mar 12, 2004 1:41 pm
by scurrier
i think i know what your talking about now
but you can do this

#stp=2
For x =1 To 100 Step #stp
Debug x
Next

Posted: Fri Mar 12, 2004 2:12 pm
by LarsG
scurrier wrote:i think i know what your talking about now
but you can do this

#stp=2
For x =1 To 100 Step #stp
Debug x
Next
Yes, but #stp=2 is still a constant...
I believe PB meant it like this..:
(just a pseudo example)

Code: Select all

if a = 1
    b = 2
else
    b = 1
endif
for i = 1 to 100 ;"step b" won't work here...
    ; code here.. blah blah
    i + b - 1
next

Re: Tip: How to do For/Next with a variable Step

Posted: Sat Mar 13, 2004 3:23 am
by TronDoc
PB wrote:so here it is again.
nice tip. thank you. --jb

Re: Tip: How to do For/Next with a variable Step

Posted: Tue Jul 13, 2021 4:18 pm
by chikega
Interestingly enough, I asked a similar question concerning the Haxe language which does not appear to support Stepping at all using the For loop. The solution was to use the While loop instead.

I thought I would try out the While/Wend loop in PB and it appears to work :D

Code: Select all

b = 2   ; Step value
While a <= 10 
  Debug a
  a + b 
Wend

Re: Tip: How to do For/Next with a variable Step

Posted: Wed Aug 11, 2021 3:20 pm
by eck49
Beware! a Continue statement in the loop will miss changing the value of a, so this would need to be duplicated...

Code: Select all

b = 2   ; Step value
While a <= 10 
  Debug a
  ;some code
  if somecondition 
    a + b : Continue
  EndIf
  ;more code  
  a + b 
Wend
But if b might be either positive or negative...
A Macro might help keep things tidy, and using a Repeat/Forever instead of a While/Wend

Code: Select all

a = ??   ; start value
b = ??   ; Step value
c = ??   ; Test value for end loop
Macro StepAndTest()
  a + b
  If ((b > 0) and (a >= c)) or ((b < 0) and (a <= c))
    Break
  EndIf  
EndMacro  
Repeat
  Debug a
  ;some code
  If somecondition 
    StepAndTest() : Continue
  EndIf
  ;more code  
  StepAndTest()
ForEver
UndefineMacro StepAndTest
There are two bonuses when compared to a conventional For/Next/Step loop, apart from having a variable step. One is that all the loop variables don't have to be integers (that has its dangers, too, of course : 1.1 + 2.3 is only closely approximate to 3.4). The other is that the step variable can be changed inside the loop.

Re: Tip: How to do For/Next with a variable Step

Posted: Mon Jan 23, 2023 5:25 pm
by Kamarro
Hi,

Probably I'm too late to say this, but it may be useful for late ones.
This is a simple question, with a tricky but very simple solution.
I include a Random situation to make the value of "b" change somehow, but you may use any lines of code that it will work fine!
Try this sample:

Code: Select all


b=1
For a=1 To 40 
   Debug a                ; This is the output of the cicle "For a=1 to 40 step b"
   If Random(10)>7   ; This IF...The...Else is only to simulate a randomized change of the step value
      b=5                    ; You my use any value here, any code, any conditions, or even a random value
   Else
      b=1                    ; To return to an original value if needed
   EndIf
   a=a+b-1                 ; -1 is necessary to exclude the regular addition of 1 by the Next instruction
Next a


Re: Tip: How to do For/Next with a variable Step

Posted: Sat Apr 06, 2024 12:18 am
by tikidays
Why would purebasic not allow a variable as a step? This seems out of step with every other basic Ive used. Make things more complicated. :(