0
0
Swiftprogramming~10 mins

Swift REPL and Playgrounds - Step-by-Step Execution

Choose your learning style9 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.