ProgramGuru

Variables and constants

Variables and constants

A student’s name may stay fixed while a corrected score changes. Swift separates these intentions: let declares a binding you cannot reassign, while var allows reassignment. Choosing let when a value does not need to change makes that constraint visible to readers and the compiler.

What you’ll learn

  • Choose let or var from whether reassignment is needed.
  • Trace a compound update without changing the variable’s type.

Variables and constants in the example

  1. Initial score 72

    A student’s name may stay fixed while a corrected score changes.

    Initial score 72
  2. Apply +8

    The equals sign assigns a value; it does not ask whether two values are equal.

    Apply +8
  3. Current score 80

    The first score is 72.

    Current score 80
The first score is 72. The compound assignment adds eight and stores 80 back in score. print reads the current value. The student binding is never reassigned, so let expresses the intended stability.

How variables and constants works

The equals sign assigns a value; it does not ask whether two values are equal. Swift infers types from initial values when it can. A variable may change its value but still keeps its type. Reassigning an Int variable to text is a type error, not a conversion.

Work through variables and constants

Save this complete program as main.swift and run it with the Swift toolchain. No separate input file is needed.

Swift · complete program

let student = "Diya"
var score = 72
score += 8
print(student, score)

Expected output

Diya 80

Follow the execution

The first score is 72. The compound assignment adds eight and stores 80 back in score. print reads the current value. The student binding is never reassigned, so let expresses the intended stability.

Try this program in the Swift compiler. Paste the complete source above.

Practice: Variables and constants

Your task: What declaration must change if the program needs to correct a score declared with let?

Swift · complete program

let student = "Diya"
var score = 72
score += 8
print(student, score)

Worked answer

Declare that score with var before assigning a new value. Do not change every binding to var; keep names and other unchanged values constant where appropriate.

Choose what may change

A let binding to a class instance prevents rebinding the reference, not every possible mutation of the object. We will distinguish that from value-type mutation when studying structures and classes.

Check your decision

What declaration must change if the program needs to correct a score declared with let?
Choose one answer. You can try again.

Answer and explanation

Declare that score with var before assigning a new value.

Declare that score with var before assigning a new value. Do not change every binding to var; keep names and other unchanged values constant where appropriate.

Choose what may change

Declare that score with var before assigning a new value. Do not change every binding to var; keep names and other unchanged values constant where appropriate. A let binding to a class instance prevents rebinding the reference, not every possible mutation of the object.

Further reading: official Swift language guide.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Data types and safe conversion →