# speeding up nested for loop

• 03-16-2002
Unregistered
speeding up nested for loop
ok, I have 3 nested for loops and I want to start at a particular point in the loop. what recommendations can you give me?

here is a sample code:
Code:

```int main() {  int a,b,c,d;  for(a=0;a<10;a++)   for(b=0;b<10;b++)   for(c=0;c<10;c++)     for(d=0;d<10;d++)     {       if((a==0)&&(b==0)&&(c==0)&&(d==0))       {       a=1,b=2,c=3,d=4;       }       cout<a<<b<<c<<d;     } }```
in my sample code, I have 3 nested for loops and it prints from "1234" to "9999" because I added an if statement to change some values. however, the if statement would slow down the nested for loops because it would be checked every time during a loop. any suggestion on another solution?

can the use of static work for this situation? if so, how?
• 03-16-2002
blight2c
what if you nested the if in another if that skiped that later after d clicked for however many number of times?
• 03-16-2002
Betazep
for (a=1;a <=9; ++a)
for (b=2;b <=9;++b)

etc...
• 03-16-2002
Betazep
>>>what if you nested the if in another if that skiped that later after d clicked for however many number of times?

That would still require one if statement to be evaluated.
• 03-16-2002
Unregistered
betazep, I think your solution is wrong.

the "for(b=2;b<=9;... " will make b start from 2 and ignore 1.
• 03-16-2002
Imperito
Grr I hate multiple unreggeds in the same thread, makes it hard to tell if they are or are not the same entity.

I THINK that the first unregged wanted a program in which b started at 2, c at 3, and d at 4.

BUT I think blue's solution would mess up at 2000. It would skip straight to 2234.

A more effective method might be to place the choice in the declarator for the loops as:

Code:

```            int main() {       int a,b,c,d;       for(a=1;a<10;++a) {         for(b=a==1?2:0;b<10;b++) {             for(c=a==1?3:0;c<10;c++) {               for(d=a==1?4:0;d<10;d++) {                   cout<<a<<b<<c<<d;                  }             }         }       }   }```
This makes the test occur only once every tenth, twice every hundredth, and thrice every thousandth iteration, rather than every single iteration.

Of course, this assumes you want every number from 1234 - 9999. Your original program has the same logic error/unusual usage as beta's solution. They both give 1234 - 1999, 2234 - 2999, 3234 - 3999, etc.

This is, of course, assuming that

Code:

```for (int n = 1234; n<10000; ++n) { cout << n; }```
would be too simple.
• 03-16-2002
Sorensen
If 1234->9999 is required something like this should work -

Code:

```#include <iostream> using namespace std; int main() {  int a,b,c,d;  for(a=1;a<10;a++)         for(a==1?b=2:b=0;b<10;b++)                 for(a==1&&b==2?c=3:c=0;c<10;c++)                         for(a==1&&b==2&&c==3?d=4:d=0;d<10;d++)                         {                                                cout<<a<<b<<c<<d<<endl;                         } }```
• 03-16-2002
Betazep
>>>betazep, I think your solution is wrong.

the "for(b=2;b<=9;... " will make b start from 2 and ignore 1.<<<

good catch.
• 03-16-2002
Unregistered
Sorensen, the code you gave would cost more time because the "?" will compare values for every pass in the nested loop.

the if statement from the first post is likely to finish the nested loop at a faster time. I also thought of something. can this possibly work?

Code:

``` int main() {  int a,b,c,d;  for(a=0, static a=1;  a<10;  a++)   for(b=0, static b=2;  b<10;  b++)   for(c=0, static c=3;  c<10;  c++)     for(d=0; static d=4; d<10;  d++)       cout<a<<b<<c<<d; }```
• 03-16-2002
Sorensen
>Sorensen, the code you gave would cost more time because the "?" will compare values for every pass in the nested loop.

No it won't. Do variables get initialised every pass of the for loop?
• 03-16-2002
Imperito
Ach soren beat me to it. But my post of the solutionis higher up and better documented so :p
• 03-16-2002
Imperito
Yeah unregged, like I said in MY post

Quote:

This makes the test occur only once every tenth, twice every hundredth, and thrice every thousandth iteration, rather than every single iteration.