Recent

Poll

I want for-loops with custom step widths (plural!).

false
24 (42.1%)
true
27 (47.4%)
42
6 (10.5%)

Total Members Voted: 57

Author Topic: for loop with custom step width (esp. ≠ 1)  (Read 51315 times)

Kays

  • Hero Member
  • *****
  • Posts: 574
  • Whasup!?
    • KaiBurghardt.de
Re: for loop with custom step width (esp. ≠ 1)
« Reply #75 on: December 13, 2021, 02:14:10 am »
[…] 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
Code: Delphi  [Select][+][-]
  1. 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:
Code: Pascal  [Select][+][-]
  1. for i := 0 to 10 do begin
  2. ...
  3. 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:
Quote
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.
Yours Sincerely
Kai Burghardt

munair

  • Hero Member
  • *****
  • Posts: 798
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: for loop with custom step width (esp. ≠ 1)
« Reply #76 on: December 13, 2021, 08:16:51 am »
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
Code: Delphi  [Select][+][-]
  1. for x := 0 to high(someDynamicArrayVariable) do

Is this de-facto then?
keep it simple

PascalDragon

  • Hero Member
  • *****
  • Posts: 5462
  • Compiler Developer
Re: for loop with custom step width (esp. ≠ 1)
« Reply #77 on: December 13, 2021, 02:52:03 pm »
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 iterator being max + 1 or min - 1 after loop exit comes naturally with the iteration. After all, the iterator is tested against the maximum/minimum value and the loop ends if the iterator is greater or less respectively.

In the case of the loop not being left with a Break or similar then the compiler can optimize the loop (e.g. flip it from 0-to-count to count-to-0) in which case the value represented by the index variable is only a calculated one (e.g. count-idx in case of the example of flipping).

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
Code: Delphi  [Select][+][-]
  1. for x := 0 to high(someDynamicArrayVariable) do

Is this de-facto then?

The documentation explicitly states:

Quote
  • In the case To is used, if the initial value is larger than the final value then Statement will never be executed.
  • In the case DownTo is used, if the initial value is less than the final value then Statement will never be executed.

For those interested: there is an old patch (even older than this discussion ;) ) that adds support for a by-clause to the for-loop. The patch won't apply on main, but it should be possible to rebase this...

munair

  • Hero Member
  • *****
  • Posts: 798
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: for loop with custom step width (esp. ≠ 1)
« Reply #78 on: December 13, 2021, 03:48:49 pm »
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 iterator being max + 1 or min - 1 after loop exit comes naturally with the iteration. After all, the iterator is tested against the maximum/minimum value and the loop ends if the iterator is greater or less respectively.

In the case of the loop not being left with a Break or similar then the compiler can optimize the loop (e.g. flip it from 0-to-count to count-to-0) in which case the value represented by the index variable is only a calculated one (e.g. count-idx in case of the example of flipping).

Thanks, got it.
keep it simple

munair

  • Hero Member
  • *****
  • Posts: 798
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: for loop with custom step width (esp. ≠ 1)
« Reply #79 on: December 13, 2021, 03:51:09 pm »
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
Code: Delphi  [Select][+][-]
  1. for x := 0 to high(someDynamicArrayVariable) do

Is this de-facto then?

The documentation explicitly states:

Quote
  • In the case To is used, if the initial value is larger than the final value then Statement will never be executed.
  • In the case DownTo is used, if the initial value is less than the final value then Statement will never be executed.

I wasn't referring to the code.
keep it simple

PascalDragon

  • Hero Member
  • *****
  • Posts: 5462
  • Compiler Developer
Re: for loop with custom step width (esp. ≠ 1)
« Reply #80 on: December 14, 2021, 09:25:17 am »
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
Code: Delphi  [Select][+][-]
  1. for x := 0 to high(someDynamicArrayVariable) do

Is this de-facto then?

The documentation explicitly states:

Quote
  • In the case To is used, if the initial value is larger than the final value then Statement will never be executed.
  • In the case DownTo is used, if the initial value is less than the final value then Statement will never be executed.

I wasn't referring to the code.

Then what were you referring to?

Thaddy

  • Hero Member
  • *****
  • Posts: 14361
  • Sensorship about opinions does not belong here.
