Results 1 to 4 of 4
Thread: Nested for loop explanation
- 06-21-2012, 02:42 AM #1
Senior Member
- Join Date
- Oct 2010
- Location
- Newark,nj
- Posts
- 111
- Rep Power
- 0
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(); }
- 06-21-2012, 03:19 AM #2
Member
- Join Date
- Jun 2012
- Location
- ON, Canada.
- Posts
- 25
- Rep Power
- 0
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 03:22 AM.
E.Hughes☆™"I'm sometimes hard to please... because I'm only satisfied with the very best." - Fernand Point.
- 06-21-2012, 03:49 AM #3
Senior Member
- Join Date
- Oct 2010
- Location
- Newark,nj
- Posts
- 111
- Rep Power
- 0
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.
- 06-21-2012, 05:17 AM #4
Member
- Join Date
- Jun 2012
- Location
- ON, Canada.
- Posts
- 25
- Rep Power
- 0
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 05:27 AM.
E.Hughes☆™"I'm sometimes hard to please... because I'm only satisfied with the very best." - Fernand Point.
Similar Threads
-
Explanation of nested methods as argument (Android)
By StateMachine in forum New To JavaReplies: 12Last Post: 01-17-2012, 12:49 AM -
Nested Loop
By sehudson in forum New To JavaReplies: 2Last Post: 03-11-2011, 03:39 AM -
Explanation of Nested Loop (very strange)
By Jonotron in forum New To JavaReplies: 5Last Post: 01-09-2011, 02:54 AM -
explanation of this loop?
By glopez09 in forum New To JavaReplies: 4Last Post: 11-15-2009, 02:36 AM -
Nested For Loop
By yuchuang in forum New To JavaReplies: 1Last Post: 07-08-2007, 01:11 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks