c++ interesting syntax for printing new line in std::cout

Viewed 1489

the following code prints a square of '*' characters:

int m = 5; int n=5;
for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
        std::cout << "*" << " \n"[j==5];

Output:

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

My question is regarding to the " \n"[j==5] part. Does anyone know how exactly does this syntax work?

1 Answers
Related