0
0
Kotlinprogramming~10 mins

Kotlin REPL and script mode - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Kotlin REPL and script mode
Start Kotlin REPL
Type Kotlin code line
REPL evaluates code
Output result shown
Type next line or exit
Back to Type Kotlin code line
Start Kotlin Script
Write Kotlin code in file
Run script with kotlin command
Script executes top to bottom
Output shown in console
Script ends
Back to Type Kotlin code line
Kotlin REPL lets you type and run code line by line interactively. Script mode runs a whole Kotlin file from top to bottom.
Execution Sample
Kotlin
>>> val x = 5
>>> println(x * 2)
In REPL, define x as 5, then print x times 2, outputting 10.
Execution Table
StepInputActionOutputState
1val x = 5Declare variable x with value 5No outputx=5
2println(x * 2)Calculate x*2 and print10x=5
3exitExit REPL sessionSession endsx=5
💡 User types exit to end REPL session
Variable Tracker
VariableStartAfter Step 1After Step 2Final
xundefined555
Key Moments - 2 Insights
Why does REPL show no output after declaring a variable?
Because variable declarations do not produce output themselves, only expressions like println() show output, as seen in step 1 and 2 of the execution_table.
How does script mode differ from REPL in running code?
Script mode runs the whole file top to bottom at once, unlike REPL which runs line by line interactively, as shown in the concept_flow diagram.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of x after step 1?
A10
B5
Cundefined
D0
💡 Hint
Check the State column in row for step 1 in execution_table.
At which step does the REPL print output?
AStep 2
BStep 1
CStep 3
DNo output at all
💡 Hint
Look at the Output column in execution_table rows.
If you run a Kotlin script file, how is the code executed?
ALine by line interactively
BOnly the last line runs
CTop to bottom all at once
DCode does not run automatically
💡 Hint
Refer to the concept_flow description about script mode.
Concept Snapshot
Kotlin REPL lets you type and run code line by line interactively.
Variable declarations do not print output.
Use println() to see output.
Kotlin script mode runs a whole file top to bottom.
Run scripts with 'kotlin filename.kts'.
REPL is good for quick tests; scripts for full programs.
Full Transcript
This visual execution shows how Kotlin REPL and script mode work. In REPL, you start by typing code line by line. For example, declaring a variable x with value 5 does not print anything immediately. When you run println(x * 2), it calculates 10 and prints it. You can continue typing or exit the REPL. In script mode, you write Kotlin code in a file and run it all at once. The script executes from top to bottom and shows output in the console. This helps beginners see how interactive REPL differs from running a full script file.