idiom43-Break outer loop-multidimensional-set-iteration-variable.cs

CSharp · Console

/Program.cs

using System;

int[,] m = {
    { 1, 2, 3 },
    { 11, 0, 30 },
    { 5, -20, 55 },
    { 0, 0, -60 }
};

for (
        int i = 0,
        rows = m.GetLength(0),
        cols = m.GetLength(1); i < rows; i++)
{
    for (int j = 0; j < cols; j++)
    {
        if (m[i, j] < 0)
        {
            Console.WriteLine(m[i, j]);
            i = int.MaxValue - 1; // Break outer loop.
            break;
        }
    }
}
;