# Simple nested loop help (kinda urgent)

• 01-24-2012, 05:44 AM
IAmACodingNoob
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 :)
• 01-24-2012, 05:46 AM
IAmACodingNoob
Re: Simple nested loop help (kinda urgent)
also, can you please explain it in full detail? thanks
• 01-24-2012, 05:56 AM
Eranga
Re: Simple nested loop help (kinda urgent)
Once you compile and run your code what was the output?
• 01-24-2012, 06:01 AM
IAmACodingNoob
Re: Simple nested loop help (kinda urgent)
it printed 35
• 01-24-2012, 06:11 AM
diamonddragon
Re: Simple nested loop help (kinda urgent)
What do You mean by "I don't get loop counter"?
• 01-24-2012, 06:12 AM
IAmACodingNoob
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.
• 01-24-2012, 06:19 AM
angrodm
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
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.
• 01-24-2012, 06:22 AM
diamonddragon
Re: Simple nested loop help (kinda urgent)
This are iterations of Your loops:

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```
• 01-24-2012, 06:28 AM
IAmACodingNoob
Re: Simple nested loop help (kinda urgent)
thanks guys! i get it now!