# Thread: Variable Number of Nested For Loops

1. Member
Join Date
May 2010
Posts
45
Rep Power
0

## Variable Number of Nested For Loops

I want to have some code that uses a different number of nested for loops, depending on input from the user. Is there an easy way to do this?

I was thinking an if statement before each for loop, but if numTimes, in the following code, is less than 3 then the println is unreachable. I haven't had any success trying anything else either.

Java Code:
```if (numTimes > 0) for (int i = 0; i < 3; i++) {
if (numTimes > 1) for (int j = 0; j < 3; j++) {
if (numTimes > 2) for (int k = 0; k < 3; k++) {
System.out.println(i+j+k);
}
}
}```

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15

## Re: Variable Number of Nested For Loops

How big will numTimes get?

For moderate values you are just printing the sum of the (decimal) digits of all the numbers from 0 to 3^numTimes in order which you can do in a single for loop.

For larger values (ie if 3^numTimes is bigger than a long) you could write a recursive method.

3. ## Re: Variable Number of Nested For Loops

Originally Posted by pbrockway2
For larger values (ie if 3^numTimes is bigger than a long) you could write a recursive method.
Yeah, much better much safer.

4. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15

## Re: Variable Number of Nested For Loops

It strikes me that a long is ... long. If that value is exceeded who is going to take the time to actually read all those System.out.println()s? If the OP is actually doing something with them (like add them up or some such project Eulerish thing) then a little thought might save a lot of calculation.

Mostly posting because I noticed a typo in my previous: I meant that what is printed is the sum of the ternary digits of 1 -> 3^numTimes.

5. Member
Join Date
May 2010
Posts
45
Rep Power
0

## Re: Variable Number of Nested For Loops

The code I posted wasn't what I'm trying to do, I was just posting a simple example to see if the for loop thing could be done.

What I'm trying to do is have all permutations listed. For example, if I had 3 nested for loops from 0 to 3 I would get the result:
000, 001, 002, 010, 011, 012, 020, 021, etc

But I would like to be able to input an int x instead of the static 3, and get the permutations for x characters. Like having x number of nested for loops.

Can I do this using a recursive method? I'm trying but it seems like it would be pretty challenging.

6. ## Re: Variable Number of Nested For Loops

Originally Posted by atomant
The code I posted wasn't what I'm trying to do, I was just posting a simple example to see if the for loop thing could be done.

What I'm trying to do is have all permutations listed. For example, if I had 3 nested for loops from 0 to 3 I would get the result:
000, 001, 002, 010, 011, 012, 020, 021, etc
Those are combinations, not permutations; combinations can be enumerated by simpe counting (here in base 3). You don't need recursion for that and you don't need recursion for permutations either although that's the solution shown in text books.

kind regards,

Jos

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•