Nested Loops?

Discussion in 'OT Technology' started by MaxMouse, Dec 16, 2007.

  1. MaxMouse

    MaxMouse New Member

    Joined:
    Jun 7, 2003
    Messages:
    197
    Likes Received:
    0
    Location:
    toronto
    Anyone know where I can find a good tutorial on nested loops and/or loops in general? Language is not too important, but Java or C/C++ would be preferred.
     
  2. ge0

    ge0 New Member

    Joined:
    Oct 31, 2005
    Messages:
    8,398
    Likes Received:
    0
    Location:
    JERSEY
    ugh..

    post your questions here and we cna answer them.. there isn't much to nested loops
     
  3. Dnepr

    Dnepr Guest

    ArrayList i = new ArrayList();
    ArrayList b = new ArrayList()
    // load some stuff into both array lists
    for(Object z : i) {
    for(Object x : b) {
    //Do stuff
    }
    }

    :hsugh: :dunno:

    I's java code btw. Nested for loops :hsugh:

    Ask questions, we'll answer em
     
  4. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    Nested loops are easy.

    Start Loop 1
    Start Loop 2
    Do something
    Repeat Loop 2
    Repeat Loop 1
     
  5. tyrionlannister

    tyrionlannister New Member

    Joined:
    Jun 13, 2006
    Messages:
    710
    Likes Received:
    0
    Location:
    New York
    A simple nested loop to create a multiplication table in Java.

    Code:
    for (int i = 1; i <= 5; i++) {
         for (int j = 1; j <= 5; j++) {
              System.out.println(i + " * " + j + " = " + i*j);
         }
    }
    This produces:

    Code:
    1 * 1 = 1
    1 * 2 = 2
    1 * 3 = 3
    1 * 4 = 4
    1 * 5 = 5
    2 * 1 = 2
    2 * 2 = 4
    2 * 3 = 6
    2 * 4 = 8
    2 * 5 = 10
    3 * 1 = 3
    3 * 2 = 6
    3 * 3 = 9
    3 * 4 = 12
    3 * 5 = 15
    4 * 1 = 4
    4 * 2 = 8
    4 * 3 = 12
    4 * 4 = 16
    4 * 5 = 20
    5 * 1 = 5
    5 * 2 = 10
    5 * 3 = 15
    5 * 4 = 20
    5 * 5 = 25
     
  6. tyrionlannister

    tyrionlannister New Member

    Joined:
    Jun 13, 2006
    Messages:
    710
    Likes Received:
    0
    Location:
    New York
    Oops, that's not a table, that's just multiplication.

    This is a multiplication table:

    Code:
    for (int i = 1; i <= 5; i++) {
         for (int j = 1; j <= 5; j++) {
              System.out.print( i*j + "\t");
         }
        System.out.println();
    }
    Which produces:
    Code:
    1    2    3    4    5    
    2    4    6    8    10    
    3    6    9    12    15    
    4    8    12    16    20    
    5    10    15    20    25
     
  7. ez4me2c3d

    ez4me2c3d Cold Member

    Joined:
    Sep 19, 2003
    Messages:
    1,836
    Likes Received:
    0
    Location:
    Minneapolis, MN
  8. MaxMouse

    MaxMouse New Member

    Joined:
    Jun 7, 2003
    Messages:
    197
    Likes Received:
    0
    Location:
    toronto
    I'm trying to print this:

    Code:
         1 
        21
       321
      4321 
     54321
    654321
    
    So far I have this:

    Code:
    		for(int i = 1; i <=6; i++)
    		{
    			int spaces = 6 - i;
    			for(int j = 0; j < spaces; j++)
    				System.out.print(" ");
    			
    			for(int k = 1; k <= i; k++)
    					System.out.print(k);
    			
    			System.out.println();
                         }
    
    Which prints this:
    Code:
         1
        12
       123
      1234
     12345
    123456
    
    I just need to know how to reverse each line.
     
    Last edited: Dec 17, 2007
  9. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    The loop counter doesn't always have to increase.
    You can have a decreasing counter like this:

    for (int i = 6; i >= 1; i--)
    {
    ...
    }
     
  10. noon

    noon get high and teach me how to listen

    Joined:
    May 4, 2002
    Messages:
    3,384
    Likes Received:
    0
    Location:
    Lawrence, KS
    here you go

    Code:
    		for(int i = 1; i <=6; i++)
    		{
    			int spaces = 6 - i;
    			for(int j = 0; j < spaces; j++)
    				System.out.print(" ");
    			
    			for(int k = i; k >=1 ; k--)
    					System.out.print(k);
    			
    			System.out.println();
                         }
    
     
  11. MaxMouse

    MaxMouse New Member

    Joined:
    Jun 7, 2003
    Messages:
    197
    Likes Received:
    0
    Location:
    toronto
    I got it by doing it this way.

    Code:
    for(int i = 1; i <=6; i++)
    {
     int spaces = 6 - i;
     for(int j = 0; j < spaces; j++)
       System.out.print(" ");
    
       for(int k = 6; k > spaces ; k--)
         System.out.print(k-spaces);
    			
        System.out.println();
    }
    
    Thanks for the help.
     

Share This Page