Книга: C# 2008 Programmer

Nested for Loop

Nested for Loop

It is common to nest two or more for loops within one another. The following example prints out the times table from 1 to 10:

for (int i = 1; i <= 10; i++) {
 Console.WriteLine("Times table for {0}", i);
 for (int j = 1; j <= 10; j++) {
  Console.WriteLine ("{0} x {1} = {2}", i, j, i*j);

Figure 3-11 shows the output.

Figure 3-11 

Here, one for loop is nested within another for loop. The first pass of the outer loop (represented by i in this example) triggers the inner loop (represented by j). The inner loop will execute to completion and then the outer loop will move to the second pass, which triggers the inner loop again. This repeats until the outer loop has finished executing.

Оглавление книги

Генерация: 0.034. Запросов К БД/Cache: 0 / 0
Вверх Вниз