Velocity Reviews > A for loop iterating over the complete range of a variable

# A for loop iterating over the complete range of a variable

Joona I Palaste
Guest
Posts: n/a

 08-29-2004
Reading Skybuck Flying's obvious troll message, I thought of how to
properly do a for loop that would iterate over the complete range of
an unsigned variable. As you all know, Skybuck's method won't work.
There are a number of other ways. You could use a wider type to do the
counting, but that would be cheating. You could also handle either the
first or last iteration as a special case outside the loop, but then it
would be more than a loop. You could make a very big array of integer
flags to tell whether you've already visited a value, but that would
consume too much memory.
So I settled down to this version. Assume unsigned short is 16 bits
wide.

int stop = 0;
unsigned short i;
for (i=0; !stop; stop = ++i==0) {
/* do something */
}

Any more elegant solutions out there?

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Ice cream sales somehow cause drownings: both happen in summer."
- Antti Voipio & Arto Wikla

Joona I Palaste
Guest
Posts: n/a

 08-29-2004
Joona I Palaste <(E-Mail Removed)> scribbled the following:
> So I settled down to this version. Assume unsigned short is 16 bits
> wide.

Actually you don't need to assume that...

> int stop = 0;
> unsigned short i;
> for (i=0; !stop; stop = ++i==0) {
> /* do something */
> }

> Any more elegant solutions out there?

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"I wish someone we knew would die so we could leave them flowers."
- A 6-year-old girl, upon seeing flowers in a cemetery

Arthur J. O'Dwyer
Guest
Posts: n/a

 08-29-2004

On Sun, 29 Aug 2004, Joona I Palaste wrote:
>
> Reading Skybuck Flying's obvious troll message, I thought of how to
> properly do a for loop that would iterate over the complete range of
> an unsigned variable. As you all know, Skybuck's method won't work.
> There are a number of other ways. You could use a wider type to do the
> counting, but that would be cheating. You could also handle either the
> first or last iteration as a special case outside the loop, but then it
> would be more than a loop. You could make a very big array of integer
> flags to tell whether you've already visited a value, but that would
> consume too much memory.
> So I settled down to this version. Assume unsigned short is 16 bits
> wide.
>
> int stop = 0;
> unsigned short i;
> for (i=0; !stop; stop = ++i==0) {
> /* do something */
> }
>
> Any more elegant solutions out there?

I'm sure you've seen the solution that gets posted every time someone
claims it's hard to do:

unsigned short i = 0;
do {
/* do something */
} while (++i != 0);

It's not a 'for' loop, but it works and is easy to remember and write.
I guess you could use a 'for' loop similar to yours, but it would still
be slower and use a temporary variable: [UNTESTED CODE]

unsigned short i;
int tmp;
for (tmp=0, i=0; i || !tmp; ++i, tmp=1) {
/* do something */
}

-Arthur

Richard Tobin
Guest
Posts: n/a

 08-29-2004
In article <cgt24f\$9um\$(E-Mail Removed)>,
Joona I Palaste <(E-Mail Removed)> wrote:

>int stop = 0;
>unsigned short i;
>for (i=0; !stop; stop = ++i==0) {
> /* do something */
>}

If you use a for loop, you are bound to need some extra variable, since
the test at the start has to succeed 65536 times and fail once, so you
need 65537 states.

You can avoid the use of an extra variable by using a do ... while
loop, which tests at the end and therefore needs to succeed only 65535
times:

i = 0;
do {
/* do something */
i++;
} while(i != 0);

-- Richard

Ed Morton
Guest
Posts: n/a

 08-29-2004

Joona I Palaste wrote:
> Reading Skybuck Flying's obvious troll message, I thought of how to
> properly do a for loop that would iterate over the complete range of
> an unsigned variable. As you all know, Skybuck's method won't work.
> There are a number of other ways. You could use a wider type to do the
> counting, but that would be cheating. You could also handle either the
> first or last iteration as a special case outside the loop, but then it
> would be more than a loop. You could make a very big array of integer
> flags to tell whether you've already visited a value, but that would
> consume too much memory.
> So I settled down to this version. Assume unsigned short is 16 bits
> wide.
>
> int stop = 0;
> unsigned short i;
> for (i=0; !stop; stop = ++i==0) {
> /* do something */
> }
>
> Any more elegant solutions out there?
>

