比較演算子は、同じ型の2つの値を比較し、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かに応じて、2つの値のどちらかを返します。
この演算子は3つの部分を使います:condition ? trueExpression : falseExpression。
conditionがtrueに評価されればtrueExpressionが評価されて返され、そうでなければfalseExpressionが評価されて返されます。
三項演算子は、シンプルな1行の選択に最適です。
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
}
この演習では、乗り物の購入に備えるためのコードを書きます。
タスクは3つあります。1つは、2つの乗り物のどちらを選ぶべきかを判断するのに必要かどうかを確かめるもの、1つは、どの免許が必要かを判断するもの、もう1つは、中古の乗り物の妥当な価格を見積もるものです。
町の自動車販売店はどこも、5年間・金利0%のキャンペーンを行っています。ぜひ利用したいところですが、欲しい車の月々の支払いを払えるかどうかはわかりません。
月々の支払いは、車の総額を5年間の月数で割った金額です。
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を取ります。originalPriceは乗り物の元の価格、yearsOldは乗り物の年齢(年数)です。
この関数は、乗り物の再販価格を返します。
calculateResellPrice(originalPrice: 1000, yearsOld: 1)
// returns 800
calculateResellPrice(originalPrice: 1000, yearsOld: 5)
// returns 700
calculateResellPrice(originalPrice: 1000, yearsOld: 15)
// returns 500