Page 1 of 1
Scripting: for loops
Posted: 27 Apr 2009 18:49
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:
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.
Re: Scripting: for loops
Posted: 27 Apr 2009 19:40
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:
.
in your example:
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.
Re: Scripting: for loops
Posted: 27 Apr 2009 20:16
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:
.
in your example:
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.
Re: Scripting: for loops
Posted: 28 Apr 2009 02:54
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.

Re: Scripting: for loops
Posted: 28 Apr 2009 07:25
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.

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).
Re: Scripting: for loops
Posted: 28 Apr 2009 08:56
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.