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

C# While

C# While 循环

在 C# 中,while 循环 用于多次迭代程序的一部分。如果迭代次数不固定,建议使用while循环而不是for循环。
语法:
while(condition){
//code to be executed
}
流程图:
C# while 循环流程图

C# While 循环示例

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

C# 嵌套 While 循环示例:

在 C# 中,我们可以在另一个 while 循环中使用 while 循环,称为嵌套 while 循环。嵌套while循环执行一次外循环就完整执行。
让我们看一个C#编程语言中嵌套while循环的简单例子。
using System;
public class WhileExample
    {
      public static void Main(string[] args)
      {
          int i=1;  
          while(i<=3) 
          {
              int j = 1;
              while (j <= 3)
              {
                  Console.WriteLine(i+" "+j);
                  j++;
              }
              i++;
          }  
     }
   }
输出:
1 1
1 2
1 3
2 1
2 2 
2 3
3 1
3 2
3 3

C# Infinitive While 循环示例:

我们也可以通过传递 true 作为测试条件来创建无限 while 循环。
using System;
public class WhileExample
    {
      public static void Main(string[] args)
      {
          while(true)
          {
                  Console.WriteLine("Infinitive while Loop");
          }  
      }
    }
输出:
Infinitive while Loop 
Infinitive while Loop
Infinitive while Loop
Infinitive while Loop
Infinitive while Loop
ctrl+c
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4