Results 1 to 9 of 9
- 01-24-2012, 04:44 AM #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 05:01 AM.
- 01-24-2012, 04:46 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 04:53 AM.
- 01-24-2012, 04:56 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: Simple nested loop help (kinda urgent)
Once you compile and run your code what was the output?
- 01-24-2012, 05:01 AM #4
Member
- Join Date
- Jan 2012
- Posts
- 8
- Rep Power
- 0
Re: Simple nested loop help (kinda urgent)
it printed 35
- 01-24-2012, 05:11 AM #5
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
Re: Simple nested loop help (kinda urgent)
What do You mean by "I don't get loop counter"?
- 01-24-2012, 05:12 AM #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.
- 01-24-2012, 05:19 AM #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
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: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);
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, 05:22 AM #8
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
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.
- 01-24-2012, 05:28 AM #9
Member
- Join Date
- Jan 2012
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
Nested loop
By Shasool in forum New To JavaReplies: 2Last Post: 10-23-2011, 05:10 PM -
Simple triangle printer (kinda)
By 5myl in forum New To JavaReplies: 2Last Post: 08-30-2011, 03:29 AM -
Nested Loop
By sehudson in forum New To JavaReplies: 2Last Post: 03-11-2011, 03:39 AM -
can some one help me with nested loop?
By keycoffee in forum New To JavaReplies: 10Last Post: 01-25-2010, 02:49 AM -
Nested For Loop
By yuchuang in forum New To JavaReplies: 1Last Post: 07-08-2007, 01:11 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks