Kotlin `When` Statement Example
20 Comments
Aug 20th 2012
In this tutorial you will learn about Whenstatement via simple step by step examples.
Example 1: When
Let us look at a simple When statement example written in Kotlin Programming Language. Follow the following steps:
Step 1: Create Project
- Open your favorite Kotlin IDE.
- In the menu go to
File --> Create New Project.
Step 2: Add Dependencies
No dependencies are needed for this project.
Step 3: Write Code
Our code will comprise the following Kotlin files:
When.kt
- In your editor or IDE, create a file known as
When.kt. - Then add the following code:
(a). When.kt
package When
fun main(args : Array<String>) {
var x = 101
val greater = { x : Int -> x > 100 }
//This is one hell of a flexible switch statement
when {
x in 1..50 -> print("In range")
greater(x) -> print("Great")
x == 50 -> print("Exact match")
else -> print("Outside range")
}
}
Step 4: Run
Copy the code, build and run.
Reference
Here are the reference links:
| Number | Link |
|---|---|
| 1. | Download Example |
| 2. | Follow code author |
| 3. | Code: Apache 2.0 License |

John
9:41 PM on August 24, 2013
I don't believe in astrology but still your writing style is really great!
John
9:41 PM on August 24, 2013
I don't believe in astrology but still your writing style is really great!