Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Help with nested loops. So confusing.

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default 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.):
    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

  2. The Following User Says Thank You to abstractmind For This Useful Post:

    Angela1626 (September 26th, 2013)


  3. #2
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Help with nested loops. So confusing.

    1)
    				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)
    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.

    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:
        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.

  4. The Following User Says Thank You to angstrem For This Useful Post:

    Angela1626 (September 26th, 2013)

  5. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Help with nested loops. So confusing.

    Quote Originally Posted by angstrem View Post
    1)
    				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)
    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.

    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:
        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.

  6. #4
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default 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.

Similar Threads

  1. Re: Nested for loops
    By capt_jaggy in forum Loops & Control Statements
    Replies: 5
    Last Post: March 18th, 2013, 05:21 AM
  2. Nested For Loops
    By javapol in forum Java Theory & Questions
    Replies: 10
    Last Post: February 22nd, 2013, 10:46 PM
  3. Nested for loops
    By Fordy252 in forum Loops & Control Statements
    Replies: 2
    Last Post: December 8th, 2012, 11:43 PM
  4. Using Nested Loops
    By m2msucks in forum Loops & Control Statements
    Replies: 7
    Last Post: November 5th, 2011, 07:05 PM
  5. Help with Nested Loops
    By Plural in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 23rd, 2010, 03:31 PM