Start learning free

LESSON 191 OF 347 · NESTED LOOPS & BRUTE FORCE

Nested Loops in C#: Rows, Columns and break

Lesson Read the full explanation

A nested loop has one loop inside of another. These are typically used for working with two dimensions such as printing stars in rows and columns as shown below. When a loop is nested inside another loop, the inner loop runs many times inside the outer loop. In each iteration of the outer loop, the inner loop will be re-started. The inner loop must finish all of its iterations before the outer loop can continue to its next iteration.

Example:

for(int row = 0; row < 3; row++) {
	for(int col = 0; col < 5; col++) {
		Console.Write("*");
	}
    Console.WriteLine();
}

// This code prints a rectangle with 3 rows and 5 columns, filled with '*'

Iterating through multidimensional arrays

Nested loops are also helpful when we want to iterate through a multi dimensional array, for example:

int[][] arr = {{1, 2}, {3, 4}, {5, 6}};

for (int i = 0; i < arr.length; i++) {
	for (int j = 0; j < arr[i].length; j++) {
		Console.WriteLine(arr[i][j]);
	}
}

This outputs each sub-element in arr one at a time. Note that for the inner loop, we are checking the .length of arr[i], since arr[i] is itself an array.

Breaking nested loops

In a nested loop, a break statement only stops the loop it is placed in. Therefore, if a break is placed in the inner loop, the outer loop still continues. For example:

int[][] arr = {{1, 2}, {3, 4}, {5, 6}};

for (int i = 0; i < arr.length; i++) {
	for (int j = 0; j < arr[i].length; j++) {
		if(arr[i][j] == 3) {
			break;
		}
		Console.WriteLine(arr[i][j]);
	}
}

// prints the numbers 1, 2, 5 and 6 on different lines

When i is 1 and j is 0, we execute the break. This means we stop the inner loop and go back to the outer loop to continue from the next iteration, which is i = 2. And as you can see, we print all the elements of row 2.

However, if the break is placed in the outer loop, all of the looping stops. For example:

int[][] arr = {{1, 2}, {3, 4}, {5, 6}};

for (int i = 0; i < arr.length; i++) {
	if(arr[i][0] == 3) {
		break;
	}
	for (int j = 0; j < arr[i].length; j++) {
		Console.WriteLine(arr[i][j]);
	}
}
// prints the numbers 1 and 2 on different lines

Assignment
Follow the Coding Tutorial and let's write some nested loops.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 4
    Ctrl + Enter to check

    C# · NESTED LOOPS & BRUTE FORCE

    Nested Loops in C#, explained

    What a nested loop is

    A nested loop is a loop inside another loop. The outer loop runs its body once per iteration, and each time it does, the inner loop starts from scratch and runs all of its iterations before the outer loop moves on. If the outer loop runs 5 times and the inner loop 10 times, the inner body runs 5 × 10 = 50 times.

    Nested loops are the natural tool for anything with two dimensions: rows and columns of text, grids, tables, and every pair of elements in a collection. The next problems in this chapter use them to try all pairs by brute force.

    Printing a rectangle

    for (int row = 0; row < 3; row++) {
        for (int col = 0; col < 5; col++) {
            Console.Write("*");
        }
        Console.WriteLine();
    }
    

    This prints 3 rows of 5 stars:

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

    Two console methods do the work. Console.Write prints its text and stays on the same line. Console.WriteLine() with no argument only ends the current line. Together they let the inner loop build one row, and the outer loop start the next.

    Looping over a jagged array

    A C# array of arrays, int[][], is called a jagged array. Each element is itself an int[], and the rows may have different lengths. A nested loop visits every value:

    int[][] arr = { new[] {1, 2}, new[] {3, 4}, new[] {5, 6} };
    
    for (int i = 0; i < arr.Length; i++) {
        for (int j = 0; j < arr[i].Length; j++) {
            Console.WriteLine(arr[i][j]);
        }
    }
    

    This prints 1, 2, 3, 4, 5 and 6 on separate lines. Note that the inner loop checks arr[i].Length, the length of the current row, not arr.Length. In C# the length of an array is the property Length, with a capital L.

    C# also has rectangular arrays, written int[,]. For those, Length is the total count of cells, and GetLength(0) and GetLength(1) give the number of rows and columns.

    break in nested loops

    A break stops only the loop it is directly inside.

    break in the inner loop

    for (int i = 0; i < arr.Length; i++) {
        for (int j = 0; j < arr[i].Length; j++) {
            if (arr[i][j] == 3) {
                break;
            }
            Console.WriteLine(arr[i][j]);
        }
    }
    

    It prints 1, 2, 5 and 6. When i is 1 and j is 0, the value is 3 and the inner loop ends. The outer loop then continues with i = 2, and row 2 prints in full. The 4 is skipped as well, because it sits after the 3 in the same row.

    break in the outer loop

    for (int i = 0; i < arr.Length; i++) {
        if (arr[i][0] == 3) {
            break;
        }
        for (int j = 0; j < arr[i].Length; j++) {
            Console.WriteLine(arr[i][j]);
        }
    }
    

    This prints only 1 and 2. At i = 1 the first value of the row is 3, and the break ends the outer loop, so rows 1 and 2 never print.

    C# has no labelled break. To leave both loops from inside the inner one, move the loops into a method and use return, or set a flag that the outer loop checks. The Two Sum lesson uses the return approach.

    The exercise, step by step

    The task is to print a rectangle of stars with 5 rows and 10 columns.

    1. Outer loop. A for loop with int row from 0 while row < 5. It runs once per row. The body never reads row; it only counts passes.
    2. Inner loop. Inside it, a loop with int col from 0 while col < 10. It runs 10 times for every pass of the outer loop.
    3. Print a star. Inside the inner loop, Console.Write("*"), so the 10 stars share a line.
    4. End the row. After the inner loop's closing brace, but still inside the outer loop, Console.WriteLine() moves to the next line.
    for (int row = 0; row < 5; row++) {
        for (int col = 0; col < 10; col++) {
            Console.Write("*");
        }
        Console.WriteLine();
    }
    

    The output is five lines of ten stars.

    Common mistakes

    • Putting Console.WriteLine() inside the inner loop. Every star would end up on its own line, 50 lines in all.
    • Leaving it out. All 50 stars would print on one long line.
    • Reusing the same variable name. C# does not let an inner loop declare a variable with the same name as one in the enclosing scope, so use i and j, or row and col.
    • Using the wrong length. In a jagged array the inner bound is arr[i].Length. With arr.Length the loop can run past the end of a short row and throw IndexOutOfRangeException.
    • Expecting break to leave both loops. It leaves only the innermost one.

    Key takeaways

    • The inner loop restarts and finishes on every pass of the outer loop, so the total work is the product of the two counts.
    • Print with Console.Write inside the inner loop and Console.WriteLine() after it.
    • Use arr[i].Length for the inner bound of a jagged array.
    • break affects only the loop it is in.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free