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.
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?
Joined: Sep 16, 2011
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.
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.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Joined: Sep 16, 2011
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?