Program simplenested.cs
Demonstrates use of a nested for loop
using System;
class Program
{
static void Main()
{
Console.WriteLine("\n\tThis program demonstrates the following nested for loop:\n");
string s = "for(int i=1; i<4; i++){\n";
s += "\t\tfor(int j=1; j<4; j++){\n";
s += "\t\tConsole.Write(\"\\t{0} + {1} = {2} // ";
s += "outer loop i = {0}, nested loop j = {1}\",";
s += "i,j,i+j);\n";
s += "\t\t}\n";
s += "\t}";
Console.WriteLine("\t{0}\n",s);
Console.WriteLine("\tAnd here is the output produced by the nested for loop shown above:\n");
for(int i=1; i<4; i++){
for(int j=1; j<4; j++){
Console.WriteLine("\t{0} + {1} = {2} // outer loop i = {0}, nested loop j = {1}", i, j, i + j);
}
}
Console.WriteLine();
Console.WriteLine("\tAs you can see, for each iteration of the outer loop, the nested loop repeats three times\n");
Console.Write("\tPress the Enter key to end program... ");
Console.ReadLine();
}
}