Step 1 of 5 · Reading · ~3 min
Learn
Getting Started
let and var
Swift has exactly two ways to name a value, and choosing between them is the first design decision you make in every function you write.
let | var | |
|---|---|---|
| Reassign later? | no | yes |
| Compiler's opinion | the default | warns if you never reassign |
| Reads as | "this is settled" | "this changes" |
let name = "Alice"
var count = 0
count = count + 1
count += 1
print(name, count)
//> Alice 2
Try to reassign the let and the program does not build:
✗ Does not compile
let name = "Alice"
name = "Bob" // cannot assign to value: 'name' is a 'let' constant
✓ Compiles
var name = "Alice"
name = "Bob"
This is a compile error, not a crash. The mistake is caught before the program ever runs, which is the whole point.
Why default to let?
Because a name that never changes is one fewer thing to track while reading. When you see
let total = ... you know that every later line sees the same total; with var you have to
scan the rest of the function to be sure. Swift leans on this hard enough that it emits a
warning when a var is never reassigned, nudging you back to let.
Start with let. Change it to var the moment the compiler tells you that you have to.
Types: inferred, but real
Every one of these names has a fixed type from the moment it is created.
| Type | Example literal | Notes |
|---|---|---|
Int | 42 | whole numbers, signed, 64-bit here |
Double | 3.5 | the default for decimals |
Bool | true | only true / false, never 0 or 1 |
String | "hi" | full Unicode text |
Character | "a" | one character; needs an explicit type |
Type names start with a capital letter. Write the type yourself when the literal is ambiguous or when you want to be explicit:
let age: Int = 30
let ratio: Double = 3 // 3 stored as 3.0
let initial: Character = "A"
print(age, ratio, initial)
//> 30 3.0 A
Printing values inside text
Gluing values into a sentence uses string interpolation — a backslash, then the expression in parentheses:
let name = "Alice"
let age = 30
print("\(name) is \(age) years old")
print("Next year: \(age + 1)")
//> Alice is 30 years old
//> Next year: 31
Anything that produces a value can go inside the parentheses, including arithmetic. This is the form you will use for nearly every line of output in this course.
No silent number conversion
Swift will not quietly turn an Int into a Double for you, not even when it is obviously safe.
✗ Does not compile
let count = 3
let price = 2.5
let total = count * price // Int and Double do not mix
✓ Compiles
let count = 3
let price = 2.5
let total = Double(count) * price
print(total)
//> 7.5
Double(count) looks like a function call because it is one — an initialiser that builds a
Double from an Int. Nearly every conversion in Swift has this shape.
Your exercise
Read two integers off separate lines and print their sum. The starter already does the reading; you add the printing.
The mistake the grader catches is printing a Double. If you convert either input — say
print(Double(a) + Double(b)) — the output becomes 12.0 where the test wants 12, and the
hidden test with -3 and 3 becomes 0.0 instead of 0. Both inputs are already Int, so
a + b is an Int; leave it that way. Printing anything around the number, like Sum: 12,
fails for the same reason.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…