For loops: Step 1 is not mandatory, but Step -1 is

Just starting out? Need help? Post your questions and find answers here.
User avatar
Kurzer
Enthusiast
Enthusiast
Posts: 693
Joined: Sun Jun 11, 2006 12:07 am
Location: Near Hamburg

For loops: Step 1 is not mandatory, but Step -1 is

Post by Kurzer »

Hi,
It's only a minor detail, but why does the “Step” parameter have to be specified explicitly for downward counting >For loops< and not for upward counting >For loops<?

If “For i = 10 To 0” automatically used a step of -1 in the same way that “For i = 0 To 10” uses a step of 1, it would be more logical.

Code: Select all

Debug "upward ----------"
For i = 0 To 10
	Debug Str(i)
Next i

Debug "downward --------"
For i = 10 To 0
	Debug Str(i)
Next i
Will result in

Code: Select all

[22:03:46] [Debug] upward ----------
[22:03:46] [Debug] 0
[22:03:46] [Debug] 1
[22:03:46] [Debug] 2
[22:03:46] [Debug] 3
[22:03:46] [Debug] 4
[22:03:46] [Debug] 5
[22:03:46] [Debug] 6
[22:03:46] [Debug] 7
[22:03:46] [Debug] 8
[22:03:46] [Debug] 9
[22:03:46] [Debug] 10
[22:03:46] [Debug] downward --------

But as I said, it's nitpicking... but there are some situations where you first look for the “error” elsewhere instead of in the For loop.
PB 6.12 x64, OS: Win 11 24H2 x64, Desktopscaling: 150%, CPU: I7 12700 H, RAM: 32 GB, GPU: Intel(R) Iris(R) Xe Graphics | NVIDIA GeForce RTX 3070, User age in 2025: 57y
"Happiness is a pet." | "Never run a changing system!"
User avatar
spikey
Enthusiast
Enthusiast
Posts: 773
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: For loops: Step 1 is not mandatory, but Step -1 is

Post by spikey »

The original Basic didn't have a step clause, all for's were an implicit +1 (Dartmouth - 1964). It's implemented as an optional clause in subsequent Basics, with a default +1 inferred where omitted because this is a really common case. However, the step must be determinate in expressions like this where the specific start and end points aren't inferable to the compiler at compile time:

Code: Select all

For x = y to z
; …
Next x
Otherwise, you get the runtime bug you illustrate.

More complex logic isn't built into the next implementation because this would slow down default loops, this was an issue on early microprocessors!

But yes, there's a missed opportunity for a compile time error in static cases there.
hoerbie
Enthusiast
Enthusiast
Posts: 137
Joined: Fri Dec 06, 2013 11:57 am
Location: DE/BY/MUC

Re: For loops: Step 1 is not mandatory, but Step -1 is

Post by hoerbie »

I think an automatic "Step -1" would be a problem for a lot of programs, when there are calculated "from" and "to" values in vars, something like this:

Code: Select all

fstart = 19 ;comes from a former calculation
fend = 12 ;comes from another former calculation
For i = fstart To fend
  ;do something
Next i
With a default of "Step 1" the programmer can trust, that the loop with above calculated values isn't executed.

But if there would be an automatic "Step -1" then the loop would be executed 8 times, or the programmer would have to check both values with an additional If clause before.
User avatar
SPH
Enthusiast
Enthusiast
Posts: 584
Joined: Tue Jan 04, 2011 6:21 pm

Re: For loops: Step 1 is not mandatory, but Step -1 is

Post by SPH »

Code: Select all

For i=1 To 10 Step 1 ; that's ok too
  Debug i
Next

!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Portable LENOVO ideapad 110-17ACL 64 bits
Version de PB : 6.12LTS - 64 bits
User avatar
Piero
Addict
Addict
Posts: 1016
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: For loops: Step 1 is not mandatory, but Step -1 is

Post by Piero »

Why all these dissertations about "low-level" trivial details?
Let's impose a "high-level" PB to Fred!
(I will probably get banned for this)
User avatar
mk-soft
Always Here
Always Here
Posts: 6284
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: For loops: Step 1 is not mandatory, but Step -1 is

Post by mk-soft »

I don't know why that's a problem. Or I'm too old.
For me, this is completely normal with "Step x" and nothing needs to be changed.

I rather see a problem with automatic negative step, if there are also conditions where you should not go through the loop.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Olli
Addict
Addict
Posts: 1251
Joined: Wed May 27, 2020 12:26 pm

Re: For loops: Step 1 is not mandatory, but Step -1 is

Post by Olli »

mk-soft wrote: Sat Oct 11, 2025 10:35 pm I don't know why that's a problem. Or I'm too old. [...]
:P
Thank you for humour !

Here comes the start of my research of my final epitaph : << I did not know why that was a problem... Or maybe I was too old... Anyway, I rest in peace. Do not urinate. >>

I hope you are not too old, just a miss of context in the first message.

@kurzer

