Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: Variable Number of Nested For Loops

  1. #1
    atomant is offline Member
    Join Date
    May 2010
    Posts
    45
    Rep Power
    0

    Default 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. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.
    Eranga likes this.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: Variable Number of Nested For Loops

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

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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. #5
    atomant is offline Member
    Join Date
    May 2010
    Posts
    45
    Rep Power
    0

    Default 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. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,648
    Blog Entries
    7
    Rep Power
    21

    Default Re: Variable Number of Nested For Loops

    Quote Originally Posted by atomant View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 20
    Last Post: 03-22-2011, 04:02 AM
  2. Nested loops
    By Aestuv in forum New To Java
    Replies: 3
    Last Post: 02-10-2011, 11:40 PM
  3. Replies: 5
    Last Post: 04-01-2010, 03:57 AM
  4. nested for loops
    By Implode in forum New To Java
    Replies: 4
    Last Post: 09-01-2009, 08:47 AM
  5. Nested loops?
    By gabriel in forum New To Java
    Replies: 4
    Last Post: 08-06-2007, 04:51 PM

Posting Permissions

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