aspose file tools*
The moose likes Beginning Java and the fly likes Understanding the logic behind making Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Understanding the logic behind making "*" triangles" Watch "Understanding the logic behind making "*" triangles" New topic
Author

Understanding the logic behind making "*" triangles

Jeong Ryu
Greenhorn

Joined: Sep 25, 2012
Posts: 28
Hello. I was hoping if someone could explain to me in details behind the logic of creating the triangle. I don't understand how every time the counter is iterated, the asterisks output the same amount as the counter. Wouldn't you have to assign the counter to the output of "*" every time there is a new iteration? For example, having a variable amountAsterisks = counter, and when counter becomes 2, the amountAsterisks also becomes 2 - where then it would output two asterisks.

Could someone please explain how the for loop works without having to explicitly state it?

[NOTE]: The code below does not correctly output the triangle. It would be great if someone could provide the correct code and an explanation of how it works.

Thank you!

Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

Your code will not produce a triangle, but would rather give a 10x10 square.

I assume, you are working to get a triangle which has one * in first row, two *s in second row and so on.

The logic is , you should have inner loop condition set to out loop variable.

So you check inner loop untill it reaches the value of the outer loop .

Ashwin Sridhar
SCJP | SCWCD | OCA
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Your code does totally different thing from what you expect. As others said, nobody is going to implement it for you.
I would suggest you try to make it work for a simpler version of what you want, to produce the output like this:

This is very easy to implement, it shouldn't take you long. Then, if you can't get it from head, try to draw what you want to accomplish, how asterisks are positioned in each line, how many of them are in each line and how many spaces you need to print in each line. That way you'll get an idea how to calculate those values for each iteration, and finally the code should be straightforward once you get that.


The quieter you are, the more you are able to hear.
Jeong Ryu
Greenhorn

Joined: Sep 25, 2012
Posts: 28
Thanks for the posts, everyone!

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.


Hi!

Yes, I understand that no one will write the code for me. On my original post, I simply said that it would be great. I should have been more specific as to the statement not being a request, sorry. And I know that the program does not work correctly to produce the triangle. However, I am more concerned with the logic behind nested for loops than the actual program.

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 to be equal to the incremental counter. I would think that it would just produce one asterisk.

P.S. How do I quote multiple posts?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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
Greenhorn

Joined: Sep 25, 2012
Posts: 28
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.


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
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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
Greenhorn

Joined: Sep 25, 2012
Posts: 28
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?


I see. So, when the outer loop iterates once, the inner loop iterates 10 times (assuming that we are doing column <= 10)?
Which would makes sense why my code produced a rectangle.

I've experimented with my code, and I was able to find the correct one to create a triangle:


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
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
Jeong Ryu
Greenhorn

Joined: Sep 25, 2012
Posts: 28
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.


Yes, finally!! The whole 'counter' thing tripped me over, haha.
Thank you so much for your help!
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

You're very welcome. Glad I could help.
Silly Sod
Greenhorn

Joined: Mar 22, 2013
Posts: 1
Hi. I'm also very new to Java, I managed to get a solution for the triangles problem. If anyone is interested.
I dont suppose this is a very neat solution, since I'm very new to Java, but the output at least was correct.
Any comments or tips very much welcome.



output was

*
***
*****
*******
*********
***********
*************
***************
*****************
*******************

I'm using eclipse btw.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Understanding the logic behind making "*" triangles