• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reading nested loop

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I am pretty bad at reading nested loops. The following code is for constructing triangle rows.


source: textbook Big Java.

If someone can walk me step by step, it would be very helpful because I will have a better idea of how to read/code nested loops in general! Thanks a lot!

 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you just read through them like anything else...you handle the first one first...so..let's assume width is 4.

we reach line 2, and set i to 1. We check and find that i is less than width, so we enter the body of the loop.

the first thing we do in this loop is...start a new loop. so we set j to 1. we check to see if j is <= i, which it is (both are 1). so we enter this loop.

we are now on line 6. and we basically set the string to "[ ]" (since it was an empty string).

NOTE: the inner loop does not have any curly brackets, so there is only one line in the body. The indentation doesn't necessarily tell you that. If line 7 were indented, it would still not be part of the inner loop. This is one reason to ALWAYS use brackets, even when your body only has one line.

we have finished the inner loop, so, we increment j to 2. Test j <= i, which fails, so we exit the inner loop, and proceed with the next thing in the outer loop.

that gets us to line 7, so we put a newline on the end of our string.

we now exit the body of the outer loop, so we increment i to 2. i <= width is true, so we execute the body of the outer loop.

The first thing we do is execute a new loop. Note that this iteration of the loop on line 5 has NO IDEA it was ever executed before. it's starting over. So, set j to 1, test j <= i (true), append "[ ]" to the string, which now contains "[ ]/n[ ]".

increment j to 2, which IS <= i, so we add another "[ ]". j becomes 3, exiting the inner loop, and we append "\n" to the string.

...

and you just keep going like that.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Cheryl Scodario wrote:Hi, I am pretty bad at reading nested loops. The following code is for constructing triangle rows.


source: textbook Big Java.

If someone can walk me step by step, it would be very helpful because I will have a better idea of how to read/code nested loops in general! Thanks a lot!



I would suggest putting in your usual braces , even if its one line.

To read nested for loop as above, say "width" is 3 for simplicity


 
Cheryl Scodario
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Fred. It was very helpful!
reply
    Bookmark Topic Watch Topic
  • New Topic