This week's book giveaway is in the Cloud/Virtualizaton forum.We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

Stars program help

Bryce Heath
Greenhorn
Posts: 19
Hey guys
Working on my for loops and trying to make a diamond shape with this program, but for some reason my second for is making 3 stars when it should only do 2. Ill keep working on it but any ideas would be great.

Tim Chaung
Greenhorn
Posts: 17
I'm guessing at what you're trying to say....
and I think your problem is that in your second series of for loops.. you have
for (int row = 2; row > MIN_ROWS; row--)

it will make 3 stars because it takes 3 steps to get to below zero. changing it to a '>=' should fix it?

Bryce Heath
Greenhorn
Posts: 19
Hey thanks for the quick reply, I tried that also with no luck. Im not really sure whats up with it because with MIN_ROW being 0 if I did for (int row = 2; row > MIN_ROW; row--) wouldent that set row to 2 run the loop decrease row to 1 run the loop a second time decrease row to 0 making the row> MIN_ROW end the loop because the 0 of row is now not greater but = to MIN_ROW? Hope that makes sense look forward to more posts on the subject.

fred rosenberger
lowercase baba
Bartender
Posts: 12123
30
put in print statements to see what is really going on. I changed your second nested loops to this (and am completely ignoring the first set of loops here):

and you get this:

C:\slop>java Stars
row: 2, star: 2
*row: 1, star: 2
*row: 1, star: 1
*

you come in, and both row and star are set to 2. you print the first star.

star gets decremented to 1. we test star >= row, which is false, so we exit the inner loop.

row is decremented to 1. 1 is > MIN_ROWS, so we enter the inner loop
star is set to 2.

star > row is true (2 >= 1), so we print again. we decrement star, making it 1.

we go back to the top of the inner loop, and check if star >= row (1 >=1), which is true, and we print another star (the 3rd one).

star is decremented to 0. 0 is NOT >= 1, so we exit the inner loop.

we decrement row, making it 0. 0 is NOT > MIN_ROWS, so we exit the outer loop.

Bryce Heath
Greenhorn
Posts: 19
Hey guys thanks for the reply.
I guess my problem was that I dident understand nested for's right. I thought it would run the outer for, enter inner for, run inner for once and drop back out to the outer for to recheck again if the outer was still true then back into the inner. Want to make sure I am understanding it correctly when thinking that it will run the outer for go to inner for and keep running inner for till it is not longer true, then drop back to the outer for?

Campbell Ritchie
Sheriff
Posts: 48949
60
I think you have got it right, but try something like this, and you can see for yourself:I haven't run it yet, so it's probably full of misspellings which the compiler will complain about.

Campbell Ritchie
Sheriff
Posts: 48949
60
There was only one misspelling, which I have corrected.