Page 1 of 1

For loop using variable step.

Posted: Sun Sep 30, 2012 8:21 pm
by jassing
I was surprised I could not do this:

Code: Select all

For i = 1 To Len(cString) Step nBreak
Wishlist is to allow step to allow a dynamic runtime set value, ok if it becomes 'static' after initial use. (ie: nBreak shouldn't change once the for loop is started)

Re: For loop using variable step.

Posted: Sun Sep 30, 2012 8:36 pm
by ts-soft
This was discussed at many Time.
The step is a fix value, it is set on compiletime, not at runtime. You can use a constant.
If Fred change this, the for:next is slower and can't optimized.

Why not use While : Wend or Repeat : Until.

Re: For loop using variable step.

Posted: Sun Sep 30, 2012 9:20 pm
by Tenaja
There are some additional options here:
http://purebasic.fr/english/viewtopic.p ... 6&p=360605

Re: For loop using variable step.

Posted: Mon Oct 01, 2012 6:33 pm
by Seymour Clufley
And, IIUC, that code is inefficient because it will re-check the length of the string which each iteration of the loop. You should put it in a variable first.

Re: For loop using variable step.

Posted: Thu Oct 04, 2012 7:58 pm
by Nituvious
What about something like this:

Code: Select all

myStep = 1
While (i<=15 - myStep): i + myStep
	
	If i = 5 : myStep = 5 : EndIf
	
	Debug "Loop count: " + Str(i)

Wend
[edit] oops, sorry. Looks like the linked thread already discusses this.

Re: For loop using variable step.

Posted: Fri Oct 05, 2012 8:15 am
by Lord
ts-soft wrote:This was discussed at many Time.
The step is a fix value, it is set on compiletime, not at runtime. You can use a constant.
If Fred change this, the for:next is slower and can't optimized.

Why not use While : Wend or Repeat : Until.
If I remember correctly, Fred suggested something like this:

Code: Select all

A.f=1
E.f=10
myStep.f=0.5
For i=A To E Step 0
;Do whatever you want to do  
  Debug i
  i+myStep
Next
But I think, this is not the answer to the original question. :D

Re: For loop using variable step.

Posted: Fri Oct 05, 2012 7:05 pm
by Josh
jassing wrote:

Code: Select all

For i = 1 To Len(cString)
To use Len in a loop can quickly end in a disaster. Ok, if it's a short string, you will not check it. But for each doubling the len of the string, the running time will be about four times longer.

Code: Select all

cString.s = Space (10000) ;test also with 20k and 40k

time = ElapsedMilliseconds()

For i = 1 To Len(cString)
  x = 1
Next

MessageRequester ("", Str (ElapsedMilliseconds() - time))

Re: For loop using variable step.

Posted: Sat Oct 06, 2012 3:29 am
by MachineCode
Josh is right. You don't call Len() repeatedly if the size doesn't change. Calc it once, and use that value in the For/Next loop. In Josh's example above, and using Space(40000), the loop takes 700 ms on my PC. But if I calc Len() first and use that value in the loop, the code executes in 0 ms (instantly).

Code: Select all

cString.s = Space (40000) ; 40k

time = ElapsedMilliseconds()

l=Len(cString)

For i = 1 To l
  x = 1
Next

MessageRequester ("", Str (ElapsedMilliseconds() - time))