Results 1 to 3 of 3
  1. #1
    Phong is offline Member
    Join Date
    Jan 2015
    Posts
    1
    Rep Power
    0

    Default for loop redundancy question

    PHP Code:
    public static void drawFirstHalf() {
          for (int line = 1; line <= SIZE; line++) {   
             System.out.print("|");
             for (int dots1 = 1; dots1 <= SIZE - line; dots1++) {
                printDot();
             }
             for (int roof = 1; roof <= SIZE - (SIZE - line); roof++) {
                printRoof();
             }
             for (int dots2 = 1; dots2 <= (SIZE * 2) - (line * 2); dots2++) {
                printDot();
             }
             for (int roof = 1; roof <= SIZE - (SIZE - line); roof++) {
                printRoof();
             }
             for (int dots1 = 1; dots1 <= SIZE - line; dots1++) {
                printDot();
             }
             System.out.println("|"); 
          }
       }
    How would I get rid of the redundancy of the dots1 and roof for loop in the nested for loop? The lines are just repeated and I'm not sure how to get rid of the redundancy.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: for loop redundancy question

    Moved from Threads and Synchronization

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: for loop redundancy question

    Maybe you can create even smalled (and private!) methods? Basically, you method has the following structure:

    Java Code:
    outer-loop {
       A;
       B;
       C;
       B;
       A;
    }
    ...where A, B and C represent the inner loops; you can create methods A(), B() and C() ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Little question about do while loop need help :)
    By Stanimir in forum New To Java
    Replies: 3
    Last Post: 03-19-2014, 03:20 PM
  2. Reduce redundancy in nested loop.
    By masterboemi in forum New To Java
    Replies: 5
    Last Post: 02-09-2011, 01:08 AM
  3. Loop question
    By rich123 in forum New To Java
    Replies: 6
    Last Post: 02-08-2011, 02:26 AM
  4. loop question
    By ccie007 in forum New To Java
    Replies: 22
    Last Post: 08-15-2010, 08:29 PM
  5. reduce redundancy
    By hydride in forum New To Java
    Replies: 4
    Last Post: 04-06-2010, 08:25 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
  •