Swift, lesson 4
So far every program has run the same lines every time. Real programs react to their input: a game checks whether you won, a shop checks whether you get free shipping. That takes conditions.
BoolA comparison produces a Bool, either true or false:
| Operator | Meaning |
|---|---|
== | equal to |
!= | not equal to |
< > |
2 exercises to try what you just read.
| less than, greater than |
<= >= | at most, at least |
Note the double == for "equal". A single = assigns a value, and Swift refuses to compile
if total = 10 because assignment does not produce a Bool. That small rule catches a classic bug.
if, else if, elseSwift checks each condition from the top and runs the first block whose condition is true,
then skips the rest. The braces are always required, even for a single line, and the condition
does not need parentheses. The condition must be a real Bool: if count { ... } with an Int
is a compile error, so you write if count > 0.
a && b is true only when both are true ("and").a || b is true when at least one is true ("or").!a flips a Bool ("not").Swift stops evaluating as soon as it knows the answer. If the left side of && is false, the
right side is never checked. Use parentheses whenever mixing && and ||; they make the intent
obvious to readers.
if as a valueAn if can also produce a value directly, as long as every branch gives the same type:
For a quick two-way choice you can also use the ternary operator
condition ? valueIfTrue : valueIfFalse, but keep it to short, simple cases.
switchWhen you compare one value against many possibilities, switch is tidier than a long chain of
else if. Each case can list single values, several values, or a range:
Three rules make Swift's switch safer than in many languages:
default.break
at the end of a case.break if a case should do nothing.Ranges come in two shapes: a...b includes both ends, and a..<b stops just before b.
Read a visitor's age and print their ticket type and price. Use a switch with ranges.
| Age | Output |
|---|---|
| 0 to 4 | Free |
| 5 to 17 | Child: $8 |
| 18 to 64 | Adult: $15 |
| 65 and up | Senior: $10 |
Input: one line with a whole number, the age (0 or more).
Output: one line from the table.
Sample input:
Sample output:
A year is a leap year if it is divisible by 4, except years divisible by 100 are not, unless they are also divisible by 400. So 2024 and 2000 are leap years, but 1900 and 2026 are not.
Input: one line with a year (a positive whole number).
Output: Leap year or Common year.
Sample input:
Sample output:
Try writing the whole rule as one condition using %, && and ||.
let temperature = 31
if temperature > 30 {
print("Hot! Drink water.")
} else if temperature < 5 {
print("Cold. Grab a coat.")
} else {
print("Nice weather.")
}let age = 15
let withAdult = true
if age >= 13 && (age < 16 || withAdult) {
print("You can watch the film.")
}let label = if temperature > 25 { "warm" } else { "cool" }let hour = 14
switch hour {
case 0..<12:
print("Good morning")
case 12, 13:
print("Lunch time")
case 14...17:
print("Good afternoon")
default:
print("Good evening")
}12Child: $82024Leap year