Hello, i am trying to learn the for loop with asteriks

So this is my code, what i am asking is,does outer loop set j to 0 every time outer loop iterates?
Like, first loop i = 0, check if i is <= 5 then go to the inner loop, j=0, check if j is <= i then print "*" then add +1 to j then end inner loop, and an endline.

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

using namespace std;

int main()
{
    for (int i=0; i <=5; i++)
    {
        for (int j=0;j<=i ; j++)
        {
            cout << "*";

        }
        cout << endl;

    }
    return 0;
}


Whenever in doubt, make a version that prints more information:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main()
{
    using std::cout;
    for ( int i=0; i<=5; ++i ) {
        cout << "i=" << i << " j=";
        for ( int j=0; j<=i; ++j ) {
            cout << j << ' ';
        }
        cout << '\n';
    }
    return 0;
}

@keskiverto Thanks for the reply, i got that now!

But then i was trying to create a diamond pattern and created this code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

#include <iostream>

using namespace std;

int main()
{
    int sayi;
    cout <<"Bir tek sayi giriniz >"; // enter an odd number is the meaning of this sayi=number :D
    cin >> sayi;

    for (int i=0; i <=sayi; i+=2)
    {
        for (int k=0; k <=sayi-i/2; k++)
        {

            cout << " " ;
        }

        for (int j=0; j<=i ; j++)
        {
            cout << "*";

        }
        cout << endl;

    }


    for (int a=0; a <=sayi; a+=2)
    {
        for (int b=sayi/2; b <=sayi+a/2; b++)
        {

            cout << " " ;
        }
        for (int c=0; c<sayi-a; c++)
        {

            cout << "*";
        }

        cout << endl;


    }


    return 0;
}


How does this look?
Last edited on
You seem to have sayi+1 rows. Is that intentional?
Topic archived. No new replies allowed.