Bird
Raised Fist0
Swiftprogramming~10 mins

Let for constants (immutable) in Swift - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Let for constants (immutable)
Declare constant with let
↓
Assign value once
↓
Use constant in code
↓
Attempt to change value?
Yes→Error: Cannot modify constant
No↓
Program continues safely
This flow shows how a constant is declared once with let, assigned a value, used, and cannot be changed later.
Execution Sample
Swift
let pi = 3.14
print(pi)
// pi = 3.14159 // Error: cannot assign to let constant
Declare a constant pi with value 3.14, print it, then show that changing pi causes an error.
Execution Table
StepActionCode LineResultNotes
1Declare constant pilet pi = 3.14pi = 3.14pi is set once and cannot change
2Print piprint(pi)Output: 3.14Prints the value of pi
3Attempt to change pi// pi = 3.14159ErrorCannot assign to constant pi
💡 Execution stops at error because pi is immutable and cannot be reassigned
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
piundefined3.143.14Error on reassignment
Key Moments - 2 Insights
Why can't we change the value of pi after declaring it with let?
Because let creates a constant that is immutable. The execution_table row 3 shows an error when trying to assign a new value.
What happens if we try to assign a value to pi before declaring it?
Swift requires constants to be declared and initialized before use. The execution_table row 1 shows pi is assigned at declaration.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of pi after step 1?
A3.14
Bundefined
CError
D3.14159
💡 Hint
Check the 'Result' column in row 1 of execution_table
At which step does the program produce an error?
AStep 1
BStep 2
CStep 3
DNo error
💡 Hint
Look at the 'Result' column in execution_table row 3
If we replace let with var, what would happen at step 3?
AError still occurs
BValue of pi changes successfully
CProgram crashes
Dpi becomes undefined
💡 Hint
Constants (let) are immutable, but variables (var) can be changed
Concept Snapshot
let constantName = value
- Declares an immutable constant
- Value assigned once, cannot change
- Trying to reassign causes error
- Use for values that must stay fixed
Full Transcript
In Swift, 'let' declares a constant that cannot be changed after it is set. First, you declare the constant with 'let' and assign a value. Then you can use it safely in your code. If you try to change the value later, Swift gives an error. This helps keep important values fixed and prevents accidental changes. The example shows declaring pi as 3.14, printing it, and then an error when trying to change it. Remember, use 'let' for values that should never change.

Practice

(1/5)
1. What does the let keyword do in Swift?
easy
A. Defines a function
B. Creates a variable that can be changed anytime
C. Creates a constant value that cannot be changed
D. Declares a class

Solution

  1. Step 1: Understand the purpose of let

    The let keyword is used to declare constants in Swift, meaning the value assigned cannot be changed later.
  2. Step 2: Compare with other keywords

    Unlike var, which declares variables that can change, let ensures immutability.
  3. Final Answer:

    Creates a constant value that cannot be changed -> Option C
  4. Quick Check:

    let means constant = A [OK]
Hint: Remember: let = fixed value, var = changeable value [OK]
Common Mistakes:
  • Confusing let with var
  • Thinking let declares variables
  • Assuming let can be reassigned
2. Which of the following is the correct way to declare a constant integer named age with value 30 in Swift?
easy
A. let age = 30
B. var age = 30
C. constant age = 30
D. const age = 30

Solution

  1. Step 1: Identify correct Swift syntax for constants

    In Swift, let is used to declare constants, so let age = 30 is correct.
  2. Step 2: Check other options

    var declares a variable, not a constant. constant and const are not valid Swift keywords.
  3. Final Answer:

    let age = 30 -> Option A
  4. Quick Check:

    Use let for constants = B [OK]
Hint: Use let for constants, var for variables [OK]
Common Mistakes:
  • Using var instead of let for constants
  • Using non-Swift keywords like const
  • Syntax errors with declaration
3. What will be the output of this Swift code?
let number = 10
// number = 20
print(number)
medium
A. 10
B. Compile-time error
C. 20
D. Runtime error

Solution

  1. Step 1: Understand the code behavior

    The constant number is set to 10. The line trying to change it to 20 is commented out, so it does not run.
  2. Step 2: Determine the printed value

    Since number remains 10, print(number) outputs 10.
  3. Final Answer:

    10 -> Option A
  4. Quick Check:

    Constant unchanged prints original value = 10 [OK]
Hint: Commented code does not run; constant stays same [OK]
Common Mistakes:
  • Thinking commented code runs
  • Expecting reassignment to work
  • Confusing output with attempted change
4. Identify the error in this Swift code:
let pi = 3.14
pi = 3.14159
print(pi)
medium
A. No error, prints 3.14159
B. Compile-time error: cannot assign to value: 'pi' is a 'let' constant
C. Runtime error: variable not initialized
D. Syntax error: missing semicolon

Solution

  1. Step 1: Check reassignment of constant

    The code tries to assign a new value to pi, which was declared with let and is immutable.
  2. Step 2: Understand Swift's error

    Swift does not allow changing a constant's value, so this causes a compile-time error.
  3. Final Answer:

    Compile-time error: cannot assign to value: 'pi' is a 'let' constant -> Option B
  4. Quick Check:

    Reassigning let constant causes compile error = D [OK]
Hint: Constants cannot be changed after assignment [OK]
Common Mistakes:
  • Trying to reassign let constants
  • Expecting runtime error instead of compile error
  • Ignoring error messages
5. You want to store the number of days in a week as a constant in Swift. Which code correctly declares this and prevents accidental changes later?
hard
A. let daysInWeek // Assign value later
B. var daysInWeek = 7 // Later changed to 8 accidentally
C. constant daysInWeek = 7 // Prevents changes
D. let daysInWeek = 7 // daysInWeek cannot be changed later

Solution

  1. Step 1: Choose correct keyword for constant

    Using let declares a constant that cannot be changed after assignment.
  2. Step 2: Check code correctness

    let daysInWeek = 7 // daysInWeek cannot be changed later declares and assigns daysInWeek with 7 and prevents changes later. var daysInWeek = 7 // Later changed to 8 accidentally uses var which allows changes. constant daysInWeek = 7 // Prevents changes uses invalid keyword. let daysInWeek // Assign value later declares without assignment, which is invalid for constants.
  3. Final Answer:

    let daysInWeek = 7 // daysInWeek cannot be changed later -> Option D
  4. Quick Check:

    Use let with assignment to make constant = A [OK]
Hint: Use let with immediate value to make constant [OK]
Common Mistakes:
  • Using var instead of let for constants
  • Trying to declare let without initial value
  • Using non-Swift keywords like constant
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard