比較運算子會比較兩個相同型別的值,並回傳一個 Bool 值(true 或 false)。
如果你試著比較不同型別的值,編譯器會產生錯誤。
Swift 中標準的比較運算子有:
| 運算子 | 說明 | 範例 |
|---|---|---|
== |
等於 |
1 == 1(true) |
!= |
不等於 |
1 != 2(true) |
< |
小於 |
4 < 5(true) |
<= |
小於或等於 |
4 <= 4(true) |
> |
大於 |
3 > 1(true) |
>= |
大於或等於 |
2 >= 2(true) |
if 與 else 敘述條件式會根據條件評估為 true 或 false 來執行程式碼。
最常見的條件式是 if 敘述,它只在條件為 true 時執行一段程式區塊。
使用 if 關鍵字撰寫 if 敘述,後面接著條件,以及以大括號括住的程式區塊:
let wheelCount = 4
if wheelCount == 4 {
print("Your vehicle is a car")
}
// Prints "Your vehicle is a car"
你可以將 if 搭配 else 子句,在條件為 false 時執行替代的程式碼:
let precipitationInMM = 0.5
if precipitationInMM > 0.5 {
print("You will need an umbrella")
} else {
print("You will not need an umbrella")
}
// Prints "You will not need an umbrella"
若要依序測試多個條件,可以用 else if 將敘述串接起來。
if 敘述不需要 else 分支,但你可以加上一個,用來處理所有條件都不符合的情況:
let favoriteFruit = "Apple"
if favoriteFruit == "Banana" {
print("Your favorite fruit is a banana")
} else if favoriteFruit == "Orange" {
print("Your favorite fruit is an orange")
} else if favoriteFruit == "Apple" {
print("Your favorite fruit is an apple")
} else {
print("Your favorite fruit is not a banana, orange, or apple")
}
// Prints "Your favorite fruit is an apple"
if 運算式在現代 Swift 中,if 敘述也可以當作運算式使用,產生一個值。
每個分支都會評估出一個值,讓你能輕鬆地有條件地指定結果:
let cpuBits = 32
let architecture = if cpuBits == 64 {
"x86_64"
} else {
"x86_32"
}
print(architecture)
// Prints "x86_32"
三元條件運算子是 if-else 運算式的簡潔寫法。
它會評估一個條件,並根據該條件為 true 或 false 回傳兩個值其中之一。
這個運算子由三個部分組成:condition ? trueExpression : falseExpression。
如果 condition 評估為 true,就會評估並回傳 trueExpression;否則會評估並回傳 falseExpression。
三元運算子很適合簡單、單行的選擇:
let showFahrenheit = true
let temperatureInCelsius = 30
let temperature = showFahrenheit ? temperatureInCelsius * 9 / 5 + 32 : temperatureInCelsius
print(temperature)
// Prints 86
上面的範例等同於以下寫法的精簡版:
let showFahrenheit = true
let temperatureInCelsius = 30
let temperature = if showFahrenheit {
temperatureInCelsius * 9 / 5 + 32
} else {
temperatureInCelsius
}
在這個練習中,你會寫一些程式碼,幫助你做好購買車輛的準備。
你有三個任務:判斷自己是否負擔得起某輛車的月付金、判斷你需要哪一種駕照,以及估算中古車的合理價格。
你鎮上的汽車經銷商都在舉辦五年期、零利率的促銷活動,而你想好好把握。但你不確定自己是否負擔得起想買的那輛車的月付金。
月付金就是車輛的總價除以五年期間的月數。
請實作 canIBuy(vehicle:price:monthlyBudget:) 函式,它接受下列引數:
vehicle:你想購買的車輛名稱。price:你想購買的車輛價格。monthlyBudget:你每月負擔得起的金額。這個函式應根據下列條件回傳對應的訊息:
"Yes! I'm getting a <vehicle>"。"I'll have to be frugal if I want a <vehicle>"。"Darn! No <vehicle> for me"。canIBuy(vehicle: "1974 Ford Pinto", price: 516.32, monthlyBudget: 100.00)
// returns "Yes! I'm getting a 1974 Ford Pinto"
canIBuy(vehicle: "2011 Bugatti Veyron", price: 2_250_880.00, monthlyBudget: 10000.00)
// returns "Darn! No 2011 Bugatti Veyron for me"
canIBuy(vehicle: "2020 Indian FTR 1200", price: 12_500, monthlyBudget: 200)
// returns "I'll have to be frugal if I want a 2020 Indian FTR 1200"
你決定要買一輛中古車,而你需要判斷自己需要哪一種駕照才能駕駛它。
請實作 licenseType(numberOfWheels:) 函式,它接受引數 numberOfWheels,也就是你想購買的車輛的輪子數量。
這個函式應根據下列條件回傳對應的訊息:
"You will need a motorcycle license for your vehicle"。"You will need an automobile license for your vehicle"。"You will need a commercial trucking license for your vehicle"。"We do not issue licenses for those types of vehicles"。licenseType(numberOfWheels: 2)
// returns "You will need a motorcycle license for your vehicle"
licenseType(numberOfWheels: 6)
// returns "You will need an automobile license for your vehicle"
licenseType(numberOfWheels: 18)
// returns "You will need a commercial trucking license for your vehicle"
licenseType(numberOfWheels: 0)
// returns "We do not issue licenses for those types of vehicles"
既然你已經做了決定,你會想確保自己在經銷商那裡拿到合理的價格。由於你買的是中古車,價格取決於車齡。粗略估算的話,假設車輛車齡不到 3 年,價格是全新時原價的 80%。如果車齡至少 10 年,價格是 50%。如果車齡至少 3 年但不超過 10 年,價格是原價的 70%。
請實作 calculateResellPrice(originalPrice:yearsOld:) 函式,它接受引數 originalPrice(車輛的原價)與 yearsOld(車輛的車齡,以年為單位)。這個函式應回傳車輛的轉售價格。
calculateResellPrice(originalPrice: 1000, yearsOld: 1)
// returns 800
calculateResellPrice(originalPrice: 1000, yearsOld: 5)
// returns 700
calculateResellPrice(originalPrice: 1000, yearsOld: 15)
// returns 500