# Help with nested loops. So confusing.

• June 18th, 2013, 03:01 PM
abstractmind
Help with nested loops. So confusing.
So for some reason, the idea of nested for loops COMPLETELY blows my mind. Consider this following code I wrote(I wrote this through experimentation and trial and error, I have no idea how it works.):
Code :

```public class nestedloops {   /** * @param args */ public static void main(String[] args) { for (int r = 1; r <= 10; r++) { System.out.println(); for (int c = 1; c <= 10; c++) { int product = r * c; if (product < 10) { System.out.print(" " + product + " "); } else { System.out.print(product + " "); }   } } } }```
Now I was wondering if someone could thoroughly explain to me how this works, what is does is print a multiplication table for the numbers 1 - 10, on a grid. I don't understand how it prints each line 1 - 9, and then the way it prints all the way down 10 times confuses my mind. I know this is a really big newbie question, but arrays have never been understood in my mind. Whoever can give me a nice explanation to this probably very simple problem, HUGE kudos to you :)
• June 18th, 2013, 03:42 PM
angstrem
Re: Help with nested loops. So confusing.
1)
Code Java:

``` int product = r * c; if (product < 10) { System.out.print(" " + product + " "); } else { System.out.print(product + " "); }```
We have 2 numbers, r and c. We have no info about their changes in (1) (this code snippet), that means, they are constant here. We find their product. We print this product so, that it always take 2 symbols and there is a space after the last symbol.
An arbitrary example is, say "24 " or " 4 ".

2)
Code Java:

```for (int c = 1; c <= 10; c++) { System.out.println(); // (1) snippet goes here }```
Here we have c changing from 1 to 10. Then we have println() - that means, everything after this statement will be printed on the next row. And the (1) code executes 10 times for each c from 1 to 10. (1) simply prints the product of c and r. So far, we have no info about r, that means, that r is constant (so far). That means, that this code snippet (2) will print 1*r, 2*r, ..., 10*r in a single line (see (1)) for a given r.

Code Java:

```for (int r = 1; r <= 10; r++) { // (2) snippet goes here }```
r changes from 1 to 10. For each r (2) snippet is executed. What (2) does? It prints new line and prints the products of all the numbers from 1 to 10 and given number r.

Also, following code illustrates my explanations in a clearer way:
Code Java:

``` public static void main(String[] args) { for (int r = 1; r <= 10; r++) { System.out.println(); productOfAllNumbersBefore10AndR(r); } }   public static void productOfAllNumbersBefore10AndR(int r) { for (int c = 1; c <= 10; c++) { printProductOfTwoNumbersInANiceWay(r, c); } }   public static void printProductOfTwoNumbersInANiceWay(int r, int c) { int product = r * c; if (product < 10) { System.out.print(" " + product + " "); } else { System.out.print(product + " "); } }```
- For each number from 1 to 10 (r), print productOfAllNumbersBefore10AndR
- How to print productOfAllNumbersBefore10AndR ?
- To do this, from the next row (println()), for each number from 1 to 10 (c) printProductOfTwoNumbersInANiceWay, and that two numbers are r and c
- How do I do this?
- First you find the product, then you print it - but with print(), not println(): you don't want a trailing carriage return.
• June 18th, 2013, 04:02 PM
abstractmind
Re: Help with nested loops. So confusing.
Quote:

Originally Posted by angstrem
1)
Code Java:

``` int product = r * c; if (product < 10) { System.out.print(" " + product + " "); } else { System.out.print(product + " "); }```
We have 2 numbers, r and c. We have no info about their changes in (1) (this code snippet), that means, they are constant here. We find their product. We print this product so, that it always take 2 symbols and there is a space after the last symbol.
An arbitrary example is, say "24 " or " 4 ".

2)
Code Java:

```for (int c = 1; c <= 10; c++) { System.out.println(); // (1) snippet goes here }```
Here we have c changing from 1 to 10. Then we have println() - that means, everything after this statement will be printed on the next row. And the (1) code executes 10 times for each c from 1 to 10. (1) simply prints the product of c and r. So far, we have no info about r, that means, that r is constant (so far). That means, that this code snippet (2) will print 1*r, 2*r, ..., 10*r in a single line (see (1)) for a given r.

Code Java:

```for (int r = 1; r <= 10; r++) { // (2) snippet goes here }```
r changes from 1 to 10. For each r (2) snippet is executed. What (2) does? It prints new line and prints the products of all the numbers from 1 to 10 and given number r.

Also, following code illustrates my explanations in a clearer way:
Code Java:

``` public static void main(String[] args) { for (int r = 1; r <= 10; r++) { System.out.println(); productOfAllNumbersBefore10AndR(r); } }   public static void productOfAllNumbersBefore10AndR(int r) { for (int c = 1; c <= 10; c++) { printProductOfTwoNumbersInANiceWay(r, c); } }   public static void printProductOfTwoNumbersInANiceWay(int r, int c) { int product = r * c; if (product < 10) { System.out.print(" " + product + " "); } else { System.out.print(product + " "); } }```
- For each number from 1 to 10 (r), print productOfAllNumbersBefore10AndR
- How to print productOfAllNumbersBefore10AndR ?
- To do this, from the next row (println()), for each number from 1 to 10 (c) printProductOfTwoNumbersInANiceWay, and that two numbers are r and c
- How do I do this?
- First you find the product, then you print it - but with print(), not println(): you don't want a trailing carriage return.

Thank you so much for that really nice explaination, so each time r runs, the second for runs 10 times, or just once? Thats one of the things that confuses me.
• June 18th, 2013, 04:28 PM
angstrem
Re: Help with nested loops. So confusing.
You're welcome!
No matter what you put into the for loop, it will be looped for specified number of times (or before specified condition). So, yes, second for loop will be executed 10 times for each r.