在 C 編程語言中的 break 語句有以下兩種用法:
當(dāng)在循環(huán)中遇到 break 語句, 循環(huán)立即終止,程序控制繼續(xù)循環(huán)語句的后面(退出循環(huán))。
它可用于終止在switch語句(在下一章節(jié))的情況(case)。
如果使用嵌套循環(huán)(即,一個(gè)循環(huán)在另一個(gè)循環(huán)), break語句將停止最內(nèi)層循環(huán)的執(zhí)行,并開始執(zhí)行下一行代碼塊之后的代碼塊。
在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
當(dāng)上述代碼被編譯和執(zhí)行時(shí),它產(chǎn)生了以下結(jié)果:
Value of index is 11 Value of index is 12 Value of index is 13 Value of index is 14