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

Discussion in 'C++' started by Jerry Coffin, Jul 10, 2008.

  1. Jerry Coffin

    Jerry Coffin Guest

    In article <f1468$48722d2a$>,
    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.
     
    Jerry Coffin, Jul 10, 2008
    #1
    1. Advertising

  2. Jerry Coffin

    James Kanze Guest

    On Jul 10, 3:55 pm, Jerry Coffin <> wrote:
    > In article <f1468$48722d2a$>,
    > 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, for example (so the previous suggestions fall into the
    first pattern).

    --
    James Kanze (GABI Software) email:
    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
     
    James Kanze, Jul 11, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Woodmon
    Replies:
    16
    Views:
    918
  2. bruce varley
    Replies:
    2
    Views:
    405
    Mike Wahler
    Nov 25, 2003
  3. Rudi
    Replies:
    5
    Views:
    5,117
  4. Nene
    Replies:
    6
    Views:
    354
    John W. Krahn
    Dec 13, 2008
  5. Isaac Won
    Replies:
    9
    Views:
    397
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page