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 3 of 3

Thread: nested loop

  1. #1
    Junior Member
    Join Date
    Apr 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default nested loop

    Can someone explain to me how nested loops work. I don't understand how the output of the following is code is determined. I am new to java. Thanks

    public class Example {
     
            public void exampleMethod(int n) {
                for (int i = 1; i <= n; i++) {
                    for (int j = 1; j <= i; j++) {
                        System.out.print(i+j); 
                    }
                    System.out.print("\n"); }
            }
            public static void main(String[] args) {
     
                Example e = new Example();
                e.exampleMethod(4);
            }
    }

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: nested loop

    And your other question that was answered?

    Regards,
    Jim

  3. #3
    Junior Member
    Join Date
    Apr 2019
    Posts
    24
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: nested loop

    1+1:2
    2+1:3 2+2:4
    3+1:4 3+2:5 3+3:6
    4+1:5 4+2:6 4+3:7 4+4:8

    you can see
    en.verejava.com/?id=19961694923642

    en.verejava.com/?id=19961622874441
    Last edited by veretimothy; April 29th, 2019 at 08:09 PM.

Similar Threads

  1. nested loop
    By karthik.m in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 11th, 2014, 11:09 AM
  2. nested for loop
    By sonusantro79 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 13th, 2014, 07:04 AM
  3. nested for loop
    By yuli in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 11th, 2013, 07:52 PM
  4. Need help with this nested loop
    By beerye28 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 22nd, 2013, 09:51 PM
  5. nested loop
    By b109 in forum Java Theory & Questions
    Replies: 1
    Last Post: May 30th, 2010, 10:05 AM