The ASM backend "For" statement is broken. I did not study if this has changed for the C backend, but if it has not changed, it is a fair wish.

Definition of "broken" here (important !).
1) Step accepts constants only.
2) The final value, however, accepts far more, to a whole math expression, what it leaks performances in the short loops.

If we count the types of values, it exists 3 types :
1) constant type
2) variable type
3) expression type

Also, we have 3 values :
1) initial value
2) final value
3) step value
[4 : maximum of iterations]

This means the compiler might manage 3^3 types of loop.
So, ideally, 27 systems of pre-processing security have to be studied.

And your suggestive idea could be available in 1 or more, of the 27 ways, in particular, when the final value is initially known, what it allows the compiler to know if the sense is positive or negative.

A fourth value could be intersting : the maximum quantity of iterations. Falcultative or required, depending the way.

At last, in the way, the initial value and the final value are constant, your suggest seems to be able to exist.
User avatar
Kurzer
Enthusiast
Enthusiast
Posts: 693
Joined: Sun Jun 11, 2006 12:07 am
Location: Near Hamburg

Re: For loops: Step 1 is not mandatory, but Step -1 is

Post by Kurzer »

I understand the technical background, the historically developed behavior, and also that people may have gotten used to it and that a change could probably break some code. And even if mk-soft says, “I don't understand the problem,” I can understand that in one way or another. Namely, if you've been using the language for a long time and know it inside and out.

What I wanted to point out is the obvious, logical inconsistency. Someone who is new to PB and doesn't know this peculiarity, nor what Mr. Dartmouth came up with in 1964, might stumble at this point. And I admit that I also fell for it for a moment when I wanted to draw something from right to left in a canvas gadget, which previously required a more complex calculation. At first, I thought that my calculation result was incorrect and that was why nothing was drawn in the canvas. Until I ran the short loops shown above to check.

If you look at these two types of loops completely impartially, then you simply *have* to know that one does not require a step, but the other does. The illogicality that I see in this is due to the fact that this specific behavior cannot be deduced from any obvious characteristics of the For loops.

Olli seems to have done even more research here. I wasn't aware that the for loop was "that bad". Among other things, Olli wrote: "And your suggestive idea could be available in one or more of the 27 possibilities, especially if the end value is known at the beginning, which allows the compiler to recognize whether the meaning is positive or negative."

In my example, the start and end values were definitely known, as I specified them directly as numbers. Therefore, in this case, I can't really imagine how the loop is implemented behind the scenes to conclude that a downward For loop from 10 to 0 is not executed at all - but in the opposite case, it cheerfully counts from 0 to 10 with Step +1.

I can't find a really suitable analogy, but the effect for an inexperienced user would be like wanting to eat a plate of spaghetti and the fork is to the left of the plate – everything works, you can eat the pasta without any problems. But if the fork is initially to the right of the plate and you want to use it, it always folds up like a pocket knife for no apparent reason, without you being able to see how this mechanism works – making it impossible to eat the pasta.

Possible reaction? Shit, why can I only eat pasta with this thing when it's to the left of the plate? I don't understand.
PB 6.12 x64, OS: Win 11 24H2 x64, Desktopscaling: 150%, CPU: I7 12700 H, RAM: 32 GB, GPU: Intel(R) Iris(R) Xe Graphics | NVIDIA GeForce RTX 3070, User age in 2025: 57y
"Happiness is a pet." | "Never run a changing system!"
AZJIO
Addict
Addict
Posts: 2211
Joined: Sun May 14, 2017 1:48 am

Re: For loops: Step 1 is not mandatory, but Step -1 is

Post by AZJIO »

Then you'll have to always specify the direction, because if you want to prevent the loop from going in the opposite direction, you'll have to explicitly set it. So, in 99% of cases where you didn't use the "Step" keyword, you'll now have to specify it every time, even if the loop is regular.
DarkDragon
Addict
Addict
Posts: 2346
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: For loops: Step 1 is not mandatory, but Step -1 is

Post by DarkDragon »

If you had a changing step depending on the order of start/end values you would have to add "Step 1" for the case when your end value potentially overflows the integer space.

Hence an implicit change of behavior depending on the begin/end values would lead to the question "why is the for loop behavior changing?"
bye,
Daniel
normeus
Enthusiast
Enthusiast
Posts: 474
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: For loops: Step 1 is not mandatory, but Step -1 is

Post by normeus »

I see your point of FOR loop guessing direction of STEP (1 or -1 ) depending on <expression1> To <expression2>
The compiler would have to add an IF before every FOR statement that does not have a STEP and that sounds like a horrible mess
I guess, I am also too old since I feel STEP 1 is set in stone.

From the Help file:

Code: Select all

For : Next is used to create a loop within a program with the given parameters.
 At each loop the <variable> value is increased by a 1,
  (or of the "Step value" if a Step value is specified) and when the <variable> value is above the <expression2> value, the loop stop. 
  
When I was a young fellow, your punched cards were kept in a shoe box and it was called a program
Norm.
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
Post Reply