簡單了解Swift語言中的break和continue語句的用法
break語句
在 C 編程語言中的 break 語句有以下兩種用法:
當在循環(huán)中遇到 break 語句, 循環(huán)立即終止,程序控制繼續(xù)循環(huán)語句的后面(退出循環(huán))。
它可用于終止在switch語句(在下一章節(jié))的情況(case)。
如果使用嵌套循環(huán)(即,一個循環(huán)在另一個循環(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
當上述代碼被編譯和執(zhí)行時,它產(chǎn)生了以下結(jié)果:
Value of index is 11 Value of index is 12 Value of index is 13 Value of index is 14
continue語句
在 Swift 編程語言中的 continue 語句告訴循環(huán)停止正在執(zhí)行的語句,并在循環(huán)下一次迭代重新開始。
對于 for 循環(huán),continue 語句使得循環(huán)的條件測試和增量部分來執(zhí)行。對于 while 和 do ... while 循環(huán),continue 語句使程序控制轉(zhuǎn)到條件測試。
語法
在 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
當上述代碼被編譯和執(zhí)行時,它產(chǎn)生了以下結(jié)果:
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
相關(guān)文章
Swift之UITabBarController 導航控制器的自定義
本文給大家介紹swift導航控制器之UITabBarController,本文通過代碼實例給大家講解swift導航控制器,導航控制器類繼承UITabBarController,代碼簡單易懂,需要的朋友可以參考下2015-10-10SwiftUI使用Paths和AnimatableData實現(xiàn)酷炫的顏色切換動畫
這篇文章主要介紹了SwiftUI使用Paths和AnimatableData實現(xiàn)酷炫的顏色切換動畫,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2020-05-05swift中利用runtime交換方法的實現(xiàn)示例
這篇文章主要給大家介紹了關(guān)于swift中利用runtime交換方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2018-05-05在Swift中使用KVO的細節(jié)以及內(nèi)部實現(xiàn)解析(推薦)
這篇文章主要介紹了在Swift中使用KVO的細節(jié)以及內(nèi)部實現(xiàn)解析,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07