我们如何在C#的while循环中使用continue语句?
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
热门推荐
10 简短新婚铺被子祝福语
11 迟到的过年祝福语简短
12 生日祝福语贺卡老师简短
13 七一祝福语明信片文案简短
14 薛之谦祝福语简短
15 开业经典祝福语简短
16 送给同桌的祝福语简短
17 高考祝福语 简短12字
18 同事们生日祝福语简短