Win a copy of Design for the Mind this week in the Design forum!

# Need Help With For Loops

Michael Hubele
Ranch Hand
Posts: 182
This was from another topic(not my topic someone elses topic) here but I decided to start a new topic since I feel for me this has branched out so much more then what the topic was about.

I understand loops(well I thought I did till this) like I can do basic loops and stuff but this is alot harder then I am use too so I am getting confussed and not sure what is going on anymore.

Here is the orginal topic: http://www.coderanch.com/t/401832/java/java/new-Java-programming

Here is a link to my problem I was trying to break it up into parts and writting comments of what is happening most of my comments ask questions too.

I hope what I wrote makes sence it is very hard to break these things down and then try to explain what is happening to them I also hope this link works.

http://i.1asphost.com/howser/loopy3.html

Joseph Sillitoe
Greenhorn
Posts: 12
Michael,

I am happy to hear that you are trying to fully understand the nuances of code that I posted about the loops. Here is a commented version for you to look at. I hope this helps!

Joseph Sillitoe
Greenhorn
Posts: 12

60 // This is the second break down
61
62
63 int n = 4;
64 for (int x = 1; x < n; x++) // the x(2) was incrimented by 1 from the first break down
65 // since of the x++
66 {
67 System.out.println(); // makes it on a new line
68
69 for (int y= 1; y < 2; y++) // the x gets changed to 2, now how does this work?
70 // it makes sence if 1 since that seems to get the desired result
71 // but thanks to the y-- one we had a zero when we left
72 // and since the int y = 1 was intillized already with the
73 // first time around it does not run anymore otherwise that would
74 // screw things even more up.
75 // so if it is zero that will work since zero is less then 2
76 // but that will not get use the desired result.

77
78 {
79 System.out.print(" "+ y);
80 }
81
82 for (int y = x; y > 0; y--)
83
84
85 {
86 System.out.print(" " + y);
87
88 }
89 }

For the comment starting on line 69, The reason this works is because 'y' is reinitialized to equal '1' every time we enter that for loop. I assure you that the code does indeed work and get the result that we are looking for. The more I think about it, I think that you might be getting confused on the scope of the x and y variables. The code could be re-writen like this and still do the exact same thing:

Ranch Hand
Posts: 95
Originally posted by Michael Hubele:
This was from another topic(not my topic someone elses topic) here but I decided to start a new topic since I feel for me this has branched out so much more then what the topic was about.

I understand loops(well I thought I did till this) like I can do basic loops and stuff but this is alot harder then I am use too so I am getting confussed and not sure what is going on anymore.

Here is the orginal topic: http://www.coderanch.com/t/401832/java/java/new-Java-programming

Here is a link to my problem I was trying to break it up into parts and writting comments of what is happening most of my comments ask questions too.

I hope what I wrote makes sence it is very hard to break these things down and then try to explain what is happening to them I also hope this link works.

http://i.1asphost.com/howser/loopy3.html

Michael Hubele
Ranch Hand
Posts: 182
Ok it makes sence now I did not know that once it was done the for loops inside and started again that the ones inside the master for loop would be reinitlized.

I been doing one this other problem but I ran into a problem.

this is what is should look like about:

So basicly this incriments by 2 stars each time till it reaches 9 stars then stops and goes back down but the reverse way. So I figure I will break them into 2 sections 1 going up the other going down.

I been working on the up part and this what I got so far but when I did it in my head and on paper it should work but when I right the code into netbeans and run it does not work. So I must have screwed up somewhere.

Here is what I got:

[ January 03, 2006: Message edited by: Michael Hubele ]

Sheriff
Posts: 14691
16
There are many ways to do this.
But why don't you write the loop the way you explained it in your message ?

You said:
So basicly this incriments by 2 stars each time till it reaches 9 stars then stops and goes back down but the reverse way.

This sounds like

So I figure I will break them into 2 sections 1 going up the other going down.

So this sounds like

Think of the rest by yourself.
The variable 'i' will give you the number of stars to print.
You don't need those ugly 'if(x == 2)' and so on.

Ranch Hand
Posts: 95
Originally posted by Michael Hubele:

