Results 1 to 4 of 4
  1. #1
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default Nested for loop explanation

    Can anyone offer an explanation this multiplication table. I tried to work it out with pen and paper and can't even do that. If ran through first 2 runs,
    1: it would evaluate to a[0][0] = 1*1;
    2: it would evaluate to a[1][1] = 2*2;

    shouldn't it instead go through each row populate it and so on?

    Java Code:
    int rows = 10; 
    int cols = 10; 
    int[][] arr = new int[rows][cols];
    
    for (int i = 0; i < rows; i++){
    	for (int j = 0; j < cols; j++){
    		arr[i][j] = (i+1)*(j+1);
    		System.out.print(arr[i][j] + " ");
    	}
    	System.out.println();
    }

  2. #2
    Tha Boss is offline Member
    Join Date
    Jun 2012
    Location
    ON, Canada.
    Posts
    25
    Rep Power
    0

    Default Re: Nested for loop explanation

    A for loop basically runs a block of code as long as it's counter "i" and "j" in your case is true to a defined range.
    So your outer for loop will run the block of code within it until the "i" is out of it's defined range.. (which happens to be another for loop). Therefore this "nested" for loop will run it's block of code until the "j" is out of it's defined range. In your case, the result is.. "j" will count from 0 to 10 every time you add 1 to "i" which counts to 10 also; when the outer loop is broken (due to "i" being more than or = 10) neither loop will run.
    - CHEERS!

    "If you fall... don't give up... get up and try harder."
    - Daruma Daishi.
    Last edited by Tha Boss; 06-21-2012 at 04:22 AM.

    E.Hughes
    "I'm sometimes hard to please... because I'm only satisfied with the very best." - Fernand Point.

  3. #3
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default Re: Nested for loop explanation

    Thank you for a good explanation, i believe i fully understand it now. so as it stands the inner for loop actually builds the columns 1-10, then the outer loop increment to row 2, which then builds the second column.

  4. #4
    Tha Boss is offline Member
    Join Date
    Jun 2012
    Location
    ON, Canada.
    Posts
    25
    Rep Power
    0

    Default Re: Nested for loop explanation

    You got it.. so by switching the for loops, you can choose to populate all the columns first before moving on to the next row, or whatever you want..
    Last edited by Tha Boss; 06-21-2012 at 06:27 AM.

    E.Hughes
    "I'm sometimes hard to please... because I'm only satisfied with the very best." - Fernand Point.

Similar Threads

  1. Explanation of nested methods as argument (Android)
    By StateMachine in forum New To Java
    Replies: 12
    Last Post: 01-17-2012, 01:49 AM
  2. Nested Loop
    By sehudson in forum New To Java
    Replies: 2
    Last Post: 03-11-2011, 04:39 AM
  3. Explanation of Nested Loop (very strange)
    By Jonotron in forum New To Java
    Replies: 5
    Last Post: 01-09-2011, 03:54 AM
  4. explanation of this loop?
    By glopez09 in forum New To Java
    Replies: 4
    Last Post: 11-15-2009, 03:36 AM
  5. Nested For Loop
    By yuchuang in forum New To Java
    Replies: 1
    Last Post: 07-08-2007, 02:11 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
  •