Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By angrodm

Thread: Simple nested loop help (kinda urgent)

  1. #1
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default Simple nested loop help (kinda urgent)

    I got stuck on this question while doing it:

    int summer=0;
    for(int ind=1; ind <=7;ind+=2)
    for(int x=1;x<=ind;x+=3)
    summer=summer+ind;
    out.println(summer);

    I have no problems with normal nested ones, but I don't get loop counter(i think that's what it's call..haha)

    thank you :)
    Last edited by IAmACodingNoob; 01-24-2012 at 05:01 AM.

  2. #2
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default Re: Simple nested loop help (kinda urgent)

    also, can you please explain it in full detail? thanks
    Last edited by IAmACodingNoob; 01-24-2012 at 04:53 AM.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: Simple nested loop help (kinda urgent)

    Once you compile and run your code what was the output?

  4. #4
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default Re: Simple nested loop help (kinda urgent)

    it printed 35

  5. #5
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Simple nested loop help (kinda urgent)

    What do You mean by "I don't get loop counter"?

  6. #6
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default Re: Simple nested loop help (kinda urgent)

    I don't get how it printed 35, I tried every single way and I still don't get it.

  7. #7
    angrodm is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Re: Simple nested loop help (kinda urgent)

    One thing that might help with nested for loops is inputting brackets. So try looking at the code like this
    Java Code:
    int summer=0;
    for(int ind=1; ind <=7;ind+=2)
    {
          for(int x=1;x<=ind;x+=3)
          {
                 summer=summer+ind;
          }
    }
    out.println(summer);
    Once you do this grab a pen and pad and try to write it out using paper. Have one column showing the summer value one with ind value and one with x value. This will get you something looking like this:
    At the start enter the first for loop
    summer = 0 ind = 1 x = 1
    If you look x <= ind is true so enter the inner loop
    summer = (0 + 1) ind = 1 x = 4
    Now x <= ind is false so exit the loop and go back to the first loop
    summer = 1 ind = 3 x = 1 (x = 1 because it is reset when you start the inner loop again)
    x <= ind returns true so enter the inner loop
    summer = (1 + 1) ind = 3 x = 4

    just keep repeating this until you get it. Hope this helps.
    Vinod Mukundan likes this.

  8. #8
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Simple nested loop help (kinda urgent)

    This are iterations of Your loops:

    Java Code:
    ind    x     sum
    1      1     1
    1      4
    3      1     4
    3      4   
    5      1     9
    5      4     14
    5      7
    7      1     21
    7      4     28
    7      7     35
    7      10
    9
    Last edited by diamonddragon; 01-24-2012 at 05:24 AM.

  9. #9
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default Re: Simple nested loop help (kinda urgent)

    thanks guys! i get it now!

Similar Threads

  1. Nested loop
    By Shasool in forum New To Java
    Replies: 2
    Last Post: 10-23-2011, 05:10 PM
  2. Simple triangle printer (kinda)
    By 5myl in forum New To Java
    Replies: 2
    Last Post: 08-30-2011, 03:29 AM
  3. Nested Loop
    By sehudson in forum New To Java
    Replies: 2
    Last Post: 03-11-2011, 03:39 AM
  4. can some one help me with nested loop?
    By keycoffee in forum New To Java
    Replies: 10
    Last Post: 01-25-2010, 02:49 AM
  5. Nested For Loop
    By yuchuang in forum New To Java
    Replies: 1
    Last Post: 07-08-2007, 01:11 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
  •