歡迎您閱讀 Mastering Swift 基礎教程,本文我們將介紹 Swift 中的變量、常量和數據類型。如果你尚未安裝 Xcode 和配置 Swift 開發環境,請您先閱讀這篇文章。
接下來,我們啟動 Xcode,然后選擇 "File" > "New" > "Playground"。創建一個新的 Playground 并命名為 "ConditionalsAndLoops"。
if-else 語句是一種常見的條件控制結構,用于根據條件的真假執行不同的代碼塊。
Swift Code
var temperature = 28if temperature > 30 { print("It's a hot day")} else { print("It's not so hot")}// Output: It's not so hot
TypeScript Code
let temperature = 28;if (temperature > 30) { console.log("It's a hot day");} else { console.log("It's not so hot");}// Output: "It's not so hot"
if...else if...else 語句允許您按順序處理多個條件。
Swift Code
let score = 85if score >= 90 { print("Excellent!")} else if score >= 80 { print("Good")} else if score >= 70 { print("Average")} else { print("Fail")}// Output: Good
TypeScript Code
const score: number = 85;if (score >= 90) { console.log("Excellent!");} else if (score >= 80) { console.log("Good");} else if (score >= 70) { console.log("Average");} else { console.log("Fail");}// Output: Good
switch 語句是一種用于處理多個可能情況的流程控制結構。在 Swift 中,switch 語句可以用于處理各種數據類型,包括整數、浮點數、字符串 等。
Swift Code
let dayOfWeek = "Wednesday"switch dayOfWeek {case "Monday": print("Start of the workweek")case "Tuesday", "Wednesday", "Thursday": print("Midweek, work in progress")case "Friday": print("It's Friday, almost there!")case "Saturday", "Sunday": print("Weekend vibes")default: print("Invalid day")}// Output: Midweek, work in progress
相比 JavaScript 和 TypeScript,在 Swift case 分支中,無需使用 break 跳出分支。
TypeScript Code
const dayOfWeek: string = "Wednesday";switch (dayOfWeek) { case "Monday": console.log("Start of the workweek"); break; case "Tuesday": case "Wednesday": case "Thursday": console.log("Midweek, work in progress"); break; case "Friday": console.log("It's Friday, almost there!"); break; case "Saturday": case "Sunday": console.log("Weekend vibes"); break; default: console.log("Invalid day");}// Output: "Midweek, work in progress"
for-in 語句用于遍歷集合(如數組、字典或范圍)的循環結構。
Swift Code
for index in 1...5 { print("Index is /(index)")}/** Output: Index is 1 Index is 2 Index is 3 Index is 4 Index is 5 */
在以上代碼中,1...5 是一個閉區間運算符,表示一個包括從 1 到 5 的整數范圍。這個范圍包括 1 和 5 兩個端點。
TypeScript Code
for (let index = 1; index <= 5; index++) { console.log(`Index is ${index}`);}/** Output: Index is 1 Index is 2 Index is 3 Index is 4 Index is 5 */
在 Swift 中 for-in 循環還支持 where 子句,它可以更好地控制循環代碼何時執行。
Swift Code
for index in 1...5 where index % 2 == 0 { print("Index is /(index)")}/** Output: Index is 2 Index is 4 */
while 語句是一種用于創建循環的控制流結構,只要給定條件為真,就會反復執行一段代碼塊。
Swift Code
var count = 1while count <= 5 { print("Count is /(count)") count += 1}/** Output: Count is 1 Count is 2 Count is 3 Count is 4 Count is 5 */
TypeScript Code
let count: number = 1;while (count <= 5) { console.log(`Count is ${count}`); count++;}/** Output: Count is 1 Count is 2 Count is 3 Count is 4 Count is 5 */
repeat-while 語句是一種循環結構,類似于 while 循環,不同之處在于 repeat-while 會先執行一次代碼塊,然后在滿足條件的情況下重復執行。
Swift Code
var count = 1repeat { print("Count is /(count)") count += 1} while count <= 5/** Output: Count is 1 Count is 2 Count is 3 Count is 4 Count is 5 */
以上代碼中,repeat-while 循環會先執行一次代碼塊,然后檢查條件 count <= 5 是否仍然為真。只要條件為真,就會重復執行代碼塊。這確保了至少會執行一次,即使條件一開始就不滿足。
在 TypeScript 中,目前并沒有對應于 Swift 中 repeat-while 的語法。但可以通過 do-while 循環來實現類似的功能。
TypeScript Code
let count: number = 1;do { console.log(`Count is ${count}`); count++;} while (count <= 5);/** Output: Count is 1 Count is 2 Count is 3 Count is 4 Count is 5 */
本文我們介紹了 Swift 中 if/else, else if, switch 和 loops 語句等相關的知識。通過與 TypeScript 語法的對比,希望能幫助您更好地理解 Swift 的相關特性。
本文鏈接:http://www.tebozhan.com/showinfo-26-60904-0.html我們一起聊聊Swift 條件控制和循環
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com
上一篇: MySQL臨時表在高并發環境下可能導致哪些性能問題?
下一篇: Swift 數組、字典和集合