What Satou said. But also, in case you were wondering, you messed up big in the second "for" loop. You start with y=0
You stop when y=x
You make x one bigger each time through the loop.

In other words, y never changes, and the test in the second clause of the for loop never fails.

Fixing this doesn't fix the code, though. Do what Satou said.

Michael Hubele
Ranch Hand
Posts: 182
<hr></blockquote>

What Satou said. But also, in case you were wondering, you messed up big in the second "for" loop. You start with y=0
You stop when y=x
You make x one bigger each time through the loop.

In other words, y never changes, and the test in the second clause of the for loop never fails.

Fixing this doesn't fix the code, though. Do what Satou said.[/QB]

Oh that was a typo LOL also why it was not working(I wrote it all on paper must have made my y look like a x when I was writing fast) and actually it does fix the code. It works when I change x++ to y++.

I actually get the result.

I am going to contiune to use the if statment way to finish it. then I am going to take a look at your guys improved method.

Since it seems it is alot easier your way since it took me forever too figure out how to do that much but at least I figured it out. It may not be pretty but at least it works and I am on the right track.

I have another question not about your code since I have not taken a full look at it yet but when my output comes it looks like this:

As you can see mine does not have a very dimonandy look to it. I know my System.out.print("*"); look like that and I know I can just do this (" * ") to make some more spaces and move it out but is there any commands that do this? And make spaces since I got alot of stars to move and sometimes is hard to track of how many spaces I made it move.

I know there is ones for new line and stuff but I can't remember if there is one for a space.

But of course this is not a major issue.

Sheriff
Posts: 14691
16
"It's working so it's alright" way of thinking is not a good way of thinking in programming. You'll understand that when you'll have to correct other people's code. You'll see all the nonsense stuff and say "What the hell is he doing ?".
Anyway, try to refactor this as soon as possible. Imagine you want to print a 100 stars diamond, do you think you'll write if() statements for each line ?

Moreover, you already understand that it is painful to output spaces. Why ? Because your code is not appropriate for this. So rewrite it first, and add the formatting stuff after.

About the spaces, come on Michael, you should try to figure this out by yourself. Your biggest line is 9 stars. And you know how many stars you print per line. You can then guess how many spaces it takes to fill a line up to 9 characters. Divide this by two, and you'll get the number of spaces for each side.
Try it, and if you still have problems, I'll be glad to help you and post the code.

This is what I have :

[ January 04, 2006: Message edited by: Satou kurinosuke ]

Ranch Hand
Posts: 95
Originally posted by Satou kurinosuke:
"It's working so it's alright" way of thinking is not a good way of thinking in programming. You'll understand that when you'll have to correct other people's code. You'll see all the nonsense stuff and say "What the hell is he doing ?".
Anyway, try to refactor this as soon as possible. Imagine you want to print a 100 stars diamond, do you think you'll write if() statements for each line ?

About the spaces, come on Michael, you should try to figure this out by yourself. Your biggest line is 9 stars. And you know how many stars you print per line. You can then guess how many spaces it takes to fill a line up to 9 characters. Divide this by two, and you'll get the number of spaces for each side.

Yeah. What he said.

FWIW, this exact problem (in two pieces) is addressed in Reges and Stuart's book

Building Java Programs If you click on the link and scroll down to section 2.5.3, you'll see that they have a nice, methodical system for solving problems of this nature. I would copy it here, but the formatting of their table would be a little tricky in UBB.

Michael Hubele
Ranch Hand
Posts: 182
Originally posted by Satou kurinosuke:
"It's working so it's alright" way of thinking is not a good way of thinking in programming. You'll understand that when you'll have to correct other people's code. You'll see all the nonsense stuff and say "What the hell is he doing ?".
Anyway, try to refactor this as soon as possible. Imagine you want to print a 100 stars diamond, do you think you'll write if() statements for each line ?

Moreover, you already understand that it is painful to output spaces. Why ? Because your code is not appropriate for this. So rewrite it first, and add the formatting stuff after.

About the spaces, come on Michael, you should try to figure this out by yourself. Your biggest line is 9 stars. And you know how many stars you print per line. You can then guess how many spaces it takes to fill a line up to 9 characters. Divide this by two, and you'll get the number of spaces for each side.
Try it, and if you still have problems, I'll be glad to help you and post the code.

