break語(yǔ)句
在 C 編程語(yǔ)言中的 break 語(yǔ)句有以下兩種用法:
當(dāng)在循環(huán)中遇到 break 語(yǔ)句, 循環(huán)立即終止,程序控制繼續(xù)循環(huán)語(yǔ)句的后面(退出循環(huán))。
它可用于終止在switch語(yǔ)句(在下一章節(jié))的情況(case)。
如果使用嵌套循環(huán)(即,一個(gè)循環(huán)在另一個(gè)循環(huán)), break語(yǔ)句將停止最內(nèi)層循環(huán)的執(zhí)行,并開(kāi)始執(zhí)行下一行代碼塊之后的代碼塊。
語(yǔ)法
在Swift 編程中的 break語(yǔ)句的語(yǔ)法如下:
break
流程圖
實(shí)例
import Cocoa
var index = 10
do{
index = index + 1
if( index == 15 ){
break
}
println( "Value of index is /(index)")
}while index < 20
當(dāng)上述代碼被編譯和執(zhí)行時(shí),它產(chǎn)生了以下結(jié)果:
Value of index is 11Value of index is 12Value of index is 13Value of index is 14
continue語(yǔ)句
在 Swift 編程語(yǔ)言中的 continue 語(yǔ)句告訴循環(huán)停止正在執(zhí)行的語(yǔ)句,并在循環(huán)下一次迭代重新開(kāi)始。
對(duì)于 for 循環(huán),continue 語(yǔ)句使得循環(huán)的條件測(cè)試和增量部分來(lái)執(zhí)行。對(duì)于 while 和 do ... while 循環(huán),continue 語(yǔ)句使程序控制轉(zhuǎn)到條件測(cè)試。
語(yǔ)法
在 Swift 中的 continue 語(yǔ)句的語(yǔ)法如下:
continue
流程圖
實(shí)例
import Cocoa
var index = 10
do{
index = index + 1
if( index == 15 ){
continue
}
println( "Value of index is /(index)")
}while index < 20
當(dāng)上述代碼被編譯和執(zhí)行時(shí),它產(chǎn)生了以下結(jié)果:
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





















