C# - continue 語句



C# 中的 continue 語句在某種程度上類似於 break 語句。但是,它並沒有強制終止迴圈,而是強制執行迴圈的下一輪迭代,跳過中間的任何程式碼。

對於 for 迴圈,continue 語句會導致迴圈的條件測試和增量部分執行。對於 whiledo...while 迴圈,continue 語句會導致程式控制傳遞到條件測試。

語法

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

continue;

流程圖

C# continue statement

示例

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
csharp_loops.htm
廣告

© . All rights reserved.