Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By joshdgreen

Thread: n Nested Loops

  1. #1
    joyful is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

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

    Thanks a lot in advance!
    --

  2. #2
    arieltal is offline Member
    Join Date
    Sep 2012
    Posts
    34
    Rep Power
    0

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

  3. #3
    joyful is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Default Re: n Nested Loops

    Quote Originally Posted by arieltal View Post
    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.
    Thanks a lot for your reply arieltal!

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

  4. #4
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default 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.
    Sincerely, Joshua Green
    Please REP if I help :)

  5. #5
    joyful is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Default Re: n Nested Loops

    Quote Originally Posted by joshdgreen View Post
    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!

  6. #6
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default 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);
    	}
    }
    penguinCoder likes this.
    Sincerely, Joshua Green
    Please REP if I help :)

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

    Default 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

Similar Threads

  1. Nested Loops Help!
    By spendogw in forum New To Java
    Replies: 2
    Last Post: 03-16-2012, 04:20 AM
  2. Using Nested Loops
    By son012189 in forum New To Java
    Replies: 6
    Last Post: 10-02-2011, 05:30 AM
  3. Replies: 20
    Last Post: 03-22-2011, 04:02 AM
  4. Nested loops
    By Aestuv in forum New To Java
    Replies: 3
    Last Post: 02-10-2011, 11:40 PM
  5. Nested Loops
    By joemama in forum New To Java
    Replies: 1
    Last Post: 01-01-2011, 09:17 PM

Tags for this Thread

Posting Permissions

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