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

Share your advanced PureBasic knowledge/code with the community.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

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

Post 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
scurrier
Enthusiast
Enthusiast
Posts: 169
Joined: Sat May 03, 2003 4:10 am

Post 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
LarsG
Enthusiast
Enthusiast
Posts: 713
Joined: Mon Jun 02, 2003 1:06 pm
Location: Norway
Contact:

Post by LarsG »

I think that PB meant this as a way of having the Step value variable, instead of a constant.. :)

AMD Athlon XP2400, 512 MB RAM, Hercules 3D Prophet 9600 256MB RAM, WinXP
PIII 800MHz, 320 MB RAM, Nvidia Riva Tnt 2 Mach 64 (32MB), WinXP + Linux
17" iMac, 1.8 GHz G5, 512 MB DDR-RAM, 80 GB HD, 64 MB Geforce FX 5200, SuperDrive, OSX
scurrier
Enthusiast
Enthusiast
Posts: 169
Joined: Sat May 03, 2003 4:10 am

Post 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
LarsG
Enthusiast
Enthusiast
Posts: 713
Joined: Mon Jun 02, 2003 1:06 pm
Location: Norway
Contact:

Post 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

AMD Athlon XP2400, 512 MB RAM, Hercules 3D Prophet 9600 256MB RAM, WinXP
PIII 800MHz, 320 MB RAM, Nvidia Riva Tnt 2 Mach 64 (32MB), WinXP + Linux
17" iMac, 1.8 GHz G5, 512 MB DDR-RAM, 80 GB HD, 64 MB Geforce FX 5200, SuperDrive, OSX
TronDoc
Enthusiast
Enthusiast
Posts: 310
Joined: Wed Apr 30, 2003 3:50 am
Location: 3DoorsDown

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

Post by TronDoc »

PB wrote:so here it is again.
nice tip. thank you. --jb
User avatar
chikega
User
User
Posts: 34
Joined: Fri Dec 04, 2020 3:19 am

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

Post 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
Gary E Chike DMD MS
'Experience is what you get when you don't get what you want' Image
eck49
Enthusiast
Enthusiast
Posts: 153
Joined: Sat Nov 14, 2020 10:08 pm
Location: England

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

Post 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.
Ubuntu 22.04 64-bit
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
User avatar
Kamarro
User
User
Posts: 65
Joined: Thu Mar 19, 2015 7:55 pm
Location: England

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

Post 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

Post Reply