i know the code but still confuse that how it works?

i know the code but still confuse that how it works?
Can any body explain how this code works,,, cuz the "*" is not mentioned in any integer so how integers work on it...

{
for (int i=0;i<=20;i++)
{
for (int j=0;j<=i;j++)
{
cout<<'*';
}
cout<<endl;
}
system("pause");
}

its result is:
*
**
***
****
*****
...
Last edited on
it's iterating over the two for loops and printing out the *.

http://www.cplusplus.com/doc/tutorial/control/

edit. you could also print out the values of i and j after each for loop to get a better idea of how it's working.
Last edited on
The outer loop runs once then -> inner for loop is going to run 1 time.... then exit out to the outer loop....then back into the inner loop...the inner loop is going to run 2 times.... back out to the outer loop...then back into the inner loop.... then, the inner loop runs 3 times.....and it keeps doing that up to 20 times...
***The amount of times the inner "food loop" runs before it exits out to the outer loop, is based on the variable "i" of the outer loop which increments each time after the inner loop has finished.

Hope this helps i remember having trouble understanding this

Run this code this will solidify your understanding after you look at it for a few minuets.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

using namespace std;

int main()
{
    for (int i = 0; i < 10; i++) {
        for(int j = 0; j< i; j++)
        {
            cout << j+1 << "\tO " << endl;
        }
        cout << i+1<<"\tX" << endl;
    }
    return 0;
}


or try this one

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;

int main()
{
    for (int i = 0; i < 10; i++) {
        for(int j = 0; j< i; j++)
        {
            cout << "*" << endl;
        }
        cout << endl;
    }
    return 0;
}
Last edited on
Topic archived. No new replies allowed.