Loops

Discussion in 'C++' started by cplusplusquestion@gmail.com, Jun 23, 2008.

  1. Guest

    For the loops:

    for(int i=0; i<FIRST; i++){
    for(int j=0; j<SECOND; j++){
    //some code here
    .......
    }
    }

    Is it possible to change it as one loop?
    , Jun 23, 2008
    #1
    1. Advertising

  2. Eric Pruneau Guest

    <> a écrit dans le message de news:
    ...
    > For the loops:
    >
    > for(int i=0; i<FIRST; i++){
    > for(int j=0; j<SECOND; j++){
    > //some code here
    > .......
    > }
    > }
    >
    > Is it possible to change it as one loop?
    >


    you mean something like

    for(int i=0; i< FIRST*SECOND; ++i)
    {
    ...
    }


    yeah... it is possible

    -----------------

    Eric Pruneau
    Eric Pruneau, Jun 23, 2008
    #2
    1. Advertising

  3. ManicQin Guest

    On Jun 23, 9:18 am, "Eric Pruneau" <> wrote:
    > <> a écrit dans le message de news:
    > ...
    >
    > > For the loops:

    >
    > > for(int i=0; i<FIRST; i++){
    > >   for(int j=0; j<SECOND; j++){
    > >       //some code here
    > >       .......
    > >   }
    > > }

    >
    > > Is it possible to change it as one loop?

    >
    > you mean something like
    >
    > for(int i=0; i< FIRST*SECOND; ++i)
    > {
    >     ...
    >
    > }
    >
    > yeah...  it is possible
    >
    > -----------------
    >
    > Eric Pruneau


    Why would you like to write it any other way?
    The first way is more clear and easier to understand.
    I hope you understand that Eric's answer only works in 3D arrays and
    not with classes...

    You better leave it the first way, there is no "Better" way that will
    give you better results.
    ManicQin, Jun 23, 2008
    #3
  4. Kai-Uwe Bux Guest

    ManicQin wrote:

    > On Jun 23, 9:18 am, "Eric Pruneau" <> wrote:
    >> <> a écrit dans le message de news:
    >> ...
    >>
    >> > For the loops:

    >>
    >> > for(int i=0; i<FIRST; i++){
    >> > for(int j=0; j<SECOND; j++){
    >> > //some code here
    >> > .......
    >> > }
    >> > }

    >>
    >> > Is it possible to change it as one loop?

    >>
    >> you mean something like
    >>
    >> for(int i=0; i< FIRST*SECOND; ++i)
    >> {
    >> ...
    >>
    >> }
    >>
    >> yeah...  it is possible


    With some caveats: FIRST and SECOND might be macros (after all, they are all
    upper case). So, maybe (FIRST)*(SECOND) is slightly better. And even then,
    the product might overflow.

    >>
    >> -----------------
    >>
    >> Eric Pruneau

    >
    > Why would you like to write it any other way?
    > The first way is more clear and easier to understand.


    Right.

    > I hope you understand that Eric's answer only works in 3D arrays and
    > not with classes...


    Huh? I don't see any 3D-arrays around. And why would there be any problem
    with classes (e.g., inside the loop)? In short, I don't understand what you
    mean. Could you explain?

    [snip]


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Jun 23, 2008
    #4
  5. joseph cook Guest

    > Huh? I don't see any 3D-arrays around. And why would there be any problem
    > with classes (e.g., inside the loop)? In short, I don't understand what you
    > mean. Could you explain?


    I suppose he could have meant that FIRST is not of int type:
    i.e.
    class FIRST
    {
    public:
    operator int() { ... }
    .... operator++() {...}
    ....
    };

    if the operator* isn't defined, then the suggested change won't be
    equivalant.

    Of course, this is a ridiculous scenario.

    Joe Cook
    joseph cook, Jun 23, 2008
    #5
  6. ManicQin Guest

    On Jun 23, 9:14 am, wrote:
    > For the loops:
    >
    > for(int i=0; i<FIRST; i++){
    >    for(int j=0; j<SECOND; j++){
    >        //some code here
    >        .......
    >    }
    >
    > }
    >
    > Is it possible to change it as one loop?


    I meant,
    cplusplusquest's example doesnt say if he is using
    tArr[i,j] <- 3D array
    or
    cVector.MemberClassVector[j]
    in the first example you can use Eric's post and just

    for(int iMulj=0; iMulj< FIRST*SECOND; ++iMulj)
    tArr[iMulj]

    Because he didnt wrote nothing inside the brackets It could be even
    cout << "Hello world!!! " << i;
    ManicQin, Jun 23, 2008
    #6
    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. eismaus4

    to many FOR loops?

    eismaus4, Apr 27, 2004, in forum: VHDL
    Replies:
    1
    Views:
    673
  2. Jeremy Morton

    Perl loops should use break, not last

    Jeremy Morton, Jan 29, 2005, in forum: Perl
    Replies:
    1
    Views:
    5,120
    Big and Blue
    Jan 30, 2005
  3. Multiple For Loops?

    , Feb 22, 2006, in forum: VHDL
    Replies:
    1
    Views:
    460
    john Doef
    Feb 22, 2006
  4. =?Utf-8?B?Um9iIE8nQ29w?=

    RedirectFromLoginPage and loops

    =?Utf-8?B?Um9iIE8nQ29w?=, Oct 25, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    406
    =?Utf-8?B?Um9iIE8nQ29w?=
    Oct 25, 2004
  5. Me
    Replies:
    2
    Views:
    229
Loading...

Share This Page