Re: for loop with custom step width (esp. ≠ 1)
« Reply #81 on: January 13, 2022, 11:07:36 am »
Side note: My hack published in UNDU becomes 25 years old this year, 8-) and I still feel that something like step should be supported, Note the hack still works.
(And is still unsafe.... ;D )
Pity UNDU is not on the wayback machine. It was great while it lasted.
« Last Edit: January 13, 2022, 11:12:58 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1313
Re: for loop with custom step width (esp. ≠ 1)
« Reply #82 on: January 13, 2022, 11:32:11 am »
I think it depends on if you think of them as offsets or steps. If it is a pointer, you need an offset. If they are steps, you want a certain amount. You can switch between them by multiplying or dividing by the record size. IMO, steps are more general purpose. So, I see no problem with the current setup.

Akira1364

  • Hero Member
  • *****
  • Posts: 561
Re: for loop with custom step width (esp. ≠ 1)
« Reply #83 on: January 14, 2022, 10:18:18 pm »
So we don't have to mess Pascal with any C syntax.

The actual "loop keyword step" syntax mostly being discussed here has precisely zero to do with C. It was literally a feature included in Niklaus Wirth's Algol W in the 60s: `for i := 0 step 2 until 10 do write(i);`

So indeed the guy who invented Pascal "approves" of this sort of thing very directly.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6682
Re: for loop with custom step width (esp. ≠ 1)
« Reply #84 on: January 15, 2022, 09:18:26 am »
So we don't have to mess Pascal with any C syntax.

The actual "loop keyword step" syntax mostly being discussed here has precisely zero to do with C. It was literally a feature included in Niklaus Wirth's Algol W in the 60s: `for i := 0 step 2 until 10 do write(i);`

So indeed the guy who invented Pascal "approves" of this sort of thing very directly.

Including having it in Modula-2. As I've said before, Pascal was basically a rush job.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

mischi

  • Full Member
  • ***
  • Posts: 170
Re: for loop with custom step width (esp. ≠ 1)
« Reply #85 on: January 15, 2022, 12:43:07 pm »
Including having it in Modula-2.
MarkMLl
as Fortran and Basic. Whether C has it or not, should not be an argument per se. Not everything of C is inherently bad 😉 Even though I "hate" C as well. Preferably stick to arguments about facts.

My 2 cents only - Mischi

MarkMLl

  • Hero Member
  • *****
  • Posts: 6682
Re: for loop with custom step width (esp. ≠ 1)
« Reply #86 on: January 15, 2022, 01:00:58 pm »
as Fortran and Basic. Whether C has it or not, should not be an argument per se. Not everything of C is inherently bad 😉 Even though I "hate" C as well. Preferably stick to arguments about facts.

Yes, but they were neither designed nor implemented by Wirth.

I've probably been arguing against C as an application-level language for longer than most here, but I really don't like it when somebody says "It's in C, therefore it has no place in Pascal".

Everybody here knows that "step" or "by" are most unlikely to be implemented by the core developers and that they have their own reasons for that decision, but "it's C-like" shouldn't be one of them and "Wirth didn't like it" /can't/ be one of them since it is manifestly untrue.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: for loop with custom step width (esp. ≠ 1)
« Reply #87 on: January 15, 2022, 01:12:39 pm »
Hi!

* Pascal has not a step in the for loop. Discussion over.

* If you want to have a Wirth language with the step parameter in the for loop then use Modula:

Code: Pascal  [Select][+][-]
  1. FOR Index := 5 TO 25 BY 4 DO WriteInt(Index,5);
  2.  

Winni

PascalDragon

  • Hero Member
  • *****
  • Posts: 5462
  • Compiler Developer
Re: for loop with custom step width (esp. ≠ 1)
« Reply #88 on: January 15, 2022, 06:50:46 pm »
Everybody here knows that "step" or "by" are most unlikely to be implemented by the core developers and that they have their own reasons for that decision, but "it's C-like" shouldn't be one of them and "Wirth didn't like it" /can't/ be one of them since it is manifestly untrue.

Repeating myself here:

For those interested: there is an old patch (even older than this discussion ;) ) that adds support for a by-clause to the for-loop. The patch won't apply on main, but it should be possible to rebase this...

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1106
Re: for loop with custom step width (esp. ≠ 1)
« Reply #89 on: January 15, 2022, 08:16:58 pm »
Side note: My hack published in UNDU becomes 25 years old this year, 8-) and I still feel that something like step should be supported, Note the hack still works.
(And is still unsafe.... ;D )
Pity UNDU is not on the wayback machine. It was great while it lasted.
What is or was that UNDU you are talking about? URL?

 

TinyPortal © 2005-2018