Re: For loops

new topic     » goto parent     » topic index » view thread      » older message » newer message

Pete Lomax wrote:
> 
> 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
> 

Agreed.

> > Using 2/ allows variables with something else than a predefined type
> 
> This was the biggest flaw in my reply to Derek. Presumably:
> }}}
<eucode>
> type odd(integer x) return remainder(x,2) end type
> odd o
> for_existing o=1 to 10 do
> end for_existing
> </eucode>
{{{

> would die with a type check on the end for_existing line.
> 

Sure would.

> I cannot accept this has any worthwhile value, not least because it encourages
> writing loops many times slower [>=8] than normal for loops, 

Use without type_check if you don't want type checking to slow things down. Is
this approach wrong?

> 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:
> }}}
<eucode>
> for_existing o=1 to 10 by 2 do
> </eucode>
{{{

> is valid despite odd(10) being false. 

The limit is not necessarily reached by the index, whose the end value is the
smallest beyond the limit ("what "beyond" means depends on the sign of the
increment). so the checking would be pointless.

> 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.
> 

Well, I'm not a staunch supporter of this. It was just the most consistent way
imho to accommodate requests that were made. We can decide that the while loop is
good enough for these cases - though using the same construct for many different
purposes doesn't help writing clearer code.

> > > 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

CChris

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu