Files
Csharp/C#入门/Lesson18_循环语句while/Program.cs
T
2025-09-30 16:49:43 +08:00

64 lines
1.9 KiB
C#

using System;
namespace Lesson18_循环语句while
{
class Program
{
static void Main(string[] args)
{
#region 知识点一 作用
// 让程序重复执行某段代码,直到满足特定条件为止。
#endregion
#region 知识点二 语法
// while (bool类型的值)
// {
// // 需要循环执行的代码
// }
//进入unity之后 基本不会使用while循环
while (true)
{
Console.WriteLine("#");
// 这里的代码如果不被结束会一直执行
// 这种无限循环会导致程序无法结束,除非手动停止。
break; // 这里的 break 语句可以用来跳出循环
}
#endregion
#region 知识点三 嵌套使用
int a = 0;
int b = 0;
while (a < 5)
{
Console.WriteLine("#");
a++;
while (b < 5)
{
Console.WriteLine("*");
b++;
}
}
#endregion
#region 知识点四 控制循环
int count = 0;
while (count < 5)
{
Console.WriteLine("*");
count++;
if (count == 3)
{
Console.WriteLine("跳过当前循环");
continue; // 跳过当前循环的剩余部分,直接进入下一次循环
}
if (count == 4)
{
Console.WriteLine("结束循环");
break; // 结束整个循环
}
Console.WriteLine("当前计数: " + count);
}
#endregion
}
}
}