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 and is . This can be achieved in programming with the help of two nested for loops. The outer loop iterates through the elements of the first set, and the inner loop iterates through the elements of the second set against each element of the first set.
There are a few new things in the code above that we need to understand:
-
The outer
forloop starts from-1and is decremented to-2and then-3, storing a value to the variableeat each iteration of this loop. -
The next statement is also a
forloop, which is in the body of the outer loop. -
The inner
forloop starts iterating from1and is incremented to2and then3, storing a value to the variablemat each iteration. -
The next statement is
System.Console.Write("(" + e + ", " + m + ") ");, which is in the body of the inner loop. The value oferemains-1for all values ofmin the inner loop. Therefore, the program outputs(-1, 1),(-1, 2), and(-1, 3). -
Then, the inner loop terminates, and the control goes to the next iteration of the outer loop.
-
Now, the value of
eis-2. The inner loop starts again and outputs(-2, 1),(-2, 2), and(-2, 3). -
Then, the inner loop terminates again, and the control goes to the next iteration of the outer loop.
-
Now, the value of
eis-3. The inner loop restarts and outputs(-3, 1),(-3, 2), and(-3, 3).
Note: We can display the results of the next
WriteLinestatements on the same line by using\tat the end ofWriteLine. The\ttab includes multiple spaces. We may use" "to show only one space.
The following code demonstrates the functionality of showing the output on a single line:
In the above code, Write does not add a next line automatically, while WriteLine does. Here, the output will be shown in a single line because we have used the Write command.
Practice nested loops
Here are a few example programs that can help you practice writing nested loops. By clicking the “Show Solution” button, you’ll find a program that solves the respective problem. You can copy and paste the given solution into the code widget to make sure the output of your solution matches the given solution. There can be several ways to write correct solutions in programming.
Ordered pairs
Write a program that shows the ordered pairs so that and .
Sample output
( 1 , 1 ) ( 1 , 2 ) ( 1 , 3 ) ( 1 , 4 ) ( 1 , 5 )
( 2 , 1 ) ( 2 , 2 ) ( 2 , 3 ) ( 2 , 4 ) ( 2 , 5 )
( 3 , 1 ) ( 3 , 2 ) ( 3 , 3 ) ( 3 , 4 ) ( 3 , 5 )
( 4 , 1 ) ( 4 , 2 ) ( 4 , 3 ) ( 4 , 4 ) ( 4 , 5 )
( 5 , 1 ) ( 5 , 2 ) ( 5 , 3 ) ( 5 , 4 ) ( 5 , 5 )
Square of asterisks
Write a program that shows a square shape built with asterisks. The number of asterisks on the side of the square is input by the user.
Sample input
5
Sample output
*****
*****
*****
*****
*****
class Test
{
static void Main()
{
// Write your code here
}
}Rectangle of asterisks
Write a program that shows a rectangular shape built with asterisks. The height and width of the rectangle are input by the user.
Sample input
5
8
Sample output
********
********
********
********
********
class Test
{
static void Main()
{
// Write your code here
}
}Right triangle
Write a program that shows a right triangle built with asterisks. The side length of the right triangle is input by the user.
Sample input
5
Sample output
*
**
***
****
*****
class Test
{
static void Main()
{
// Write your code here
}
}Hollow square
Write a program that displays a hollow square built with asterisks. The length of each side of the hollow square shape is input by the user.
Sample input
5
Sample output
*****
* *
* *
* *
*****
class Test
{
static void Main()
{
// Write your code here
}
}