Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Removing curly bracket in a nested for C#

I’m trying to remove {} in a nested for but for some reason it doesn’t seem to work.

public void PrintBoard()
{
    for (int i = 0; i < _matrix.N; i++)
    {
        for (int j = 0; j < _matrix.M; j++)
        {
            Console.Write(_matrix.Get[i, j]);
        }
        Console.WriteLine();
    }
}

This is what I’m trying, but the output is kinda different, it seems that the line breaks doesn’t work properly. I also would like to know why it’s behaving like that.

public void PrintBoard()
{
    for (int i = 0; i < _matrix.N; i++)
       for (int j = 0; j < _matrix.M; j++)
           Console.Write(_matrix.Get[i, j]);
       Console.WriteLine();
}

// Output
// .........

// Output expected
// ...
// ...
// ...

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This is because you can only remove the braces of the inner foreach, since the outer forceach has 2 statements in the block.

public void PrintBoard()
{
    for (int i = 0; i < _matrix.N; i++)
    {
        for (int j = 0; j < _matrix.M; j++)
            Console.Write(_matrix.Get[i, j]);
        Console.WriteLine();
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading