Swift continue語句

Swift 4中的continue語句告訴迴圈停止,並在迴圈的下一次迭代開始時再次啟動。

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

語法

Swift 4中continue語句的語法如下 -

continue

流程圖

示例代碼

var index = 10

repeat {
   index = index + 1
   if( index == 15 ){
      continue
   }
   print( "Value of index is \(index)")
} while index < 20

編譯並執行上述代碼時,會產生以下結果 -

Value of index is 11
Value of index is 12
Value of index is 13
Value of index is 14
Value of index is 16
Value of index is 17
Value of index is 18
Value of index is 19
Value of index is 20

上一篇: Swift迴圈語句 下一篇: Swift字串