比较运算符会比较两个相同类型的值,并返回一个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
}
在这个练习中,你将编写一些代码,帮助你为购买一辆车做准备。
你有三个任务:确定自己能否负担得起月供,确定你需要哪种驾照,以及估算一辆二手车的合理价格。
你所在城镇的汽车经销商都在搞五年期、0% 利率的促销活动,你很想借此机会买车。但你不确定自己能否负担得起心仪车辆的月供。
月供就是车辆总价除以五年期内的月数。
实现 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