For...Next Step Variable

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by ebs.

Fred,

I don't know if this was requested before, but please make the Step value in a For...Next loop a *variable*, not a constant. I often want to set the step based on other variables, and also be able to "reverse direction" in the loop by using: "StepValue = -StepValue".

Regards,
Eric
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by VPureBasic.

Hi ebs,

Why you did not try something like...

For x = A to B step C
...
...
Next x

A = ( your start )
B = ( your limit )
C = ( your step value )

Roger
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

> Why you did not try something like...
> For x = A to B step C

Doesn't work. Compiler complains about C not being a constant.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tinman.
Originally posted by ebs

I don't know if this was requested before, but please make the Step value in a For...Next loop a *variable*, not a constant. I often want
Better making it an expression (so you can do e.g. "... Step foo+bar(blah)" :)



--
It's not minimalist - I'm increasing efficiency by reducing input effort.
(Win98first ed. + all updates, PB3.50)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Kendrel.

actually it works if the step variable is a constant or a fixed number... but it would be nice if one could also use a variable.

my 2 cents

kendrel
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

I take good notes.

Fred - AlphaSND
Master Games
User
User
Posts: 22
Joined: Mon Oct 06, 2003 1:42 am

Post by Master Games »

Fred, is this feature in the next version? or can it still be added if it is not?
Thanks,
Master Games

System: P4 1.9 GHZ, 1 GB DDR Memory, 80 GB Hard Drive, WinXP Home Edition with Latest Patch, Creative labs Audigy Sound Blaster Platinum Sound Card, Geforce 3 Graphics card with 52.16 drivers
Fred
Administrator
Administrator
Posts: 16619
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Not implemented for now...
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Post by GPI »

Fred wrote:Not implemented for now...
But would be a good idea (and i can't imagine, that it is a big problem)
Master Games
User
User
Posts: 22
Joined: Mon Oct 06, 2003 1:42 am

Post by Master Games »

GPI wrote:
Fred wrote:Not implemented for now...
But would be a good idea (and i can't imagine, that it is a big problem)
I second that! Since C allows you to do it.
Thanks,
Master Games

System: P4 1.9 GHZ, 1 GB DDR Memory, 80 GB Hard Drive, WinXP Home Edition with Latest Patch, Creative labs Audigy Sound Blaster Platinum Sound Card, Geforce 3 Graphics card with 52.16 drivers
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

Until Fred implements 'Step' as a variable, you can always do it like this:

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
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6161
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

oh, pb, use repeat until or while wend instead! brrr...

; for a = 1 to 40 step b

a = 1
while a+b <= 40
; whatever
a = a+b
wend

or

a=1
repeat
; whatever
a = a+b
until a > 40
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> use repeat until or while wend instead

But then it's not a For/Next loop with a variable anymore, is it? :lol:
dontmailme
Enthusiast
Enthusiast
Posts: 537
Joined: Wed Oct 29, 2003 10:35 am

Post by dontmailme »

For - Next works well as it is
While - Wend and repeat - until can do the step.....

Everythings quick !

Why change a winning formula ?

This isn't TrueBasic, or a TRUE Basic, it's a very fast basic !

Why do we need the variable step, if it will slow the loop down ?
Paid up PB User !
Froggerprogger
Enthusiast
Enthusiast
Posts: 423
Joined: Fri Apr 25, 2003 5:22 pm
Contact:

Post by Froggerprogger »

Of course the compiler could&should watch for the step-parameter if it is a const or a variable and then compile to the fast loop as always if it is a const or to another loop if it is a variable...

Anyway, I personally don't need step-variables, it would only be a nice gimmick.
%1>>1+1*1/1-1!1|1&1<<$1=1
Post Reply