unsigned short i;
for (i=0; ++i {
/* do something */
}

For the general case where i might be getting incremented by some number
other than 1 and so won't necessarily have the value zero when it loops
around (e.g. 13 in the following example), this might be better:

unsigned short i, j;
for (i=0, j=0; i >= j; j=i, i+=13) {
/* do something */
}

Regards,

Ed.

Christian Bau
Guest
Posts: n/a

 08-29-2004
In article <cgt24f\$9um\$(E-Mail Removed)>,
Joona I Palaste <(E-Mail Removed)> wrote:

> Reading Skybuck Flying's obvious troll message, I thought of how to
> properly do a for loop that would iterate over the complete range of
> an unsigned variable. As you all know, Skybuck's method won't work.
> There are a number of other ways. You could use a wider type to do the
> counting, but that would be cheating. You could also handle either the
> first or last iteration as a special case outside the loop, but then it
> would be more than a loop. You could make a very big array of integer
> flags to tell whether you've already visited a value, but that would
> consume too much memory.
> So I settled down to this version. Assume unsigned short is 16 bits
> wide.
>
> int stop = 0;
> unsigned short i;
> for (i=0; !stop; stop = ++i==0) {
> /* do something */
> }
>
> Any more elegant solutions out there?

i = 0;
do {
<statements>
++i;
} while (i != 0);

Doing the same for signed int in a portable way seems to be difficult.

CBFalconer
Guest
Posts: n/a

 08-29-2004
Joona I Palaste wrote:
>
> Reading Skybuck Flying's obvious troll message, I thought of how to
> properly do a for loop that would iterate over the complete range of
> an unsigned variable. As you all know, Skybuck's method won't work.
> There are a number of other ways. You could use a wider type to do the
> counting, but that would be cheating. You could also handle either the
> first or last iteration as a special case outside the loop, but then it
> would be more than a loop. You could make a very big array of integer
> flags to tell whether you've already visited a value, but that would
> consume too much memory.
> So I settled down to this version. Assume unsigned short is 16 bits
> wide.
>
> int stop = 0;
> unsigned short i;
> for (i=0; !stop; stop = ++i==0) {
> /* do something */
> }
>
> Any more elegant solutions out there?

You need a loop that postpones the test until after the first
iteration. For example:

unsigned int i;

i = -1;
do {
i++;
/* stuff */
} while (i < UINT_MAX);

or

i = 0;
do {
/* stuff */
} while (0U != ++i);

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Keith Thompson
Guest
Posts: n/a

 08-29-2004
Christian Bau <(E-Mail Removed)> writes:
[...]
> i = 0;
> do {
> <statements>
> ++i;
> } while (i != 0);
>
> Doing the same for signed int in a portable way seems to be difficult.

int i = INT_MIN;
while (1) {
<statements>
if (i == INT_MAX) break;
i ++;
}

(Don't try this at home; depending on how fast your machine is,
iterating over a 32-bit int type could take several minutes and annoy
other users.)

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Ben Pfaff
Guest
Posts: n/a

 08-29-2004
Keith Thompson <(E-Mail Removed)> writes:

> (Don't try this at home; depending on how fast your machine is,
> iterating over a 32-bit int type could take several minutes and annoy
> other users.)

Most home machines are single-user
--
"Some people *are* arrogant, and others read the FAQ."
--Chris Dollin

Tim Rentsch
Guest
Posts: n/a

 08-29-2004
Joona I Palaste <(E-Mail Removed)> writes:

> Reading Skybuck Flying's obvious troll message, I thought of how to
> properly do a for loop that would iterate over the complete range of
> an unsigned variable.
>
> [snip]
>
> Any more elegant solutions out there?

The code

if( i = LOWER_BOUND, i <= UPPER_BOUND ) do {

/* stuff to do */

} while( i++ != UPPER_BOUND );

does the loop body once for each value of 'i' in the closed
interval [ LOWER_BOUND .. UPPER_BOUND ] whatever the bounds are (*),
and doesn't rely on any wraparound tricks. Any decent compiler
would optimize away the initial test if LOWER_BOUND and UPPER_BOUND
were known at compile time.

(*) As long as they are inside the range of the type of 'i'.
Being outside the range should also should be detected by
any reasonably decent compiler.