Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: irregular (non-consecutive) iteration (for loop)

Reply
Thread Tools

Re: irregular (non-consecutive) iteration (for loop)

 
 
Jerry Coffin
Guest
Posts: n/a
 
      07-10-2008
In article <f1468$48722d2a$(E-Mail Removed)>, http://www.velocityreviews.com/forums/(E-Mail Removed)
says...
> hi there,
>
> suppose I want to iterate for a specific variable e.g. i , but for non
> regular (or consecutive) values. For example i=0,1,2,4,5,7,8 etc
> how can I do that with a for loop?
>
> MY solution which is not that elegant involves if statements (or switch
> statements) in the body of the loop: e.g.
>
>
> for (int i=0; i<=8; i++)


I'd probably do something like this:

int f(int i) {
if (i==3 || i==6)
return i+2;
return i+1;
}

for (int i=0; i<max; i=f(i))
// whatever

of course, I've only defined f to do exactly what you showed, skipping 3
and 6, but nothing else. If you really meant (for example) to skip all
multiples of three, that's somewhat simpler. In any case, it's purely a
matter of figuring out what you want and writing 'f' to suit.

--
Later,
Jerry.

The universe is a figment of its own imagination.
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      07-11-2008
On Jul 10, 3:55 pm, Jerry Coffin <(E-Mail Removed)> wrote:
> In article <f1468$48722d2a$(E-Mail Removed)>,
> (E-Mail Removed) says...


> > suppose I want to iterate for a specific variable e.g. i ,
> > but for non regular (or consecutive) values. For example
> > i=0,1,2,4,5,7,8 etc how can I do that with a for loop?


> > MY solution which is not that elegant involves if statements
> > (or switch statements) in the body of the loop: e.g.


> > for (int i=0; i<=8; i++)


> I'd probably do something like this:


> int f(int i) {
> if (i==3 || i==6)
> return i+2;
> return i+1;
> }


> for (int i=0; i<max; i=f(i))
> // whatever


Which could easily be written without the function:

for ( int i = 0 ; i < max ; i += (i == 3 || i == 6 ? 2 : 1) )

(Whether that's an improvement is another question. Written on
a single like, like above, it's actually rather intimidating,
but correctly formatted:

for ( int i = 0 ;
i < max ;
i += (i == 3 || i == 6
? 2
: 1) )

it's not that bad.)

> of course, I've only defined f to do exactly what you showed,
> skipping 3 and 6, but nothing else. If you really meant (for
> example) to skip all multiples of three, that's somewhat
> simpler. In any case, it's purely a matter of figuring out
> what you want and writing 'f' to suit.


More generally, in C++, he as two choices:

for ( int i = 0 ; i < max ; ++ i ) {
// use f(i) throughout the loop...
}

or

for ( int i = 0 ; i < max ; i = f(i) ) {
// use i throughout the loop...
}

Most other languages (or at least older languages) only offer
the first.

And of course, in the above, f(i) should be understood as any
arbitrary expression taking an i, and returning the same type.
Array[i], for example (so the previous suggestions fall into the
first pattern).

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Struts - Problem with nested iteration or double iteration Rudi Java 5 10-01-2008 03:30 AM
IE 7 has irregular white space at the tops of certain pages? elzool HTML 1 11-30-2006 08:13 AM
J2ME: Point inside zone (irregular polygon) Simon Brooke Java 2 11-16-2006 03:14 PM
Text and images in irregular shaped column(s) Woodmon HTML 16 10-23-2005 08:52 PM
Irregular behaviour: C++ standard lib and file stream. bruce varley C++ 2 11-25-2003 05:27 PM



Advertisments