D 程式設計 - continue 語句



D 程式語言中的continue語句有點像break語句。然而,它不是強制終止,而是強制執行迴圈的下一個迭代,跳過中間的任何程式碼。

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

語法

D 中continue語句的語法如下:

continue;

流程圖

D continue statement

示例

import std.stdio;
 
int main () {
   /* local variable definition */
   int a = 10;

   /* do loop execution */
   do {
      if( a == 15) {
         /* skip the iteration */
         a = a + 1;
         continue;
      }
      writefln("value of a: %d", a);
      a++;
     
   } while( a < 20 );
 
   return 0;
}

編譯並執行上述程式碼後,將產生以下結果:

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
d_programming_loops.htm
廣告
© . All rights reserved.