Search⌘ K
AI Features

Nested Loops

Explore how nested loops work in C#, combining outer and inner loops to manage repetitive tasks effectively. Understand their structure through examples like Cartesian products and practice writing programs that use nested loops to generate ordered pairs, shapes, and patterns with asterisks.

Nested loop

When we use a loop inside another loop, we call them nested loops. The first loop is called the outer loop, and the second loop is called the inner loop. Nested loops may be the same type or different types of loops. Both loops can be for loops or while loops. The outer loop can be a for loop and the inner loop a while loop, or vice versa.

Let’s use the Cartesian product of two sets as an illustration of nested loops.

Cartesian product

The Cartesian product of the sets A={1,2,3}A = \{-1, -2, -3\} and B={1,2,3}B = \{1, 2, 3\} is A× ...