Converting for loop to do-while loop

Hello,

I have been working on this small program for 3 days and am able to write for loops quite easily. This time I need to convert my for loop to a do-while loop and am having quite a bit of difficulty. Using Codeblocks, here is what I have:

//For Loop
for (row = 5; row >= 1; row--)
{
for (int star = 1; star <= row; star++)
cout << "*";
cout << endl;
}

//Do While Loop
star = 0;
row = 5;
do
{
cout << "*";
cout << endl;
star++;
star = 0;
row--;
}
while ((star <= row) && (row >= 1));

My for loop works perfectly, but my do while loop only prints 1 line of 5 stars instead of what it should, which is this: (It's what my for loop prints)

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

Any advice is greatly appreciated. Thank you.
welp, first i'd look at the fact that your first example contains two loops nested, and your second example (the while loop) contains only one.
and i'm suprised that you even get the output you mention from the while loop with that endl where it is, but i am myself a beginner.
Last edited on
Topic archived. No new replies allowed.