Learn KOTLIN-PLAYGROUND with Real Code Examples
Updated Nov 26, 2025
Code Sample Descriptions
1
Hello World in Kotlin Playground
fun main() {
println("Hello World")
}
A simple Hello World example in Kotlin Playground.
2
Simple Addition in Kotlin Playground
fun main() {
val a = 5
val b = 10
println("Sum: ${a + b}")
}
Adds two numbers and prints the result.
3
Kotlin Playground If-Else Example
fun main() {
val num = -3
if(num >= 0) {
println("Positive")
} else {
println("Negative")
}
}
A simple if-else example to check if a number is positive or negative.
4
Kotlin Playground For Loop Example
fun main() {
for(i in 1..5) {
println(i)
}
}
Prints numbers from 1 to 5 using a for loop.
5
Kotlin Playground While Loop Example
fun main() {
var i = 1
while(i <= 5) {
println(i)
i++
}
}
Prints numbers from 1 to 5 using a while loop.
6
Kotlin Playground Function Example
fun greet(name: String) {
println("Hello, $name")
}
fun main() {
greet("Alice")
}
Defines a function to greet a user by name.
7
Kotlin Playground Array Example
fun main() {
val numbers = arrayOf(1, 2, 3, 4, 5)
for(num in numbers) {
println(num)
}
}
Iterates over an array of integers and prints them.
8
Kotlin Playground List Example
fun main() {
val fruits = listOf("Apple", "Banana", "Cherry")
for(fruit in fruits) {
println(fruit)
}
}
Uses a list of strings and prints each element.
9
Kotlin Playground When Expression Example
fun main() {
val num = 2
when(num) {
1 -> println("One")
2 -> println("Two")
else -> println("Other")
}
}
Uses a when expression to print messages based on a number.
10
Kotlin Playground Nullable Example
fun main() {
val name: String? = null
println(name?.length ?: "Name is null")
}
Demonstrates nullable types and safe calls.