Continue语句使循环跳过主体的其余部分,并在重新进行迭代之前立即重新测试其条件。
C#中的continue语句的工作原理与break语句类似。但是,continue不会强制终止,而是会强制执行循环的下一次迭代,从而跳过两者之间的任何代码。
对于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