如何在 C# 中的 while 循环中使用 continue 语句?
continue 语句导致循环跳过其主体的其余部分,并在重复之前立即重新测试其条件。
C# 中的 continue 语句的工作原理有点像 break 语句。然而,它不是强制终止,而是强制进行循环的下一轮迭代,跳过其中的任何代码。
对于 while 循环,continue 语句将使程序控制权传递给条件测试。
以下是在 while 循环中使用 continue 语句的完整代码。
示例
using System;
namespace Demo {
class Program {
static void Main(string[] args) {
/* local variable definition */
int a = 10;
/* loop execution */
while (a < 20) {
if (a == 15) {
/* skip the iteration */
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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP