Control flow
Hello world
Basic types
Collections
Control flow
Functions
Classes
Null safety
Like other programming languages, Kotlin is capable of making decisions based on whether a piece of code is evaluated to be true. Such pieces of code are called conditional expressions. Kotlin is also able to create and iterate through loops.
Kotlin provides if
and when
for checking conditional expressions.
note
If you have to choose between
if
andwhen
, we recommend usingwhen
as it leads to more robust and safer programs.
To use if
, add the conditional expression within parentheses ()
and the action to take if the result is true within curly braces {}
:
fun main() { //sampleStart val d: Int val check = true if (check) { d = 1 } else { d = 2 } println(d) // 1 //sampleEnd }
xxxxxxxxxx
val d: Int
val check = true
if (check) {
d = 1
} else {
d = 2
}
println(d)
// 1
There is no ternary operator condition ? then : else
in Kotlin. Instead, if
can be used as an expression. If there is only one line of code per action, the curly braces {}
are optional:
fun main() { //sampleStart val a = 1 val b = 2 println(if (a > b) a else b) // Returns a value: 2 //sampleEnd }
xxxxxxxxxx
val a = 1
val b = 2
println(if (a > b) a else b) // Returns a value: 2
Use when
when you have a conditional expression with multiple branches. when
can be used either as a statement or as an expression.
Here is an example of using when
as a statement:
Place the conditional expression within parentheses
()
and the actions to take within curly braces{}
.Use
->
in each branch to separate each condition from each action.
fun main() { //sampleStart val obj = "Hello" when (obj) { // Checks whether obj equals to "1" "1" -> println("One") // Checks whether obj equals to "Hello" "Hello" -> println("Greeting") // Default statement else -> println("Unknown") } // Greeting //sampleEnd }
xxxxxxxxxx
val obj = "Hello"
when (obj) {
// Checks whether obj equals to "1"
"1" -> println("One")
// Checks whether obj equals to "Hello"
"Hello" -> println("Greeting")
// Default statement
else -> println("Unknown")
}
// Greeting
note
Note that all branch conditions are checked sequentially until one of them is satisfied. So only the first suitable branch is executed.
Here is an example of using when
as an expression. The when
syntax is assigned immediately to a variable:
fun main() { //sampleStart val obj = "Hello" val result = when (obj) { // If obj equals "1", sets result to "one" "1" -> "One" // If obj equals "Hello", sets result to "Greeting" "Hello" -> "Greeting" // Sets result to "Unknown" if no previous condition is satisfied else -> "Unknown" } println(result) // Greeting //sampleEnd }
xxxxxxxxxx
val obj = "Hello"
val result = when (obj) {
// If obj equals "1", sets result to "one"
"1" -> "One"
// If obj equals "Hello", sets result to "Greeting"
"Hello" -> "Greeting"
// Sets result to "Unknown" if no previous condition is satisfied
else -> "Unknown"
}
println(result)
// Greeting
If when
is used as an expression, the else branch is mandatory, unless the compiler can detect that all possible cases are covered by the branch conditions.
The previous example showed that when
is useful for matching a variable. when
is also useful when you need to check a chain of Boolean expressions:
fun main() { //sampleStart val temp = 18 val description = when { // If temp < 0 is true, sets description to "very cold" temp < 0 -> "very cold" // If temp < 10 is true, sets description to "a bit cold" temp < 10 -> "a bit cold" // If temp < 20 is true, sets description to "warm" temp < 20 -> "warm" // Sets description to "hot" if no previous condition is satisfied else -> "hot" } println(description) // warm //sampleEnd }
xxxxxxxxxx
val temp = 18
val description = when {
// If temp < 0 is true, sets description to "very cold"
temp < 0 -> "very cold"
// If temp < 10 is true, sets description to "a bit cold"
temp < 10 -> "a bit cold"
// If temp < 20 is true, sets description to "warm"
temp < 20 -> "warm"
// Sets description to "hot" if no previous condition is satisfied
else -> "hot"
}
println(description)
// warm
Before talking about loops, it's useful to know how to construct ranges for loops to iterate over.
The most common way to create a range in Kotlin is to use the ..
operator. For example, 1..4
is equivalent to 1, 2, 3, 4
.
To declare a range that doesn't include the end value, use the ..<
operator. For example, 1..<4
is equivalent to 1, 2, 3
.
To declare a range in reverse order, use downTo.
For example, 4 downTo 1
is equivalent to 4, 3, 2, 1
.
To declare a range that increments in a step that isn't 1, use step
and your desired increment value. For example, 1..5 step 2
is equivalent to 1, 3, 5
.
You can also do the same with Char
ranges:
'a'..'d'
is equivalent to'a', 'b', 'c', 'd'
'z' downTo 's' step 2
is equivalent to'z', 'x', 'v', 't'
The two most common loop structures in programming are for
and while
. Use for
to iterate over a range of values and perform an action. Use while
to continue an action until a particular condition is satisfied.
Using your new knowledge of ranges, you can create a for
loop that iterates over numbers 1 to 5 and prints the number each time.
Place the iterator and range within parentheses ()
with keyword in
. Add the action you want to complete within curly braces {}
:
fun main() { //sampleStart for (number in 1..5) { // number is the iterator and 1..5 is the range print(number) } // 12345 //sampleEnd }
xxxxxxxxxx
for (number in 1..5) {
// number is the iterator and 1..5 is the range
print(number)
}
// 12345
Collections can also be iterated over by loops:
fun main() { //sampleStart val cakes = listOf("carrot", "cheese", "chocolate") for (cake in cakes) { println("Yummy, it's a $cake cake!") } // Yummy, it's a carrot cake! // Yummy, it's a cheese cake! // Yummy, it's a chocolate cake! //sampleEnd }
xxxxxxxxxx
val cakes = listOf("carrot", "cheese", "chocolate")
for (cake in cakes) {
println("Yummy, it's a $cake cake!")
}
// Yummy, it's a carrot cake!
// Yummy, it's a cheese cake!
// Yummy, it's a chocolate cake!
while
can be used in two ways:
To execute a code block while a conditional expression is true. (
while
)To execute the code block first and then check the conditional expression. (
do-while
)
In the first use case (while
):
Declare the conditional expression for your while loop to continue within parentheses
()
.Add the action you want to complete within curly braces
{}
.
note
The following examples use the increment operator
++
to increment the value of thecakesEaten
variable.
fun main() { //sampleStart var cakesEaten = 0 while (cakesEaten < 3) { println("Eat a cake") cakesEaten++ } // Eat a cake // Eat a cake // Eat a cake //sampleEnd }
xxxxxxxxxx
var cakesEaten = 0
while (cakesEaten < 3) {
println("Eat a cake")
cakesEaten++
}
// Eat a cake
// Eat a cake
// Eat a cake
In the second use case (do-while
):
Declare the conditional expression for your while loop to continue within parentheses
()
.Define the action you want to complete within curly braces
{}
with the keyworddo
.
fun main() { //sampleStart var cakesEaten = 0 var cakesBaked = 0 while (cakesEaten < 3) { println("Eat a cake") cakesEaten++ } do { println("Bake a cake") cakesBaked++ } while (cakesBaked < cakesEaten) // Eat a cake // Eat a cake // Eat a cake // Bake a cake // Bake a cake // Bake a cake //sampleEnd }
xxxxxxxxxx
var cakesEaten = 0
var cakesBaked = 0
while (cakesEaten < 3) {
println("Eat a cake")
cakesEaten++
}
do {
println("Bake a cake")
cakesBaked++
} while (cakesBaked < cakesEaten)
// Eat a cake
// Eat a cake
// Eat a cake
// Bake a cake
// Bake a cake
// Bake a cake
For more information and examples of conditional expressions and loops, see Conditions and loops.
Now that you know the fundamentals of Kotlin control flow, it's time to learn how to write your own functions.
Using a when
expression, update the following program so that when you input the names of GameBoy buttons, the actions are printed to output.
Button | Action |
---|---|
A | Yes |
B | No |
X | Menu |
Y | Nothing |
Other | There is no such button |
fun main() { val button = "A" println( // Write your code here ) }
xxxxxxxxxx
fun main() {
val button = "A"
println(
// Write your code here
)
}
Example solution
{...}
You have a program that counts pizza slices until there’s a whole pizza with 8 slices. Refactor this program in two ways:
Use a
while
loop.Use a
do-while
loop.
fun main() { var pizzaSlices = 0 // Start refactoring here pizzaSlices++ println("There's only $pizzaSlices slice/s of pizza :(") pizzaSlices++ println("There's only $pizzaSlices slice/s of pizza :(") pizzaSlices++ println("There's only $pizzaSlices slice/s of pizza :(") pizzaSlices++ println("There's only $pizzaSlices slice/s of pizza :(") pizzaSlices++ println("There's only $pizzaSlices slice/s of pizza :(") pizzaSlices++ println("There's only $pizzaSlices slice/s of pizza :(") pizzaSlices++ println("There's only $pizzaSlices slice/s of pizza :(") pizzaSlices++ // End refactoring here println("There are $pizzaSlices slices of pizza. Hooray! We have a whole pizza! :D") }
xxxxxxxxxx
fun main() {
var pizzaSlices = 0
// Start refactoring here
pizzaSlices++
println("There's only $pizzaSlices slice/s of pizza :(")
pizzaSlices++
println("There's only $pizzaSlices slice/s of pizza :(")
pizzaSlices++
println("There's only $pizzaSlices slice/s of pizza :(")
pizzaSlices++
println("There's only $pizzaSlices slice/s of pizza :(")
pizzaSlices++
println("There's only $pizzaSlices slice/s of pizza :(")
pizzaSlices++
println("There's only $pizzaSlices slice/s of pizza :(")
pizzaSlices++
println("There's only $pizzaSlices slice/s of pizza :(")
pizzaSlices++
// End refactoring here
println("There are $pizzaSlices slices of pizza. Hooray! We have a whole pizza! :D")
}
Example solution 1
{...}
Example solution 2
{...}
Write a program that simulates the Fizz buzz game. Your task is to print numbers from 1 to 100 incrementally, replacing any number divisible by three with the word "fizz", and any number divisible by five with the word "buzz". Any number divisible by both 3 and 5 must be replaced with the word "fizzbuzz".
fun main() { // Write your code here }
xxxxxxxxxx
fun main() {
// Write your code here
}
Example solution
{...}
You have a list of words. Use for
and if
to print only the words that start with the letter l
.
- Hint
Use the
.startsWith()
function forString
type.
fun main() { val words = listOf("dinosaur", "limousine", "magazine", "language") // Write your code here }
xxxxxxxxxx
fun main() {
val words = listOf("dinosaur", "limousine", "magazine", "language")
// Write your code here
}
Example solution
{...}
Thanks for your feedback!