[…] I don't know why Wirth changed his mind (minimalist as he was in that period, it must have been significant)
I now think
for-loops have a fixed step width, because
in mathematics ∑ and ∏ also are just ternary functions (initial value, final value, function [i.e. the subsequent statement])
presuming a step width of
1.
I like the way they did it in COBOL85. A less wordy variation of it would be:
FROM i := <start value> TO <end value> DO <statement>
Well, the nice thing about “explicitly” stating the direction
to/
downto is that you can write (possibly) “
empty loops” such as
for x := 0 to high(someDynamicArrayVariable) do
But I just came up with a relatively short notation that unambiguously tells increment from decrement, without needing the extra downto keyword or step/by -1 clause:
Could we keep the topic on the de facto state. If I wanna get some inspiration, I’ll head to
RosettaCode.
The best seems to me that we can change the value of index in the loop:for i := 0 to 10 do begin
...
i := i + 1; // not accepted now by the compiler
Certain situations remain undetected though like the
wiki article illustrates.
I'm not sure why FPC has the iterator in an undetermined state after loop exit, or do they mean that it depends on the code like whether or not the loop was shortcut with a break?
The
reason can be found in the
ISO standards:
After a for‑statement is executed, other than being left by a goto‑statement, the control-variable shall be undefined.
There are two kinds of loop with any step you like.
They are while and repeat.
Please read the topic. Using
conditional loops was suggested in the
3rd post, but this topic is specifically about
counting loops.