Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Understanding Nested For Loops  RSS feed

 
Gerald.M Beiles
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone and I hope someone can help me understand nested loops. At this point I think the nested loop is my everest and I cant for the life of me understand how to get things working. This time around in my java learning I'm trying to go through a book chapter by chapter doing all the problem sets as well as the programming project but I cant go any further until I figure out this problem. Here is the code that is giving me trouble:



The output is:

So thats six outputs to go along with the two loops but thats what I don't understand. I even installed eclipse in order to step through the variable one by one and every time I think I got it the little moment of epiphany disappeared. In any case why would it print six lines when the print statement should only print innercount(3x) which is controlled by the second for loop.

This is real simple for some but I cant understand the concept.

Thanks in advance.

G.m.
 
David McCombs
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It prints 6 lines because the out loop controls the inner loop.

Write it like this:



The extra println and brackets for the loops may help make things clearer. With the exception of the added print statement, this is equivalent to the code you posted, and the extra println has no effect on the numerical lines that are printed.

Run the code and then get out a pencil and paper and trace through the code. Write the name of both variables, and then trace through the code. When a variable changes, write the change down. Once you do this you should be able to tell how the outer loop is affecting the inner loop. Make sure the program output matches what you have on paper.

The only time the print statement should print 3 times is when count=3, but it still has to deal with the cases when count=0, count=1 and count=2. When count=0, innercount is never less then count, so control never drops into the body of the inner loop, instead it returns to the outer loop where count is incremented. Hopefully this will get you started in the right direction.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I might also suggest you print the 'count' variable each time you enter the outer loop:


that should also format your output in a way that makes it easier to see what is happening for each loop.
[ November 06, 2006: Message edited by: fred rosenberger ]
 
Gerald.M Beiles
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks David and Fred for your help. I finally had the "ahah" moment and I cant believe I was stumped for so long.

The first go around count = 0 and because of that the inner loop doesn't get initiated(thanks David) which is all fine and good. Second time count =1 and innercount is 0 and this cause the inner loop to run. Third time around count = 2 and this is where I got stuck since I expected innercount = 1 but instead the variable is initialized to 0 and the inner loop is run until the inner loop is less than the outer.

Onto the infamous star pattern problem.

Again thanks to the both of you for helping me to stop



G.m
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!