# n Nested Loops

• 10-15-2012, 07:46 AM
joyful
n Nested Loops
Hello,

Can I use recursion to create n nested loops? Obviously, the following is very poor code:
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?

--
• 10-15-2012, 07:52 PM
arieltal
Re: n Nested Loops
if the variable count is the same to all..
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.
• 10-16-2012, 04:41 AM
joyful
Re: n Nested Loops
Quote:

Originally Posted by arieltal
if the variable count is the same to all..
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:
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...
• 10-16-2012, 04:57 AM
joshdgreen
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.
• 10-16-2012, 05:04 AM
joyful
Re: n Nested Loops
Quote:

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!
• 10-16-2012, 05:13 AM
joshdgreen
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!

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);         } }```
• 10-16-2012, 05:14 AM
penguinCoder
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:
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...