C# continue语句

C# 中的continue语句有点类似于break语句。 但不是强制终止,而是继续强制循环的下一次迭代发生,跳过其间的任何代码。

对于for循环,continue语句将导致循环的条件测试和增量部分执行。对于whiledo...while循环,continue语句导致程序控制传递到条件测试。

语法

C# 中的continue语句的语法如下:

continue;

流程图

示例

using System;
namespace Loops
{
   class Program
   {
      static void Main(string[] args)
      {
         /* local variable definition */
         int a = 10;

         /* do loop execution */
         do
         {
            if (a == 15)
            {
               /* skip the iteration */
               a = a + 1;
               continue;
            }
            Console.WriteLine("value of a: {0}", a);
            a++;
         } 
         while (a < 20);
         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

上一篇: C#循环 下一篇: C#封装