Loops

C

cplusplusquestion

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?
 
E

Eric Pruneau

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
 
M

ManicQin

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
[/QUOTE]

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.
 
K

Kai-Uwe Bux

ManicQin said:
you mean something like

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

}

yeah...  it is possible
[/QUOTE]

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.
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
 
J

joseph cook

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
 
M

ManicQin

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;
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top