Scripting: for loops

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
admin
Site Admin
Posts: 66308
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Scripting: for loops

Post by admin »

Let me make a confession: I hate the common syntax for for loops!

How many times was I forced to write this ugly construct...

Code: Select all

for ($i = 1; $i <= 10; $i++) {
    echo $i;
}
...when all I needed to do was something like this:

Code: Select all

for (10) {
    echo $i;
}
So, I propose to use a simplified for syntax for XY scripting:

Code: Select all

for ([number of iterations]) {
    statement(s);
}
All more complex cases can easily be handled using the while loop.

Pagat
Posts: 308
Joined: 09 Oct 2007 21:23
Location: Austria

Re: Scripting: for loops

Post by Pagat »

i also don't like the "common" syntax for for loops. But i guess that's because i started with the Pascal/Delphi way of doing things where it is:

Code: Select all

for i := 1 to 10 do ... 
.
in your example:

Code: Select all

for (10) {
    echo $i;
}
would $i be a "standard" variable in such for loops? Because we would definitely need a variable of some kind in the loop. And if it's hardcoded it could conflict with a variable outside of the loop. So you should at least give the option to let the user define an own loop-variable name.
OS: Windows 11, 64-bit, Version 25H2, Build 26200.8655 (10.0), AMD64
Monitor: 1/2 (Primary) Primary Screen DPI: 96 (100%)

admin
Site Admin
Posts: 66308
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Scripting: for loops

Post by admin »

Pagat wrote:i also don't like the "common" syntax for for loops. But i guess that's because i started with the Pascal/Delphi way of doing things where it is:

Code: Select all

for i := 1 to 10 do ... 
.
in your example:

Code: Select all

for (10) {
    echo $i;
}
would $i be a "standard" variable in such for loops? Because we would definitely need a variable of some kind in the loop. And if it's hardcoded it could conflict with a variable outside of the loop. So you should at least give the option to let the user define an own loop-variable name.
Oh yes, I had forgotten about that. Yep, a variable must be definable.

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: Scripting: for loops

Post by TheQwerty »

Eh... I don't know why you hate the normal syntax, but I think there's a reason so many languages use it.

Only being able to define the number of iterations takes a lot of the functionality of a for loop away.
You can't define a different starting number.
You can't change the direction.
You can't change the increment/decrement amount so it isn't too useful to do something to every odd/even/third item.
It's a much cleaner interface for anything that requires some type of index, even if you're not breaking based on that index's value.

I think if you implemented it with only an iterations value, I'd still be cursing you every time I used this. :P

admin
Site Admin
Posts: 66308
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Scripting: for loops

Post by admin »

TheQwerty wrote:Eh... I don't know why you hate the normal syntax, but I think there's a reason so many languages use it.

Only being able to define the number of iterations takes a lot of the functionality of a for loop away.
You can't define a different starting number.
You can't change the direction.
You can't change the increment/decrement amount so it isn't too useful to do something to every odd/even/third item.
It's a much cleaner interface for anything that requires some type of index, even if you're not breaking based on that index's value.

I think if you implemented it with only an iterations value, I'd still be cursing you every time I used this. :P
:mrgreen:

Anyway, the normal For syntax looks to me like an obsessive one-liner. You can do all you mentioned very easily using a While loop. Actually, having While, I don't see a good reason adding a For loop at all (apart from that users might expect to have it).

fishgod
Posts: 231
Joined: 03 Feb 2008 00:40
Location: Sankt Augustin (near Bonn), Germany

Re: Scripting: for loops

Post by fishgod »

admin wrote:Anyway, the normal For syntax looks to me like an obsessive one-liner. You can do all you mentioned very easily using a While loop. Actually, having While, I don't see a good reason adding a For loop at all (apart from that users might expect to have it).
Why don't implement both versions, I think the shorten one can be very usefull, but the normal version has advantages too.
Operating System: Win10 x64 / Win11 x64 / almost allways newest XY-beta
totally XYscripting-addicted

Post Reply