0
0
Kotlinprogramming~10 mins

Print and println output in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Print and println output
Start Program
Call print()
Output text without newline
Call println()
Output text with newline
End Program
The program starts, prints text without moving to a new line, then prints text with a newline, and ends.
Execution Sample
Kotlin
fun main() {
    print("Hello, ")
    println("world!")
}
Prints 'Hello, ' without newline, then prints 'world!' with newline.
Execution Table
StepFunction CalledOutputCursor Position After Output
1print("Hello, ")Hello, After the comma and space, cursor stays on same line
2println("world!")world! Cursor moves to start of next line
💡 Program ends after printing both outputs; cursor is at start of new line.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
Output Stream"""Hello, ""Hello, world!\n""Hello, world!\n"
Key Moments - 2 Insights
Why does print() not move to a new line after output?
Because print() outputs text exactly as given without adding a newline, as shown in step 1 of the execution_table where the cursor stays on the same line.
What does println() do differently from print()?
println() outputs the text and then adds a newline character, moving the cursor to the next line, as seen in step 2 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the cursor position after step 1?
AAfter the printed text on the same line
BAt the start of the next line
CAt the beginning of the output
DCursor position is unchanged
💡 Hint
Check the 'Cursor Position After Output' column for step 1 in the execution_table.
At which step does the output move to a new line?
AStep 1
BStep 2
CBoth steps
DNeither step
💡 Hint
Look at the 'Output' and 'Cursor Position' columns in the execution_table.
If we replace print() with println() in step 1, what changes in the output?
AOutput stays on the same line
BNo output is printed
COutput moves to a new line after step 1
DOutput is printed twice
💡 Hint
Recall that println() adds a newline after printing, as shown in step 2.
Concept Snapshot
print(text): outputs text without moving to a new line
println(text): outputs text and moves cursor to next line
Use print() to continue on same line
Use println() to start new line after output
Full Transcript
This example shows how Kotlin's print() and println() functions work. First, print("Hello, ") outputs text without moving to a new line, so the cursor stays right after the comma and space. Then, println("world!") outputs text and moves the cursor to the next line by adding a newline character. This difference is important when formatting output. The execution table tracks each step's output and cursor position. The variable tracker shows how the output stream builds up. Remember, print() keeps output on the same line, println() moves to a new line after printing.