Explaining Code Reading Problem: for (int i=0; i<=9; i++)

  • Thread starter Thread starter chmate
  • Start date Start date
  • Tags Tags
    Code Reading
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
4 replies · 3K views
chmate
Messages
37
Reaction score
0
Hi!

Code:
for (int i=0; i<=9; i++)
{
   for (int j=0; j<i; j++)
       cout << " ";
   for (int j=i; j<=9; j++)
        cout << "*"
   cout << endl;
}

Can anyone explain me what these lines are doing?

Thank you!
 
Physics news on Phys.org
what do you think the are doing?
Do you know how to trace through code like that?
...write it all out on bpaper.

If you don't know what a for loop does then i can't help ya.
the "count<<endl" is just a new line.

EDIT: oh yeah you also count ean error in your j-for loops
 
Last edited:
I don't know. It seems like it's printing 9 lines, each line having an increasing number of spaces and decreasing number of stars. But i can't figure out what something like that can possibly look like.
 
It does this:
**********
*********
********
*******
******
*****
****
***
**
*
Press any key to continue
When you don't try to redfine j in:
for (int j=i; j<=9; j++).. (I used x)
and correct the syntax error by adding a semi-colon to:
count << "*"
Sorry for butting in...:blushing: