Re: For loops
- Posted by Pete Lomax <petelomax at b?ueyonde?.co.uk> Nov 19, 2007
- 594 views
CChris wrote: > Er, wait a minute: > }}} <eucode> > for i=1 to 10 do end for > for j=1 to 3 do > for i=1 to 5 do end for > end for > ?i > </eucode> {{{ > Should it print 6 or 11? 6 > Using 2/ allows variables with something else than a predefined type This was the biggest flaw in my reply to Derek. Presumably:
type odd(integer x) return remainder(x,2) end type odd o for_existing o=1 to 10 do end for_existing
would die with a type check on the end for_existing line. I cannot accept this has any worthwhile value, not least because it encourages writing loops many times slower [>=8] than normal for loops, but that if the for construct is wrong, you will know soon enough - you don't need type checking on it as well, and of course any type-check will probably pass with flying colours even though the for construct is wrong. You cannot even do apparently sensible things such as type checking the limit, eg:
for_existing o=1 to 10 by 2 do
is valid despite odd(10) being false. I could even make a similar argument for the init value, in cases where the limit/step is such that the loop will iterate zero times. I still say that adding a new for_existing construct now seems pointless. > > not particularly logical that i cannot be assigned to > > after the end for, but it is a compromise I can easily accept. > > can't write to in natural scope, but can once out of that scope. I don't > find this very logical. Touche. I don't want to make an issue out of this. Actually it does make some logical sense to say that variables introduced as for loop controls can only ever be modified by for loop statements, both before and now after the corresponding end for(s). Regards, Pete