# Thread: Simple nested loop help (kinda urgent)

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

## 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 06:01 AM.

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

## 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 05:53 AM.

3. ## Re: Simple nested loop help (kinda urgent)

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

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

## Re: Simple nested loop help (kinda urgent)

it printed 35

5. Senior Member
Join Date
Jan 2012
Posts
210
Rep Power
9

## Re: Simple nested loop help (kinda urgent)

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

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

## 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. Member
Join Date
Jan 2012
Posts
9
Rep Power
0

## 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.

8. Senior Member
Join Date
Jan 2012
Posts
210
Rep Power
9

## 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 06:24 AM.

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

## Re: Simple nested loop help (kinda urgent)

thanks guys! i get it now!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•