如何在 C# 中的 while 循环中使用 continue 语句?
csharpprogrammingserver side programming更新于 2025/5/19 3:07:17
continue 语句使循环跳过其主体的其余部分并在重复之前立即重新测试其条件。
C# 中的 continue 语句的工作方式有点像 break 语句。但是,continue 不会强制终止,而是强制进行循环的下一次迭代,跳过其间的任何代码。
对于 while 循环,continue 语句使程序控制权传递给条件测试。
以下是在 while 循环中使用 continue 语句的完整代码。
示例
using System;
namespace Demo {
class Program {
static void Main(string[] args) {
/* 局部变量定义 */
int a = 10;
/* 循环执行 */
while (a < 20) {
if (a == 15) {
/* 跳过迭代 */
a = a + 1;
continue;
}
Console.WriteLine("value of a: {0}", a);
a++;
}
Console.ReadLine();
}
}
}
输出
value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 16 value of a: 17 value of a: 18 value of a: 19

