Results 1 to 6 of 6
  1. #1
    djtoxicdance is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Smile Need help analysising for loop

    im having a tough time understanding the for loop in this code and what the limits of the loop are. I need help breaking the code down and understanding a for loop.
    [CODE]class Diamond {
    public static void main(String[] args) {
    for (int i = 1; i < 10; i += 2) {
    for (int j = 0; j < 9 - i / 2; j++)
    System.out.print(" ");

    for (int j = 0; j < i; j++)
    System.out.print("*");

    System.out.print("\n");
    }

    for (int i = 7; i > 0; i -= 2) {
    for (int j = 0; j < 9 - i / 2; j++)
    System.out.print(" ");

    for (int j = 0; j < i; j++)
    System.out.print("*");

    System.out.print("\n");
    }
    }
    }
    /*
    *
    ***
    *****
    *******
    *********
    *******
    *****
    ***
    *
    */[CODE/]

  2. #2
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    10

    Default

    What is your question? exactly what is it you are stuck on or don't understand?

  3. #3
    djtoxicdance is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default

    I donít understand the code. You see what i posted for the code was given to us in class. i was told to learn how the code works and how to change it so you get different shapes. Like to how to change it from a diamond to just a triangle. But i donít understand how the for loop works and what its limits are. Because i figured out that to get a triangle all i need is a for loop with different limits.

  4. #4
    pw2467 is offline Member
    Join Date
    May 2011
    Posts
    1
    Rep Power
    0

    Default

    Add System.out.println for both of the loop variables ( 'i' and 'j' ). Seeing how they change will help you understand the loops and see exactly how each of the loops exits.

  5. #5
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    10

  6. #6
    djtoxicdance is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default

    ok both solutions helped me out a lot and especially the link. i also had my professor help me a bit so i understand it better now. thanks for all the help. glad i joined this site has good help

Similar Threads

  1. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 06:46 PM
  2. [Q] Loop issue (while loop)
    By iriscience in forum New To Java
    Replies: 9
    Last Post: 01-31-2011, 05:21 PM
  3. Convert do while loop to for loop
    By sandeeptheviper in forum New To Java
    Replies: 3
    Last Post: 01-03-2011, 01:37 PM
  4. Replies: 5
    Last Post: 04-30-2010, 06:05 PM
  5. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 09:46 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
  •