Kotlin Tour

joow
948 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Control structures

If

https://kotlinlang.org/docs/reference/control-flow.html#if-expression

The classic if is revised, it is an expression :

Declare an immutable string variable holding "head" or "tail" depending on the toss
1
val toss: String = TODO()
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

When

https://kotlinlang.org/docs/reference/control-flow.html#when-expression

Kotlin doesn't have a switch keyword, it replaces it with a powerful when expression, similar to pattern matching :

Declare a function fizzbuzz solving the infamous algorithm
1
fun fizzbuzz(n: Int): String = TODO()
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

For

https://kotlinlang.org/docs/reference/control-flow.html#for-loops

For loops are implemented as foreach loops, using in keyword :

Iterate over the list of languages to print them
1
2
3
val languages = arrayOf("Java", "JavaScript", "Go", "Kotlin")
fun printLanguages(): Unit = TODO()
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Ranges

https://kotlinlang.org/docs/reference/ranges.html

Using rangeTo functions ranges can be defined for any comparable type. The .. operator is used to define ranges, in and !in to check if a value is included in a range or to iterate over a range (for integral type). downTo, step and until can be used to revert the range, change the progression step or exclude the last value.

Display the ten first numbers, a countdown from 10 to 0 and the ten first even numbers
1
2
3
4
5
fun tenFirstNumber(): Unit = TODO()
fun countdown() : Unit = TODO()
fun tenFirstEvenNumbers(): Unit = TODO()
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content