C#教程
C#控制语句
C#函数
C#数组
C#面向对象
C#命名空间和异常
C#文件IO
C#集合
C#多线程
C#其它

C# Do-While

C# Do-While 循环

C# do-while 循环 用于多次迭代程序的一部分。如果迭代次数不固定且必须至少执行一次循环,建议使用do-while循环。
C#do-while循环至少执行一次,因为在循环体之后检查条件。
语法:
do{
//code to be executed
}while(condition);
C# do while 循环流程图

C# do-while 循环示例

让我们看一个 C# do-while 循环打印 1 的表的简单示例。
using System;
public class DoWhileExample
    {
      public static void Main(string[] args)
      {
          int i = 1;
          
          do{
              Console.WriteLine(i);
              i++;
          } while (i <= 10) ;
  
     }
   }
输出:
1
2
3
4
5
6
7
8
9
10

C# 嵌套 do-while 循环

在 C# 中,如果在另一个 do-while 循环中使用 do-while 循环,则称为嵌套 do-while 循环。嵌套 do-while 循环对每个外部 do-while 循环都完全执行。
让我们看一个 C# 中嵌套 do-while 循环的简单示例。
using System;
public class DoWhileExample
    {
      public static void Main(string[] args)
      {
          int i=1;  
          
          do{
              int j = 1;
              
              do{
                  Console.WriteLine(i+" "+j);
                  j++;
              } while (j <= 3) ;
              i++;
          } while (i <= 3) ;  
     }
   }
输出:
1 1
1 2
1 3
2 1
2 2 
2 3
3 1
3 2
3 3

C# 不定式 do-while 循环

在 C# 中,如果在 do-while 循环中传递 true ,它将是不定式 do-while循环。
语法:
do{
//code to be executed
}while(true);

C# 无限 do-while 循环示例

using System;
public class WhileExample
    {
      public static void Main(string[] args)
      {
          
          do{
              Console.WriteLine("Infinitive do-while Loop");
          } while(true); 
      }
    }
输出:
Infinitive do-while Loop 
Infinitive do-while Loop
Infinitive do-while Loop
Infinitive do-while Loop
Infinitive do-while Loop
ctrl+c
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4