Ashwin Sridhar
SCJP | SCWCD | OCA
The quieter you are, the more you are able to hear.
Jeff Verdegan wrote:Hi, and welcome to the Ranch!
First, nobody is going to write the code for you. That's not how this site works. People come here to help others learn, not to hand out solutions. So if you show some effort and explain clearly what you're having trouble with, people will be happy to nudge you in the right direction toward figuring things out for yourself.
As for your actual question, it's not clear what you're asking. We have a couple loops, and on each iteration of the inner loop, we print a '*' on the current line, and on each iteration of the outer loop, we print a newline. I have no idea what you mean by "assign the counter to the output of '*'", or why you think we need another variable, or what you think that variable would represent.
Jeong Ryu wrote:
I guess the more specific question would be that I don't understand how the nested for loops work in a way that outputs the asterisks in relation to the counter. For example, when counter = 1, one asterisk would be produced. When the counter = 2, two asterisks would be produced. I don't see how the increase of counters tell the program to produce a certain amount of asterisks. I would think that it would only produce one asterisk, regardless of how many times the counter incremented.
What exactly does the outer loop do to the inner loop, and vice-versa?
To be more specific, based on the code:
The outer for loop is initializing that row = 1, and that while row is less than or equal to 10, it will increment. And in the inner loop, the column will do the same. So, every time the row and column increment, the system will output the asterisk. What I can't grasp is how having a nested for loop statement causes the asterisks outputted equals the incremental counter. I would think that it would just produce one asterisk.
P.S. How do I quote multiple posts?
Jeff Verdegan wrote:
Jeong Ryu wrote:
I guess the more specific question would be that I don't understand how the nested for loops work in a way that outputs the asterisks in relation to the counter. For example, when counter = 1, one asterisk would be produced. When the counter = 2, two asterisks would be produced. I don't see how the increase of counters tell the program to produce a certain amount of asterisks. I would think that it would only produce one asterisk, regardless of how many times the counter incremented.
What exactly does the outer loop do to the inner loop, and vice-versa?
To be more specific, based on the code:
The outer for loop is initializing that row = 1, and that while row is less than or equal to 10, it will increment. And in the inner loop, the column will do the same. So, every time the row and column increment, the system will output the asterisk. What I can't grasp is how having a nested for loop statement causes the asterisks outputted equals the incremental counter. I would think that it would just produce one asterisk.
Each pass through the inner loop produces one asterisk. The inner loop runs 10 times, so it produces 10 asterisks.
The outer loop runs 10 times, each time running the inner loop (which runs 10 times, producing 10 asterisks) and then printing a newline.
Try changing the numbers from 10 and 10 to something smaller and different for each loop, say, 2 and 3. Then try 3 and 2.
P.S. How do I quote multiple posts?
If you mean nested quotes, just quoting a post that quotes another does that for you. If you mean getting pieces from two separate posts, no nested, into your post, you'd have to open a text editor, hit the quote button for each post, copy what's there, and paste it into your editor.
Jeong Ryu wrote:
Are you saying that the for loops iterate based on the number on the counter? So when counter = 1, it loops once. And when counter = 2, it loops twice, etc. If that is the case, I can understand how the incremented counter affects how many asterisks are produced.
Jeff Verdegan wrote:
Jeong Ryu wrote:
Are you saying that the for loops iterate based on the number on the counter? So when counter = 1, it loops once. And when counter = 2, it loops twice, etc. If that is the case, I can understand how the incremented counter affects how many asterisks are produced.
No. The counter is only used here to count the number of times executed so far, and to test in the condition to see if we're done yet. In the inner loop, when column is 0, we print one '*', and when it's 1, we print one '*', and when it's 2, we print one '*', ..., and when it's 9 we print one '*', and when it's 10 we're done so we don't execute the body at all any more.
A for loop iterates as long as its condition is true. Those particular for loops each iterate 10 times.
You can find more details by reading the looping section of a tutorial, such as here: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html
Also, did you try using different numbers like I suggested?
Jeong Ryu wrote:
I see. So, when the outer loop iterates once, the inner loop iterates 10 times (assuming that we are doing column <= 10)?
I think I understand why I needed "column <= row". When outer for loop iterates once, the counter = 1. And in the inner for loop, it will iterate 10 times. However, because it starts with "column = 1" and is "<= row" (which is 1), the loop stops after iterating once - creating a single asterisk. And after the inner loop terminates, the outer for loop iterates again, but now with "counter = 2", which then allows the inner for loop to iterate twice, which produces two asterisks, and so on and so forth.
Is this correct?
And yes, I did try using the other numbers. Did you want me to use these numbers so that I could understand what I've said above?
Jeff Verdegan wrote:
Jeong Ryu wrote:
I see. So, when the outer loop iterates once, the inner loop iterates 10 times (assuming that we are doing column <= 10)?
Correct.
I think I understand why I needed "column <= row". When outer for loop iterates once, the counter = 1. And in the inner for loop, it will iterate 10 times. However, because it starts with "column = 1" and is "<= row" (which is 1), the loop stops after iterating once - creating a single asterisk. And after the inner loop terminates, the outer for loop iterates again, but now with "counter = 2", which then allows the inner for loop to iterate twice, which produces two asterisks, and so on and so forth.
Is this correct?
Yup.
And yes, I did try using the other numbers. Did you want me to use these numbers so that I could understand what I've said above?
Yes, or at least to get you started on the path to understanding. I wanted you to see the relationship between what was in your code and what the output was. Smaller numbers are easier to work with for this stuff and easier to trace through by hand, and using different values for the two loops lets you see which one contributes what. I've been doing this stuff for a couple of decades now, and I still do that when I encounter something new or confusing. It's a technique you'll want to hang onto.