For/Next Loop with Step doesn't excute properly

Just starting out? Need help? Post your questions and find answers here.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

well ok, I'm convinced that it is not bottom-controlled.

but

Code: Select all

LStart = 10
LStop  = 20
LStep  = -1
LCount = LStart
While LCount >= LStart And LCount <= LStop   ; For LCount = 10 To 20 Step -1

  Debug "Step " + Str(LCount) + ", clap your hands!"

LCount + LStep    ; Next
Wend
Top Controlled, next step at the end....

executed at least once.
oh... and have a nice day.
Mahan
User
User
Posts: 35
Joined: Sun Jan 25, 2009 10:12 am
Location: Sweden

Post by Mahan »

Top Controlled, next step at the end....

executed at least once.
Agree 100%. That's what I meant when I wrote:
... a while..wend type loop are very easy to rig for 1 single forced run-through ...
In the end it's clearly a matter of tastes (as some already noted), but it's easy to adapt PB to do what we want it to anyways :-)

edit: or more correctly "adapt our code to PB so it does what we want."
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

I just wanted to discuss what should be called basic behaviour of a For-Loop.

skipping the first execution when the Iteration does not meet the Interval, is an additional feature.

ts gave a good example. same applies in stepping thru a string.
it's really handy, it increases the usability of the For-Loop, I really like it.

but it's no basic behaviour, it's an additional feature.
oh... and have a nice day.
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Post by djes »

Mahan wrote: Maybe it's a matter of habit but I almost never write repeat..until loops anymore. I only use while..wend, for..next and foreach..next loops.
I almost never use While...Wend loops :)
Maybe because I have the ASM habit, generally the test is after the code to avoid a jump.
Psych
Enthusiast
Enthusiast
Posts: 239
Joined: Thu Dec 18, 2008 3:35 pm
Location: Wales, UK

Post by Psych »

Interesting...

As an old VB programmer I cannot think of a single time I didn't need to check the values first when using for next loops with non-static values, so I am in favour of the way PB handles them, it's less steps for the programmer, IMHO. Classic basic never checked if the 'range' was invalid and so always iterated once, meaning you could execute once even if the end condition was met (or exceeded).
As for While and Repeat, this is why we have them both, I am guessing, so we can decide whether to check before or after the code block, it's all about preference and how your particular program flow is configured.
In summary, I prefer this 'pre-loop' checking on For loops, makes more sense to me.
----------------------------------------------------------------------------
Commenting your own code is admitting you don't understand it.
----------------------------------------------------------------------------
Post Reply