This is what I have :

[ January 04, 2006: Message edited by: Satou kurinosuke ]

I think you miss understood what I was saying.

Like with "It's working so it's alright" I never said that in any words like that yes I said it works but the turth is that it does work and as I stated before it maybe not be the prettiest way of doing it but it worked.

I knew already when writting the code that it is not the most efficent way of doing it but Like it was said there are many different ways of doing some code some ways will be alot better then others.

As I also stated I would be learning your way too after I finished. Since as I stated before it looked alot easier then my way and more improved then the way I was doing it but since it was half way done I might as well finish it.

It may help me one day out when I am writting a test and a better way is not so obvious and I don't have time to figure out how to make it the better way in the time I would have and since in a test unless it says that this code will be used to be motified later on, my way would be expected.... also it is good to do it different ways so you always can improve on it.

Michael Hubele
Ranch Hand
Posts: 182

<hr></blockquote>
Yeah. What he said.

FWIW, this exact problem (in two pieces) is addressed in Reges and Stuart's book

Building Java Programs If you click on the link and scroll down to section 2.5.3, you'll see that they have a nice, methodical system for solving problems of this nature. I would copy it here, but the formatting of their table would be a little tricky in UBB.

I did not understand too much of 2.5.3 but I heard of "Pseudocode" before and maybe I should invest some time learning it? maybe it will help me break up my questions easier before I code it.

So is there any good tutorials Pseudocode? Like it seems Pseudocode makes it a bit easier to break down a problem and write it in away that it is still in english but it also is written in away so it can be easily changed into code.

Like that is my biggest problem right now it taking a problem and breaking it up into peices to start coding.

So if there are any good tips that would be nice.

By the way I finished the dimond thing useing your guy's improved method I have not figured out how to do the space yet though...

Joseph Sillitoe
Greenhorn
Posts: 12
Please don't get defensive. Satou is absolutly correct in the advice he gave you. Indeed, if you abandon the whole "if (x==2)" thought process and started rethinking the problem in the ways that Satou, Adam, and I are hinting at, the "space" part of the problem will almost solve itself.

Maybe it would be helpful to understand if instead of the diamond filled with *'s and spaces, you did something like this:

or even better:

Joseph Sillitoe
Greenhorn
Posts: 12
Good Job! You are almost there.

A couple of things that I would like to point out:

You have this:

And then later, this:

Both of these loops do the exact same thing (iterate through the loop exactly x number of times), so maybe we can abstract that out into a method?
(Also, please note that it is considered good practice to start counting from zero, like you do in the first one.)

But wait... we need to be able to print other characters besides a star. Lets refactor this to:

Now to use that method in your code. All you would have to do is:

Now all we have to do is get those pesky space chars printed ahead of our stars.
[ January 05, 2006: Message edited by: Joseph Sillitoe ]

Ranch Hand
Posts: 95
Originally posted by Michael Hubele:

I did not understand too much of 2.5.3 but I heard of "Pseudocode" before and maybe I should invest some time learning it? maybe it will help me break up my questions easier before I code it.

So is there any good tutorials Pseudocode? Like it seems Pseudocode makes it a bit easier to break down a problem and write it in away that it is still in english but it also is written in away so it can be easily changed into code.

Like that is my biggest problem right now it taking a problem and breaking it up into peices to start coding.

So if there are any good tips that would be nice.

By the way I finished the dimond thing useing your guy's improved method I have not figured out how to do the space yet though...

The important thing in the section I referred to was the chart.

You want something like this:

To do this, you would use one outer loop that is counting line numbers, and three inner loops that generate the spaces, asterisks and trailing spaces. The asterisks are slightly more obvious - the spaces aren't hard either, though. Instead of starting with none and counting up, start with some and count down

[invalid, but conceptually correctcode:]

This may not exactly work - I didn't pay attention to whether the increment operators should add one or 2 and things like that, but it DOES construct something basically diamond shaped with spaces on each side - you can tweak the incrementors yourself.

You really ought to work through the first few chapters of the book I linked to before. Especially the parts that deal with loops - they are explaining fairly clearly exactly what you are struggling with. If you go back to the beginning of the chapter, you will find that they walk through it step-by-step