📜  C#嵌套循环

📅  最后修改于: 2021-05-29 17:40:16             🧑  作者: Mango

嵌套循环是存在于另一个循环中的那些循环。在C#中,允许嵌套for,while和do-while循环,您还可以在其中嵌套任何嵌套的循环 任何其他类型的循环(如for循环)都允许您嵌套if循环。

for循环: for循环的功能与while循环非常相似。基本上在事先知道要执行循环语句的次数时使用。 N代表循环esting是允许的,这意味着你可以使用内循环的另一个循环。

句法:

for(variable initialization; testing condition; increment / decrement)
{
    for(variable initialization; testing condition; 
                                 increment / decrement)
    {
        // Statements 
    }
}

例子:

C#
// C# program to illustrate nested for loop 
using System; 
  
class GFG{
      
public static void Main() 
{ 
      
    // for loop within another for loop 
    // printing GeeksforGeeks 
    for(int i = 0; i < 4; i++) 
        for(int j = 1; j < i; j++) 
            Console.WriteLine("GeeksforGeeks!!"); 
} 
}


C#
// C# program to illustrate nested while loop 
using System; 
  
class GFG{
      
public static void Main() 
{ 
    int x = 1, y = 2;
      
    while (x < 4)
    {
        Console.WriteLine("Outer loop = {0}", x);
        x++;
      
        while (y < 4)
        {
            Console.WriteLine("Inner loop = {0}", y);
            y++;
        }
    }
} 
}


C#
// C# program to illustrate nested do-while loop 
using System; 
  
class GFG{
      
public static void Main() 
{ 
    int x = 1;
    do
    {
        Console.WriteLine("Outer loop = {0}", x);
        int y = x;
      
        x++;
                  
        do
        {
            Console.WriteLine("Inner loop: {0}", y);
            y++;
        } while (y < 4);
  
    } while (x < 4);
} 
}


输出:

GeeksforGeeks!!
GeeksforGeeks!!
GeeksforGeeks!!

while循环:在while循环中,在循环的开头给出测试条件并执行所有语句,直到给定的布尔条件满足为止,当条件变为false时,控件将退出while循环。允许嵌套while循环,这意味着您可以在另一个while循环中使用while循环。但是,不建议使用嵌套的while循环,因为它很难维护和调试。

句法:

while(condition) 
{
   while(condition)
   {
      // Statements 
   }

  // Statements 
}

例子:

C#

// C# program to illustrate nested while loop 
using System; 
  
class GFG{
      
public static void Main() 
{ 
    int x = 1, y = 2;
      
    while (x < 4)
    {
        Console.WriteLine("Outer loop = {0}", x);
        x++;
      
        while (y < 4)
        {
            Console.WriteLine("Inner loop = {0}", y);
            y++;
        }
    }
} 
}

输出:

Outer loop = 1
Inner loop = 2
Inner loop = 3
Outer loop = 2
Outer loop = 3

do-while循环:在C#中, do-while循环与while循环相似,唯一的区别是,它在执行语句后检查条件。 ñesting的do-while循环是允许的,你可以用一个做,而内另一个循环do-while循环,其手段。

句法:

do
{
   // Statements
   do 
   {
      // Statements
   }
   while(condition);
}
while(condition);

例子:

C#

// C# program to illustrate nested do-while loop 
using System; 
  
class GFG{
      
public static void Main() 
{ 
    int x = 1;
    do
    {
        Console.WriteLine("Outer loop = {0}", x);
        int y = x;
      
        x++;
                  
        do
        {
            Console.WriteLine("Inner loop: {0}", y);
            y++;
        } while (y < 4);
  
    } while (x < 4);
} 
}

输出:

Outer loop = 1
Inner loop: 1
Inner loop: 2
Inner loop: 3
Outer loop = 2
Inner loop: 2
Inner loop: 3
Outer loop = 3
Inner loop: 3