Swift4 Day-07 Control Flow ( 2 )

Albert
Albert
Sep 3, 2018 · 3 min read

控制轉移語句

控制轉移語句 ( control transfer statement )可以改變程式執行的順序,或是跳轉執行程式。Swift 有五種控制轉移語句:

・continue
・break
・fallthrough
・return
・throw

continue

continue 表示在循環中,立即停止本次循環,重新開始此流程的下個循環。

上述範例中,for in 迴圈在除以 2 餘數等於 0 時 continue,所以只會印出有餘數的1、3、5、7、9

break

break 會立刻停止循環流程,然後接著繼續執行程式。switch要提前停止程式也可以使用 break

fallthrough

fallthough 是 break 的對手,break會立刻停止,而 fallthrough則會繼續執行下面的程式碼。

使用 fallthrough 前
使用 fallthrough 後

這裡強調一下,fallthrough是直接移動到下一個情況,並不是直接貫穿喔!

今天的 Control Flow就到這裡囉!如果有其他的問題歡迎留言給我!\(*´∀`)/

Albert 的 Swift4 語法集:

Swift4 Day-01 基本語法
Swift4 Day-02 Option 及 nil
Swift4 Day-03 集合類型 — Array
Swift4 Day-04 集合類型 — Set
Swift4 Day-05 集合類型 — Dictionary
Swift4 Day-06 Control Flow ( 1 )

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade