1. Member Join Date
Oct 2012
Posts
3
Rep Power
0

## n Nested Loops

Hello,

Can I use recursion to create n nested loops? Obviously, the following is very poor code:
Java Code:
```for(int i=0;i<count;i++){
for(int x=0;x<count;x++){
for(int y=0;y<count;y++){
for(int z=0;z<count;z++){
// etc...
}
}
}
}```
My issue is that the number of loops is variable. How can you do this with recursion?

--  Reply With Quote

2. Member Join Date
Sep 2012
Posts
34
Rep Power
0

## Re: n Nested Loops

if the variable count is the same to all..
Java Code:
```[CODE]public void doLoop(int n)
{
for (int i = 0;i<n;i++)
{
doTheLoop();
}
}
public void doTheLoop()
{
for (int i  = 0;i<count;i++)
{
// enter code here..
}
}```
[/code]
.. just from the top of my head, but this should work.  Reply With Quote

3. Member Join Date
Oct 2012
Posts
3
Rep Power
0

## Re: n Nested Loops Originally Posted by arieltal if the variable count is the same to all..
Java Code:
```[CODE]public void doLoop(int n)
{
for (int i = 0;i<n;i++)
{
doTheLoop();
}
}
public void doTheLoop()
{
for (int i  = 0;i<count;i++)
{
// enter code here..
}
}```
[/code]
.. just from the top of my head, but this should work.

The issue is that I need all of the variables inside the most nested loop; so, for example, I am doing this inside the loops:
Java Code:
```for(int i=0;i<count;i++){
for(int x=0;x<count;x++){
for(int y=0;y<count;y++){
for(int z=0;z<count;z++){
String returnStr = i + " and " + x + " and " + y + " and " + z; // So I need all of the loops' vars
return returnStr;
}
}
}
}```
Can I do this with the methods you provided? I need n nested loops, and I need all of the loops' incrementing variables...  Reply With Quote

4. ## Re: n Nested Loops

You would probably need to create a new recursion method and have a for loop which calls the same method that it is in until your count is equal to loop variable. Hope this helps get you started.  Reply With Quote

5. Member Join Date
Oct 2012
Posts
3
Rep Power
0

## Re: n Nested Loops Originally Posted by joshdgreen You would probably need to create a new recursion method and have a for loop which calls the same method that it is in until your count is equal to loop variable. Hope this helps get you started.
Hello joshdgreen, thanks for the response!

Could you show me how to do this (with code)? It does not have to even be fleshed-out (complete) code... Sorry, I have trouble with recursion.

Thanks a lot!  Reply With Quote

6. ## Re: n Nested Loops

I don't know if this will apply to your program at all, but it may get some creative juices flowing!

Java Code:
```public class test
{
public static void main(String[] args)
{
loop(10);
}

public static void loop(int count)
{
if(count != 0)
{
System.out.println(count);
count --;
loop(count);
}
else
System.out.print("Done, count = " + count);
}
}```  Reply With Quote

7. Member Join Date
Oct 2012
Location
Tempe, Arizona
Posts
77
Blog Entries
12
Rep Power
0

## Re: n Nested Loops

First, I must say that I am not an expert at recursion, so there may be a better way of doing this then how I provide. This will however accomplish what you are trying to do, hopefully:
Java Code:
```public class recursion {
//how many times you want each loop to loop through
static int count=5;
//how many times you want the recursion to happen
static final int nLoops = 100;

static int counter=0;
static String output = "";

public static void main(String[] args){

for(int ix=0; ix<count; ix++){
System.out.println(recur());
}
}

public static String recur(){

int ix = 0;
do{

ix++;
}while(ix<count);
output += ix + " and ";

counter++;
if(counter == nLoops){
output += ix;
return output;
}
else{
output += ix + " and ";
recur();
}

return output;
}
}```
I hope this is just for you to learn more about recursion, and not some type of homework assignment...

By the way, the only reason that I did not make a for loop inside of the method was because I wanted to declare ix outside of the loop so that I could add the final value of it to the string. If you declared it inside of the for loop, the visibility would have ended at the end of the for loop, and you would be required to add an if statement inside of the for loop to compare if it is on the final loop of not so that it could add the value to the string. I guess I should have done it this way since you specified a for loop, but meh...
Last edited by penguinCoder; 10-16-2012 at 04:19 AM. Reason: added last paragraph explanation  Reply With Quote

n nested loops, recursion 