break語句
在 C 編程語言中的 break 語句有以下兩種用法:
當在循環中遇到 break 語句, 循環立即終止,程序控制繼續循環語句的后面(退出循環)。
它可用于終止在switch語句(在下一章節)的情況(case)。
如果使用嵌套循環(即,一個循環在另一個循環), break語句將停止最內層循環的執行,并開始執行下一行代碼塊之后的代碼塊。
語法
在Swift 編程中的 break語句的語法如下:
break
流程圖
實例
import Cocoa
var index = 10
do{
index = index + 1
if( index == 15 ){
break
}
println( "Value of index is /(index)")
}while index < 20
當上述代碼被編譯和執行時,它產生了以下結果:
Value of index is 11Value of index is 12Value of index is 13Value of index is 14
continue語句
在 Swift 編程語言中的 continue 語句告訴循環停止正在執行的語句,并在循環下一次迭代重新開始。
對于 for 循環,continue 語句使得循環的條件測試和增量部分來執行。對于 while 和 do ... while 循環,continue 語句使程序控制轉到條件測試。
語法
在 Swift 中的 continue 語句的語法如下:
continue
流程圖
實例
import Cocoa
var index = 10
do{
index = index + 1
if( index == 15 ){
continue
}
println( "Value of index is /(index)")
}while index < 20
當上述代碼被編譯和執行時,它產生了以下結果:
Value of index is 11Value of index is 12Value of index is 13Value of index is 14Value of index is 16Value of index is 17Value of index is 18Value of index is 19Value of index is 20