In this tutorial I will show simple nested for loop example in c sharp. When a loop performs within a loop is called nested loop. In this tutorial I have created two loops after creating first loop I have also created another loop within the first loops.
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | class Program { static void Main(string[] args) { /* variable definition */ int i, j; // Simple Nested for loop for (i = 1; i <= 12; i++) { for (j = 1; j <= 10; j++) { Console.Write(j + " "); } Console.WriteLine("\t\t" + i); } Console.WriteLine("-------------------------------------------"); Console.ReadLine(); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 1 2 3 4 5 6 7 8 9 10 1 1 2 3 4 5 6 7 8 9 10 2 1 2 3 4 5 6 7 8 9 10 3 1 2 3 4 5 6 7 8 9 10 4 1 2 3 4 5 6 7 8 9 10 5 1 2 3 4 5 6 7 8 9 10 6 1 2 3 4 5 6 7 8 9 10 7 1 2 3 4 5 6 7 8 9 10 8 1 2 3 4 5 6 7 8 9 10 9 1 2 3 4 5 6 7 8 9 10 10 1 2 3 4 5 6 7 8 9 10 11 1 2 3 4 5 6 7 8 9 10 12 ------------------------------------------- |