unsigned int in for loops

Ellery Newcomer ellery-newcomer at utulsa.edu
Fri Feb 4 11:36:57 PST 2011


I think this was the impetus for foreach_reverse, or at least it is one 
place where it is pretty handy. Don't remember what all there is in D1, 
but in D2 you could do something like

foreach_reverse(i; 0u .. 10u){
// iterates over 9,8,7 .. 1,0
}

for more complex iterations, I suppose you're stuck with

foreach(i; retro(somerange)){
}

or tired old for loops

On 02/04/2011 10:14 AM, eles wrote:
> hi everybody,
>
> recently i was hit by an old-known programming error, using unsigned int in for
> loops could get into infinite traps:
>
> http://stackoverflow.com/questions/665745/whats-the-best-way-to-do-a-reverse-
> for-loop-with-an-unsigned-index
>
> so, i D (1 or 2) is counter-attacking in some way (for example, disabling
> implicit casting of (-1) to UINT_MAX).
>
> cordially,
>
> eles.
>
> PS i really like being able to use an unsigned int for those loops, both for
> logical and for programming (large values of the loop index), while still
> avoiding such traps. thanks.


More information about the Digitalmars-d-learn mailing list