Bird
Raised Fist0
Swiftprogramming~10 mins

Swift REPL and Playgrounds - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Swift REPL and Playgrounds
Start Swift REPL or Playground
↓
Write Swift code line or block
↓
Code is parsed and compiled
↓
Code is executed immediately
↓
Output or result shown
↓
Modify code and repeat
↓
End
You start Swift REPL or Playground, write code, it runs immediately, shows output, then you can change code and run again.
Execution Sample
Swift
let greeting = "Hello"
print(greeting)
var count = 1
count += 1
print(count)
This code defines a greeting, prints it, updates a count, and prints the updated count.
Execution Table
StepActionCode LineVariable StateOutput
1Declare constant greetinglet greeting = "Hello"greeting = "Hello"
2Print greetingprint(greeting)greeting = "Hello"Hello
3Declare variable countvar count = 1greeting = "Hello", count = 1
4Increment countcount += 1greeting = "Hello", count = 2
5Print countprint(count)greeting = "Hello", count = 22
💡 All code lines executed sequentially with immediate output after print statements.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 4Final
greetingundefined"Hello""Hello""Hello""Hello"
countundefinedundefined122
Key Moments - 3 Insights
Why does the output appear immediately after print statements?
Because Swift REPL and Playgrounds execute code line-by-line and show output right after print commands, as seen in steps 2 and 5 in the execution_table.
Can I change a variable after declaring it with var?
Yes, variables declared with var can be changed anytime, like count was updated from 1 to 2 at step 4 in the execution_table.
Why can't I change a constant declared with let?
Constants declared with let cannot be changed after assignment, so greeting stays "Hello" throughout, as shown in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of count after step 4?
Aundefined
B1
C2
D0
💡 Hint
Check the 'Variable State' column at step 4 in the execution_table.
At which step does the program print the greeting?
AStep 2
BStep 1
CStep 3
DStep 5
💡 Hint
Look for the print(greeting) action in the execution_table.
If you change greeting to a variable (var) and update it, what changes in the variable_tracker?
Acount would change instead
Bgreeting value would update after reassignment
Cgreeting would stay constant
DNo variables would change
💡 Hint
Variable_tracker shows constant greeting value because of let; changing to var allows updates.
Concept Snapshot
Swift REPL and Playgrounds let you write Swift code and see results immediately.
Use let for constants and var for variables.
Code runs line-by-line, showing output after print statements.
Modify code anytime and re-run to see changes.
Great for learning and quick testing.
Full Transcript
Swift REPL and Playgrounds are tools where you write Swift code and it runs immediately, showing output right away. You can declare constants with let and variables with var. Variables can change values, constants cannot. When you use print, the output appears immediately. You can change your code and run it again to see new results. This makes learning and testing Swift easy and interactive.

Practice

(1/5)
1. What is the main purpose of Swift Playgrounds?
easy
A. To provide an interactive environment to write and test Swift code easily
B. To compile Swift code into an executable file
C. To manage Swift project dependencies
D. To deploy Swift apps to the App Store

Solution

  1. Step 1: Understand what Playgrounds offer

    Swift Playgrounds allow users to write and run Swift code interactively with immediate feedback.
  2. Step 2: Compare options with Playgrounds' purpose

    Options B, C, and D describe other development tasks, not the main purpose of Playgrounds.
  3. Final Answer:

    To provide an interactive environment to write and test Swift code easily -> Option A
  4. Quick Check:

    Playgrounds = Interactive coding environment [OK]
Hint: Playgrounds = interactive coding + instant results [OK]
Common Mistakes:
  • Confusing Playgrounds with full app deployment tools
  • Thinking Playgrounds compile apps for release
  • Mixing Playgrounds with dependency managers
2. Which of the following is the correct way to start the Swift REPL in a terminal?
easy
A. swift run
B. swift
C. swift repl
D. swift playground

Solution

  1. Step 1: Recall the command to start Swift REPL

    Typing swift alone in the terminal starts the Swift REPL environment.
  2. Step 2: Check other options

    swift run runs a Swift package, swift repl is not a valid command, and swift playground does not start REPL.
  3. Final Answer:

    swift -> Option B
  4. Quick Check:

    Start REPL with 'swift' command [OK]
Hint: Just type 'swift' to open REPL in terminal [OK]
Common Mistakes:
  • Using 'swift run' which runs packages, not REPL
  • Typing 'swift repl' which is invalid
  • Confusing REPL with Playgrounds commands
3. What will be the output when you run this code in Swift REPL?
let x = 5
let y = 10
x + y
medium
A. 15
B. x + y
C. SyntaxError
D. 10

Solution

  1. Step 1: Evaluate the variables and expression

    Variables x and y are assigned 5 and 10 respectively. The expression x + y sums them.
  2. Step 2: Calculate the sum

    5 + 10 equals 15, so the REPL outputs 15.
  3. Final Answer:

    15 -> Option A
  4. Quick Check:

    5 + 10 = 15 [OK]
Hint: Add values step-by-step to find output [OK]
Common Mistakes:
  • Expecting the expression to print the code itself
  • Thinking REPL shows errors for valid expressions
  • Confusing variable values
4. You wrote this code in a Swift Playground but it shows an error:
var greeting = "Hello"
greeting += 5
What is the problem?
medium
A. You must use '==' instead of '+='
B. Variable 'greeting' should be a constant
C. You cannot add an integer to a string directly
D. Strings cannot be modified in Playgrounds

Solution

  1. Step 1: Analyze the code operation

    The code tries to add an integer (5) to a string variable using '+='.
  2. Step 2: Understand Swift type rules

    Swift does not allow adding an Int directly to a String; types must match or be converted.
  3. Final Answer:

    You cannot add an integer to a string directly -> Option C
  4. Quick Check:

    String + Int without conversion causes error [OK]
Hint: Check data types before combining values [OK]
Common Mistakes:
  • Thinking '+=' works with different types
  • Assuming strings are immutable in Playgrounds
  • Confusing assignment operators
5. You want to quickly test a function that calculates the square of a number using Swift Playgrounds. Which approach is best?
hard
A. Write the function in a text editor and run it later in the terminal
B. Write the function in a Swift file and compile it with Xcode without running
C. Use Swift REPL but avoid Playgrounds because it can't run functions
D. Write the function in Playgrounds and call it with test values to see results immediately

Solution

  1. Step 1: Understand Playgrounds' interactive nature

    Playgrounds allow writing functions and immediately running them with test inputs to see results.
  2. Step 2: Evaluate other options

    Write the function in a Swift file and compile it with Xcode without running compiles but does not run immediately; Use Swift REPL but avoid Playgrounds because it can't run functions is false because Playgrounds can run functions; Write the function in a text editor and run it later in the terminal delays testing.
  3. Final Answer:

    Write the function in Playgrounds and call it with test values to see results immediately -> Option D
  4. Quick Check:

    Playgrounds = quick function testing [OK]
Hint: Use Playgrounds for instant function testing [OK]
Common Mistakes:
  • Thinking Playgrounds can't run functions
  • Confusing compiling with running
  • Delaying testing by using external editors
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