我們一起聊聊Swift 條件控制和循環(huán)
歡迎您閱讀 Mastering Swift 基礎教程,本文我們將介紹 Swift 中的變量、常量和數(shù)據(jù)類型。如果你尚未安裝 Xcode 和配置 Swift 開發(fā)環(huán)境,請您先閱讀這篇文章。
接下來,我們啟動 Xcode,然后選擇 "File" > "New" > "Playground"。創(chuàng)建一個新的 Playground 并命名為 "ConditionalsAndLoops"。
if...else
if-else 語句是一種常見的條件控制結(jié)構(gòu),用于根據(jù)條件的真假執(zhí)行不同的代碼塊。
Swift Code
var temperature = 28
if 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
if...else if...else 語句允許您按順序處理多個條件。
Swift Code
let score = 85
if 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
switch 語句是一種用于處理多個可能情況的流程控制結(jié)構(gòu)。在 Swift 中,switch 語句可以用于處理各種數(shù)據(jù)類型,包括整數(shù)、浮點數(shù)、字符串 等。
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
for-in 語句用于遍歷集合(如數(shù)組、字典或范圍)的循環(huán)結(jié)構(gòu)。
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 是一個閉區(qū)間運算符,表示一個包括從 1 到 5 的整數(shù)范圍。這個范圍包括 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 循環(huán)還支持 where 子句,它可以更好地控制循環(huán)代碼何時執(zhí)行。
Swift Code
for index in 1...5 where index % 2 == 0 {
print("Index is \(index)")
}
/**
Output:
Index is 2
Index is 4
*/
while
while 語句是一種用于創(chuàng)建循環(huán)的控制流結(jié)構(gòu),只要給定條件為真,就會反復執(zhí)行一段代碼塊。
Swift Code
var count = 1
while 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
repeat-while 語句是一種循環(huán)結(jié)構(gòu),類似于 while 循環(huán),不同之處在于 repeat-while 會先執(zhí)行一次代碼塊,然后在滿足條件的情況下重復執(zhí)行。
Swift Code
var count = 1
repeat {
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 循環(huán)會先執(zhí)行一次代碼塊,然后檢查條件 count <= 5 是否仍然為真。只要條件為真,就會重復執(zhí)行代碼塊。這確保了至少會執(zhí)行一次,即使條件一開始就不滿足。
在 TypeScript 中,目前并沒有對應于 Swift 中 repeat-while 的語法。但可以通過 do-while 循環(huán)來實現(xiàn)類似的功能。
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 語句等相關(guān)的知識。通過與 TypeScript 語法的對比,希望能幫助您更好地理解 Swift 的相關(guān)特性。